Skip to content

Commit 2009691

Browse files
committed
fix: show forwarded message in logviewer.
1 parent 4a20960 commit 2009691

3 files changed

Lines changed: 59 additions & 41 deletions

File tree

core/clients.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,18 @@ async def append_log(
661661
channel_id: str = "",
662662
type_: str = "thread_message",
663663
) -> dict:
664+
from core.utils import extract_forwarded_content
665+
664666
channel_id = str(channel_id) or str(message.channel.id)
665667
message_id = str(message_id) or str(message.id)
666668

669+
content = message.content or ""
670+
if forwarded := extract_forwarded_content(message):
671+
if content:
672+
content += "\n" + forwarded
673+
else:
674+
content = forwarded
675+
667676
data = {
668677
"timestamp": str(message.created_at),
669678
"message_id": message_id,
@@ -674,7 +683,7 @@ async def append_log(
674683
"avatar_url": message.author.display_avatar.url if message.author.display_avatar else None,
675684
"mod": not isinstance(message.channel, DMChannel),
676685
},
677-
"content": message.content,
686+
"content": content,
678687
"type": type_,
679688
"attachments": [
680689
{

core/thread.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ async def snooze(self, moderator=None, command_used=None, snooze_for=None):
205205
"messages": [
206206
{
207207
"author_id": m.author.id,
208-
"content": m.content,
208+
"content": (
209+
(m.content or "")
210+
+ (("\n" + extract_forwarded_content(m)) if extract_forwarded_content(m) else "")
211+
).strip(),
209212
"attachments": [a.url for a in m.attachments],
210213
"embeds": [e.to_dict() for e in m.embeds],
211214
"created_at": m.created_at.isoformat(),
@@ -224,16 +227,12 @@ async def snooze(self, moderator=None, command_used=None, snooze_for=None):
224227
"author_name": (
225228
getattr(m.embeds[0].author, "name", "").split(" (")[0]
226229
if m.embeds and m.embeds[0].author and m.author == self.bot.user
227-
else getattr(m.author, "name", None)
228-
if m.author != self.bot.user
229-
else None
230+
else getattr(m.author, "name", None) if m.author != self.bot.user else None
230231
),
231232
"author_avatar": (
232233
getattr(m.embeds[0].author, "icon_url", None)
233234
if m.embeds and m.embeds[0].author and m.author == self.bot.user
234-
else m.author.display_avatar.url
235-
if m.author != self.bot.user
236-
else None
235+
else m.author.display_avatar.url if m.author != self.bot.user else None
237236
),
238237
}
239238
async for m in channel.history(limit=None, oldest_first=True)

core/utils.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -658,39 +658,49 @@ def extract_forwarded_content(message) -> typing.Optional[str]:
658658

659659
try:
660660
# Handle multi-forward (message_snapshots)
661-
if hasattr(message, "flags") and getattr(message.flags, "has_snapshot", False):
662-
if hasattr(message, "message_snapshots") and message.message_snapshots:
663-
forwarded_parts = []
664-
for snap in message.message_snapshots:
665-
author = getattr(snap, "author", None)
666-
author_name = getattr(author, "name", "Unknown") if author else "Unknown"
667-
snap_content = getattr(snap, "content", "")
668-
669-
if snap_content:
670-
# Truncate very long messages to prevent spam
671-
if len(snap_content) > 500:
672-
snap_content = snap_content[:497] + "..."
673-
forwarded_parts.append(f"**{author_name}:** {snap_content}")
674-
elif getattr(snap, "embeds", None):
675-
for embed in snap.embeds:
676-
if hasattr(embed, "description") and embed.description:
677-
embed_desc = embed.description
678-
if len(embed_desc) > 300:
679-
embed_desc = embed_desc[:297] + "..."
680-
forwarded_parts.append(f"**{author_name}:** {embed_desc}")
681-
break
682-
elif getattr(snap, "attachments", None):
683-
attachment_info = ", ".join(
684-
[getattr(a, "filename", "Unknown") for a in snap.attachments[:3]]
685-
)
686-
if len(snap.attachments) > 3:
687-
attachment_info += f" (+{len(snap.attachments) - 3} more)"
688-
forwarded_parts.append(f"**{author_name}:** [Attachments: {attachment_info}]")
689-
else:
690-
forwarded_parts.append(f"**{author_name}:** [No content]")
691-
692-
if forwarded_parts:
693-
return "\n".join(forwarded_parts)
661+
# Check directly for snapshots as flags.has_snapshot can be unreliable in some versions
662+
if getattr(message, "message_snapshots", None):
663+
forwarded_parts = []
664+
for snap in message.message_snapshots:
665+
author = getattr(snap, "author", None)
666+
# If author is missing, we can try to rely on the container message context or just omit.
667+
# Since we can't reliably get the original author from snapshot in this state, we focus on content.
668+
669+
snap_content = getattr(snap, "content", "")
670+
671+
formatted_part = "📨 **Forwarded Message**\n"
672+
673+
if snap_content:
674+
if len(snap_content) > 500:
675+
snap_content = snap_content[:497] + "..."
676+
formatted_part += "\n".join([f"{line}" for line in snap_content.splitlines()]) + "\n"
677+
678+
if getattr(snap, "embeds", None):
679+
for embed in snap.embeds:
680+
if hasattr(embed, "description") and embed.description:
681+
embed_desc = embed.description
682+
if len(embed_desc) > 300:
683+
embed_desc = embed_desc[:297] + "..."
684+
formatted_part += (
685+
"\n".join([f"> {line}" for line in embed_desc.splitlines()]) + "\n"
686+
)
687+
break # One embed preview is usually enough
688+
689+
if getattr(snap, "attachments", None):
690+
attachment_info = ", ".join(
691+
[getattr(a, "filename", "Unknown") for a in snap.attachments[:3]]
692+
)
693+
if len(snap.attachments) > 3:
694+
attachment_info += f" (+{len(snap.attachments) - 3} more)"
695+
formatted_part += f"[Attachments: {attachment_info}]\n"
696+
697+
# Add source link to the container message since snapshot doesn't have its own public link
698+
formatted_part += f"\n**Source:** {message.jump_url}"
699+
700+
forwarded_parts.append(formatted_part)
701+
702+
if forwarded_parts:
703+
return "\n".join(forwarded_parts)
694704

695705
# Handle single-message forward
696706
elif getattr(message, "type", None) == getattr(discord.MessageType, "forward", None):

0 commit comments

Comments
 (0)