Skip to content

Commit ebdd122

Browse files
committed
fix(bookmarks): Better handle attachments to prevent errors
fix(bookmarks): Better handle attachments to prevent errors from exceeding the discord character limit
1 parent 44c0a38 commit ebdd122

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

src/tux/modules/features/bookmarks.py

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
from __future__ import annotations
99

1010
import io
11+
from collections.abc import Sequence
1112

1213
import aiohttp
1314
import discord
15+
from discord import StickerFormatType, StickerItem
1416
from discord.abc import Messageable
1517
from discord.ext import commands
1618
from loguru import logger
@@ -181,7 +183,7 @@ async def _get_files_from_attachments(
181183
The list to append extracted files to.
182184
"""
183185
for attachment in message.attachments:
184-
if len(files) >= 10:
186+
if len(files) > 10:
185187
break
186188

187189
if attachment.content_type and "image" in attachment.content_type:
@@ -240,11 +242,11 @@ async def _get_files_from_embeds(
240242
files : list[discord.File]
241243
The list to append extracted files to.
242244
"""
243-
if len(files) >= 10:
245+
if len(files) > 10:
244246
return
245247

246248
for embed in message.embeds:
247-
if len(files) >= 10:
249+
if len(files) > 10:
248250
break
249251

250252
if embed.image and embed.image.url:
@@ -311,13 +313,56 @@ def _create_bookmark_embed(self, message: discord.Message) -> discord.Embed:
311313
if len(content) > EMBED_MAX_DESC_LENGTH:
312314
content = f"{content[: EMBED_MAX_DESC_LENGTH - 4]}..."
313315

316+
# Prepare attachment bullets
317+
attachment_list = ""
318+
if message.attachments:
319+
attachment_list = "\n".join(
320+
f"• [{att.filename}]({att.url})" for att in message.attachments
321+
)
322+
323+
sticker_list: str = ""
324+
325+
stickers: Sequence[StickerItem] = message.stickers
326+
327+
if stickers:
328+
bullets: list[str] = [
329+
f"• [{sticker.name}](https://cdn.discordapp.com/stickers/{sticker.id}.png)"
330+
if sticker.format in {StickerFormatType.png, StickerFormatType.apng}
331+
and sticker.id != 0
332+
else f"• {sticker.name}"
333+
for sticker in stickers
334+
]
335+
sticker_list = "\n".join(bullets)
336+
337+
# Combine everything into the embed description and enforce the max length
338+
339+
parts: list[str] = []
340+
if content:
341+
parts.append(content)
342+
else:
343+
parts.append("> No content available to display")
344+
if attachment_list:
345+
parts.append(f"**Attachments:**\n{attachment_list}")
346+
if sticker_list:
347+
parts.append(f"**Stickers:**\n{sticker_list}")
348+
description = "\n\n".join(parts)
349+
350+
limit_warning = "\n\n*(Message cut off due to character limit.)*"
351+
# Ensure we never exceed Discord's embed description limit
352+
if len(description) > EMBED_MAX_DESC_LENGTH:
353+
# reserve space for the notice
354+
cutoff = EMBED_MAX_DESC_LENGTH - len(limit_warning)
355+
356+
# avoid negative slice sizes in extreme cases
357+
cutoff = max(0, cutoff)
358+
359+
description = description[:cutoff].rstrip() + limit_warning
360+
314361
embed = EmbedCreator.create_embed(
315362
bot=self.bot,
316363
embed_type=EmbedCreator.INFO,
317364
title="Message Bookmarked",
318-
description=f"{content}"
319-
if content
320-
else "> No content available to display",
365+
description=description,
321366
)
322367

323368
# Add author to the embed
@@ -341,18 +386,6 @@ def _create_bookmark_embed(self, message: discord.Message) -> discord.Embed:
341386
value=f"[Click Here]({message.jump_url})",
342387
)
343388

344-
# Add attachments to the embed
345-
if message.attachments:
346-
attachments = "\n".join(
347-
f"[{a.filename}]({a.url})" for a in message.attachments
348-
)
349-
embed.add_field(name="Attachments", value=attachments, inline=False)
350-
351-
# Add stickers to the embed
352-
if message.stickers:
353-
stickers = "\n".join(f"[{s.name}]({s.url})" for s in message.stickers)
354-
embed.add_field(name="Stickers", value=stickers, inline=False)
355-
356389
# Handle embeds
357390
if message.embeds:
358391
embed.add_field(

0 commit comments

Comments
 (0)