Skip to content

Commit

Permalink
Fix for unhandled exception in context button commands
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanC committed Oct 25, 2024
1 parent 3c5336f commit 7423a3b
Showing 1 changed file with 51 additions and 40 deletions.
91 changes: 51 additions & 40 deletions extensions/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,43 +338,48 @@ async def EventContext(event: InteractionCreateEvent) -> None:

results: list[Embed] = []

for message in await context:
logger.trace(message)

fields: list[dict[str, str | bool]] = []

if attachments := message.attachments:
for attachment in attachments:
fields.append(
{
"name": "Attachment",
"value": f"[`{attachment.filename}`]({attachment.url})",
}
)

if stickers := message.stickers:
for sticker in stickers:
fields.append(
{
"name": "Sticker",
"value": f"[{sticker.name}]({sticker.image_url})",
}
try:
for message in await context:
logger.trace(message)

fields: list[dict[str, str | bool]] = []

if attachments := message.attachments:
for attachment in attachments:
fields.append(
{
"name": "Attachment",
"value": f"[`{attachment.filename}`]({attachment.url})",
}
)

if stickers := message.stickers:
for sticker in stickers:
fields.append(
{
"name": "Sticker",
"value": f"[{sticker.name}]({sticker.image_url})",
}
)

results.append(
Response(
color=Colors.DiscordBlurple.value,
description=f">>> {message.content}" if message.content else None,
fields=fields,
author=await ExpandUser(message.author, format=False),
authorIcon=GetUserAvatar(message.author),
footer=str(message.id),
footerIcon=await GetServerIcon(
message.guild_id, client=plugin.client
),
timestamp=message.created_at
if not message.edited_timestamp
else message.edited_timestamp,
)

results.append(
Response(
color=Colors.DiscordBlurple.value,
description=f">>> {message.content}" if message.content else None,
fields=fields,
author=await ExpandUser(message.author, format=False),
authorIcon=GetUserAvatar(message.author),
footer=str(message.id),
footerIcon=await GetServerIcon(message.guild_id, client=plugin.client),
timestamp=message.created_at
if not message.edited_timestamp
else message.edited_timestamp,
)
)
except Exception as e:
logger.opt(exception=e).error("Failed to fetch message(s) in context")

if len(results) < 1:
results.append(
Expand Down Expand Up @@ -437,12 +442,18 @@ async def EventDump(event: InteractionCreateEvent) -> None:
targetChnl, after=targetId
).limit(100)

# Reverse iterator to maintain chronological order
for message in await before.reversed():
context.append(message)
try:
# Reverse iterator to maintain chronological order
for message in await before.reversed():
context.append(message)
except Exception as e:
logger.opt(exception=e).error("Failed to fetch message(s) in context")

for message in await after:
context.append(message)
try:
for message in await after:
context.append(message)
except Exception as e:
logger.opt(exception=e).error("Failed to fetch message(s) in context")

logger.trace(context)

Expand Down

0 comments on commit 7423a3b

Please sign in to comment.