diff --git a/bot/exts/filtering/filtering.py b/bot/exts/filtering/filtering.py index 97b194d658..210ae3fb05 100644 --- a/bot/exts/filtering/filtering.py +++ b/bot/exts/filtering/filtering.py @@ -235,7 +235,7 @@ async def on_message(self, msg: Message) -> None: text_contents = [ await _extract_text_file_content(a) - for a in msg.attachments if "charset" in a.content_type + for a in msg.attachments if a.content_type and "charset" in a.content_type ] if text_contents: diff --git a/bot/exts/utils/attachment_pastebin_uploader.py b/bot/exts/utils/attachment_pastebin_uploader.py index 1740baee94..c788111d99 100644 --- a/bot/exts/utils/attachment_pastebin_uploader.py +++ b/bot/exts/utils/attachment_pastebin_uploader.py @@ -75,8 +75,21 @@ async def on_message_delete(self, message: discord.Message) -> None: @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 or in DMs. - if message.author.bot or not message.guild or not any("charset" in a.content_type for a in message.attachments): + # Check the message is not sent by a bot or in DMs. + if message.author.bot or not message.guild: + return + + # Check if the message contains any text-based attachments. + # we only require a charset here, as its setting is matched + attachments: list[discord.Attachment] = [] + for attachment in message.attachments: + if ( + attachment.content_type + and "charset" in attachment.content_type + ): + attachments.append(attachment) + + if not attachments: return log.trace(f"Offering to upload attachments for {message.author} in {message.channel}, message {message.id}") @@ -104,11 +117,7 @@ async def on_message(self, message: discord.Message) -> None: self.pending_messages.discard(message.id) # Extract the attachments. - files = [ - await self._convert_attachment(f) - for f in message.attachments - if "charset" in f.content_type - ] + files = [await self._convert_attachment(f) for f in attachments] # Upload the files to the paste bin, exiting early if there's an error. log.trace(f"Attempting to upload {len(files)} file(s) to pastebin.")