|
5 | 5 | from contextlib import suppress |
6 | 6 |
|
7 | 7 | import arrow |
| 8 | +import discord |
| 9 | +from aiohttp import ClientSession |
8 | 10 | from discord import Member, NotFound |
9 | 11 | from discord.ext import commands |
10 | 12 | from discord.ext.commands import Cog, Context |
11 | 13 | from pydis_core.utils import scheduling |
| 14 | +from pydis_core.utils.paste_service import PasteFile, PasteUploadError, send_to_paste_service |
12 | 15 |
|
13 | 16 | from bot.constants import Channels, DEBUG_MODE, RedirectOutput |
14 | 17 | from bot.log import get_logger |
@@ -154,8 +157,39 @@ async def inner(self: Cog, ctx: Context, *args, **kwargs) -> None: |
154 | 157 | log.trace(f"Redirecting output of {ctx.author}'s command '{ctx.command.name}' to {redirect_channel.name}") |
155 | 158 | ctx.channel = redirect_channel |
156 | 159 |
|
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 | + |
159 | 193 | scheduling.create_task(func(self, ctx, *args, **kwargs)) |
160 | 194 |
|
161 | 195 | message = await old_channel.send( |
|
0 commit comments