-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: show forwarded message in logviewer. #3414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lorenzo132
wants to merge
11
commits into
development
Choose a base branch
from
users/lorenzo/forwarded-msg-logviewer
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
88f00f7
fix: show forwarded message in logviewer.
lorenzo132 09069ff
fix: show forwarded message in logviewer.
lorenzo132 b0e03b0
fix: show forwarded message in logviewer.
lorenzo132 ddab052
black formatting
lorenzo132 c9dbcbc
Merge branch 'development' into users/lorenzo/forwarded-msg-logviewer
lorenzo132 7bb8e67
remove jump_url
lorenzo132 b570101
feat: adds attachment support to forwarded content.
lorenzo132 4cf543a
black formatting
lorenzo132 c3a0232
Merge branch 'development' into users/lorenzo/forwarded-msg-logviewer
lorenzo132 e9c1bfc
use: hassattr(), remove dedunant package
lorenzo132 2c1e6ce
Merge branch 'development' into users/lorenzo/forwarded-msg-logviewer
lorenzo132 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ | |
| "ConfirmThreadCreationView", | ||
| "DummyParam", | ||
| "extract_forwarded_content", | ||
| "extract_forwarded_attachments", | ||
| ] | ||
|
|
||
|
|
||
|
|
@@ -640,6 +641,37 @@ def __init__(self): | |
| self.value = None | ||
|
|
||
|
|
||
| def extract_forwarded_attachments(message) -> typing.List[typing.Tuple[str, str]]: | ||
| """ | ||
| Extract attachment URLs from forwarded messages. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| message : discord.Message | ||
| The message to extract attachments from. | ||
|
|
||
| Returns | ||
| ------- | ||
| List[Tuple[str, str]] | ||
| List of (url, filename) tuples for attachments. | ||
| """ | ||
| import discord | ||
|
|
||
| attachments = [] | ||
| try: | ||
| if getattr(message, "message_snapshots", None): | ||
lorenzo132 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for snap in message.message_snapshots: | ||
| if getattr(snap, "attachments", None): | ||
| for a in snap.attachments: | ||
| url = getattr(a, "url", None) | ||
| filename = getattr(a, "filename", "Unknown") | ||
| if url: | ||
| attachments.append((url.split("?")[0], filename)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| except Exception: | ||
| pass | ||
| return attachments | ||
|
|
||
|
|
||
| def extract_forwarded_content(message) -> typing.Optional[str]: | ||
| """ | ||
| Extract forwarded message content from Discord forwarded messages. | ||
|
|
@@ -658,39 +690,51 @@ def extract_forwarded_content(message) -> typing.Optional[str]: | |
|
|
||
| try: | ||
| # Handle multi-forward (message_snapshots) | ||
| if hasattr(message, "flags") and getattr(message.flags, "has_snapshot", False): | ||
| if hasattr(message, "message_snapshots") and message.message_snapshots: | ||
| forwarded_parts = [] | ||
| for snap in message.message_snapshots: | ||
| author = getattr(snap, "author", None) | ||
| author_name = getattr(author, "name", "Unknown") if author else "Unknown" | ||
| snap_content = getattr(snap, "content", "") | ||
|
|
||
| if snap_content: | ||
| # Truncate very long messages to prevent spam | ||
| if len(snap_content) > 500: | ||
| snap_content = snap_content[:497] + "..." | ||
| forwarded_parts.append(f"**{author_name}:** {snap_content}") | ||
| elif getattr(snap, "embeds", None): | ||
| for embed in snap.embeds: | ||
| if hasattr(embed, "description") and embed.description: | ||
| embed_desc = embed.description | ||
| if len(embed_desc) > 300: | ||
| embed_desc = embed_desc[:297] + "..." | ||
| forwarded_parts.append(f"**{author_name}:** {embed_desc}") | ||
| break | ||
| elif getattr(snap, "attachments", None): | ||
| attachment_info = ", ".join( | ||
| [getattr(a, "filename", "Unknown") for a in snap.attachments[:3]] | ||
| ) | ||
| if len(snap.attachments) > 3: | ||
| attachment_info += f" (+{len(snap.attachments) - 3} more)" | ||
| forwarded_parts.append(f"**{author_name}:** [Attachments: {attachment_info}]") | ||
| else: | ||
| forwarded_parts.append(f"**{author_name}:** [No content]") | ||
|
|
||
| if forwarded_parts: | ||
| return "\n".join(forwarded_parts) | ||
| # Check directly for snapshots as flags.has_snapshot can be unreliable in some versions | ||
| if getattr(message, "message_snapshots", None): | ||
| forwarded_parts = [] | ||
| for snap in message.message_snapshots: | ||
| author = getattr(snap, "author", None) | ||
| # If author is missing, we can try to rely on the container message context or just omit. | ||
| # Since we can't reliably get the original author from snapshot in this state, we focus on content. | ||
|
|
||
| snap_content = getattr(snap, "content", "") | ||
|
|
||
| formatted_part = "📨 **Forwarded Message**\n" | ||
|
|
||
| if snap_content: | ||
| if len(snap_content) > 500: | ||
| snap_content = snap_content[:497] + "..." | ||
| formatted_part += "\n".join([f"{line}" for line in snap_content.splitlines()]) + "\n" | ||
|
|
||
| if getattr(snap, "embeds", None): | ||
| for embed in snap.embeds: | ||
| if hasattr(embed, "description") and embed.description: | ||
| embed_desc = embed.description | ||
| if len(embed_desc) > 300: | ||
| embed_desc = embed_desc[:297] + "..." | ||
| formatted_part += ( | ||
| "\n".join([f"> {line}" for line in embed_desc.splitlines()]) + "\n" | ||
| ) | ||
| break # One embed preview is usually enough | ||
|
|
||
| if getattr(snap, "attachments", None): | ||
| attachment_links = [] | ||
| for a in snap.attachments[:3]: | ||
| filename = getattr(a, "filename", "Unknown") | ||
| url = getattr(a, "url", None) | ||
| if url: | ||
| url = url.split("?")[0] | ||
| attachment_links.append(f"[{filename}]({url})") | ||
| else: | ||
| attachment_links.append(filename) | ||
| if len(snap.attachments) > 3: | ||
| attachment_links.append(f"(+{len(snap.attachments) - 3} more)") | ||
| formatted_part += f"📎 {', '.join(attachment_links)}\n" | ||
| forwarded_parts.append(formatted_part) | ||
|
|
||
| if forwarded_parts: | ||
| return "\n".join(forwarded_parts) | ||
|
|
||
| # Handle single-message forward | ||
| elif getattr(message, "type", None) == getattr(discord.MessageType, "forward", None): | ||
|
|
@@ -719,12 +763,18 @@ def extract_forwarded_content(message) -> typing.Optional[str]: | |
| embed_desc = embed_desc[:297] + "..." | ||
| return f"**{ref_author_name}:** {embed_desc}" | ||
| elif getattr(ref_msg, "attachments", None): | ||
| attachment_info = ", ".join( | ||
| [getattr(a, "filename", "Unknown") for a in ref_msg.attachments[:3]] | ||
| ) | ||
| attachment_links = [] | ||
| for a in ref_msg.attachments[:3]: | ||
| filename = getattr(a, "filename", "Unknown") | ||
| url = getattr(a, "url", None) | ||
| if url: | ||
| url = url.split("?")[0] | ||
| attachment_links.append(f"[{filename}]({url})") | ||
| else: | ||
| attachment_links.append(filename) | ||
| if len(ref_msg.attachments) > 3: | ||
| attachment_info += f" (+{len(ref_msg.attachments) - 3} more)" | ||
| return f"**{ref_author_name}:** [Attachments: {attachment_info}]" | ||
| attachment_links.append(f"(+{len(ref_msg.attachments) - 3} more)") | ||
| return f"**{ref_author_name}:** 📎 {', '.join(attachment_links)}" | ||
| except Exception as e: | ||
| # Log and continue; failing to extract a reference preview shouldn't break flow | ||
| logger.debug("Failed to extract reference preview: %s", e) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


Uh oh!
There was an error while loading. Please reload this page.