Skip to content

Commit e5bca50

Browse files
fix: check for unknown/None content_type for pastebin uploader
1 parent 6d7400e commit e5bca50

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

bot/exts/utils/attachment_pastebin_uploader.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,24 @@ async def on_message_delete(self, message: discord.Message) -> None:
7777
@commands.Cog.listener()
7878
async def on_message(self, message: discord.Message) -> None:
7979
"""Listens for messages containing attachments and offers to upload them to the pastebin."""
80-
# Check if the message contains an embedded file and is not sent by a bot or in DMs.
81-
if message.author.bot or not message.guild or not any("charset" in a.content_type for a in message.attachments):
80+
# Check the message is not sent by a bot or in DMs.
81+
if message.author.bot or not message.guild:
82+
return
83+
84+
# Check if the message contains any text-based attachments.
85+
# Note that Discord does not always provide a content type
86+
# its not good enough to check the content type is text/plain,
87+
# we still need to check the charset
88+
attachments: list[discord.Attachment] = []
89+
for attachment in message.attachments:
90+
if (
91+
attachment.content_type
92+
and attachment.content_type.startswith("text/plain")
93+
and "charset" in attachment.content_type
94+
):
95+
attachments.append(attachment)
96+
97+
if not attachments:
8298
return
8399

84100
log.trace(f"Offering to upload attachments for {message.author} in {message.channel}, message {message.id}")
@@ -106,11 +122,7 @@ async def on_message(self, message: discord.Message) -> None:
106122
self.pending_messages.discard(message.id)
107123

108124
# Extract the attachments.
109-
files = [
110-
await self._convert_attachment(f)
111-
for f in message.attachments
112-
if "charset" in f.content_type
113-
]
125+
files = [await self._convert_attachment(f) for f in attachments]
114126

115127
# Upload the files to the paste bin, exiting early if there's an error.
116128
log.trace(f"Attempting to upload {len(files)} file(s) to pastebin.")

0 commit comments

Comments
 (0)