diff options
| author | 2025-01-29 20:49:18 -0500 | |
|---|---|---|
| committer | 2025-01-29 20:49:18 -0500 | |
| commit | c19aeaab3704c60c6dda9b7ed3d207cfc3ff50aa (patch) | |
| tree | 0f352052673f49e5ce8342d31fd6ebff3fd3ab5d | |
| parent | Add logging; expand initial message from bot; fix bug in deleting the paste. (diff) | |
Account for messages with attachments being deleted before author reacts.
Messages might be deleted immediately if the message or the attachment trips a filter, in which case we don't want the user to be able to upload them.
| -rw-r--r-- | bot/exts/utils/attachment_pastebin_uploader.py | 14 | 
1 files changed, 14 insertions, 0 deletions
diff --git a/bot/exts/utils/attachment_pastebin_uploader.py b/bot/exts/utils/attachment_pastebin_uploader.py index 156814686..6c54a4a2b 100644 --- a/bot/exts/utils/attachment_pastebin_uploader.py +++ b/bot/exts/utils/attachment_pastebin_uploader.py @@ -33,6 +33,7 @@ class EmbedFileHandler(commands.Cog):      def __init__(self, bot: Bot):          self.bot = bot +        self.pending_messages = set[int]()      @staticmethod      async def _convert_attachment(attachment: discord.Attachment) -> paste_service.PasteFile: @@ -42,6 +43,11 @@ class EmbedFileHandler(commands.Cog):          return paste_service.PasteFile(content=file_content, name=attachment.filename)      @commands.Cog.listener() +    async def on_message_delete(self, message: discord.Message) -> None: +        """Allows us to know which messages with attachments have been deleted.""" +        self.pending_messages.discard(message.id) + +    @commands.Cog.listener()      async def on_message(self, message: discord.Message) -> None:          """Listens for messages containing attachments and offers to upload them to the pastebin."""          # Check if the message contains an embedded file and is not sent by a bot. @@ -49,6 +55,7 @@ class EmbedFileHandler(commands.Cog):              return          log.trace(f"Offering to upload attachments for {message.author} in {message.channel}, message {message.id}") +        self.pending_messages.add(message.id)          # Offer to upload the attachments and wait for the user's reaction.          bot_reply = await message.reply( @@ -72,8 +79,15 @@ class EmbedFileHandler(commands.Cog):              log.trace(f"{message.author} didn't give permission to upload {message.id} content; aborting.")              await bot_reply.edit(content=f"~~{bot_reply.content}~~")              await bot_reply.clear_reactions() + +        if message.id not in self.pending_messages: +            log.trace(f"{message.author}'s message was deleted before the attachments could be uploaded; aborting.") +            await bot_reply.delete()              return +        # In either case, we do not want the message ID in pending_messages anymore. +        self.pending_messages.discard(message.id) +          # Extract the attachments.          files = [              await self._convert_attachment(f)  |