Skip to content

Commit a0e6853

Browse files
DragonSenseiGuyjb3
andauthored
Preserve Invocation Context (#3377)
When redirecting commands, upload a paste of the original invocation command to the paste server for users to copy/paste. --------- Co-authored-by: Joe Banks <[email protected]>
1 parent fe2dabf commit a0e6853

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

bot/decorators.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
from contextlib import suppress
66

77
import arrow
8+
import discord
9+
from aiohttp import ClientSession
810
from discord import Member, NotFound
911
from discord.ext import commands
1012
from discord.ext.commands import Cog, Context
1113
from pydis_core.utils import scheduling
14+
from pydis_core.utils.paste_service import PasteFile, PasteUploadError, send_to_paste_service
1215

1316
from bot.constants import Channels, DEBUG_MODE, RedirectOutput
1417
from bot.log import get_logger
@@ -154,8 +157,39 @@ async def inner(self: Cog, ctx: Context, *args, **kwargs) -> None:
154157
log.trace(f"Redirecting output of {ctx.author}'s command '{ctx.command.name}' to {redirect_channel.name}")
155158
ctx.channel = redirect_channel
156159

157-
if ping_user:
158-
await ctx.send(f"Here's the output of your command, {ctx.author.mention}")
160+
paste_response = None
161+
if RedirectOutput.delete_invocation:
162+
async with ClientSession() as session:
163+
try:
164+
paste_response = await send_to_paste_service(
165+
files=[PasteFile(content=ctx.message.content, lexer="markdown")],
166+
http_session=session,
167+
)
168+
except PasteUploadError:
169+
log.exception(
170+
"Failed to upload message %d in channel %d to paste service when redirecting output",
171+
ctx.message.id, ctx.message.channel.id
172+
)
173+
174+
msg = "Here's the output of "
175+
msg += f"[your command]({paste_response.link})" if paste_response else "your command"
176+
msg += f", {ctx.author.mention}:" if ping_user else ":"
177+
178+
await ctx.send(msg)
179+
if paste_response:
180+
try:
181+
# Send a DM to the user about the redirect and paste removal
182+
await ctx.author.send(
183+
f"Your command output was redirected to <#{Channels.bot_commands}>."
184+
f" [Click here](<{paste_response.removal}>) to delete the pasted"
185+
" copy of your original command."
186+
)
187+
except discord.Forbidden:
188+
log.info(
189+
"Failed to DM %s with redirected command paste removal link, user has bot DMs disabled",
190+
ctx.author.name
191+
)
192+
159193
scheduling.create_task(func(self, ctx, *args, **kwargs))
160194

161195
message = await old_channel.send(

0 commit comments

Comments
 (0)