diff options
author | 2025-09-22 20:14:50 -0400 | |
---|---|---|
committer | 2025-09-23 01:14:50 +0100 | |
commit | a0e6853f57bc0b1960103e5916daba78e61e5751 (patch) | |
tree | b55b53c978c5b45e53852448c8e61ce423a5f84a | |
parent | Merge pull request #3385 from onerandomusername/patch-1 (diff) |
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]>
-rw-r--r-- | bot/decorators.py | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/bot/decorators.py b/bot/decorators.py index a68f85d5b..8a29a67d5 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -5,10 +5,13 @@ import typing as t from contextlib import suppress import arrow +import discord +from aiohttp import ClientSession from discord import Member, NotFound from discord.ext import commands from discord.ext.commands import Cog, Context from pydis_core.utils import scheduling +from pydis_core.utils.paste_service import PasteFile, PasteUploadError, send_to_paste_service from bot.constants import Channels, DEBUG_MODE, RedirectOutput from bot.log import get_logger @@ -154,8 +157,39 @@ def redirect_output( log.trace(f"Redirecting output of {ctx.author}'s command '{ctx.command.name}' to {redirect_channel.name}") ctx.channel = redirect_channel - if ping_user: - await ctx.send(f"Here's the output of your command, {ctx.author.mention}") + paste_response = None + if RedirectOutput.delete_invocation: + async with ClientSession() as session: + try: + paste_response = await send_to_paste_service( + files=[PasteFile(content=ctx.message.content, lexer="markdown")], + http_session=session, + ) + except PasteUploadError: + log.exception( + "Failed to upload message %d in channel %d to paste service when redirecting output", + ctx.message.id, ctx.message.channel.id + ) + + msg = "Here's the output of " + msg += f"[your command]({paste_response.link})" if paste_response else "your command" + msg += f", {ctx.author.mention}:" if ping_user else ":" + + await ctx.send(msg) + if paste_response: + try: + # Send a DM to the user about the redirect and paste removal + await ctx.author.send( + f"Your command output was redirected to <#{Channels.bot_commands}>." + f" [Click here](<{paste_response.removal}>) to delete the pasted" + " copy of your original command." + ) + except discord.Forbidden: + log.info( + "Failed to DM %s with redirected command paste removal link, user has bot DMs disabled", + ctx.author.name + ) + scheduling.create_task(func(self, ctx, *args, **kwargs)) message = await old_channel.send( |