Skip to content

Commit e9c1bfc

Browse files
committed
use: hassattr(), remove dedunant package
1 parent c3a0232 commit e9c1bfc

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

core/utils.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -655,13 +655,11 @@ def extract_forwarded_attachments(message) -> typing.List[typing.Tuple[str, str]
655655
List[Tuple[str, str]]
656656
List of (url, filename) tuples for attachments.
657657
"""
658-
import discord
659-
660658
attachments = []
661659
try:
662-
if getattr(message, "message_snapshots", None):
660+
if hasattr(message, "message_snapshots") and message.message_snapshots:
663661
for snap in message.message_snapshots:
664-
if getattr(snap, "attachments", None):
662+
if hasattr(snap, "attachments") and snap.attachments:
665663
for a in snap.attachments:
666664
url = getattr(a, "url", None)
667665
filename = getattr(a, "filename", "Unknown")
@@ -686,15 +684,12 @@ def extract_forwarded_content(message) -> typing.Optional[str]:
686684
Optional[str]
687685
The extracted forwarded content, or None if not a forwarded message.
688686
"""
689-
import discord
690-
691687
try:
692688
# Handle multi-forward (message_snapshots)
693689
# Check directly for snapshots as flags.has_snapshot can be unreliable in some versions
694-
if getattr(message, "message_snapshots", None):
690+
if hasattr(message, "message_snapshots") and message.message_snapshots:
695691
forwarded_parts = []
696692
for snap in message.message_snapshots:
697-
author = getattr(snap, "author", None)
698693
# If author is missing, we can try to rely on the container message context or just omit.
699694
# Since we can't reliably get the original author from snapshot in this state, we focus on content.
700695

@@ -707,7 +702,7 @@ def extract_forwarded_content(message) -> typing.Optional[str]:
707702
snap_content = snap_content[:497] + "..."
708703
formatted_part += "\n".join([f"{line}" for line in snap_content.splitlines()]) + "\n"
709704

710-
if getattr(snap, "embeds", None):
705+
if hasattr(snap, "embeds") and snap.embeds:
711706
for embed in snap.embeds:
712707
if hasattr(embed, "description") and embed.description:
713708
embed_desc = embed.description
@@ -718,26 +713,25 @@ def extract_forwarded_content(message) -> typing.Optional[str]:
718713
)
719714
break # One embed preview is usually enough
720715

721-
if getattr(snap, "attachments", None):
722-
attachment_links = []
716+
if hasattr(snap, "attachments") and snap.attachments:
717+
attachment_urls = []
723718
for a in snap.attachments[:3]:
724-
filename = getattr(a, "filename", "Unknown")
725719
url = getattr(a, "url", None)
726720
if url:
727-
url = url.split("?")[0]
728-
attachment_links.append(f"[{filename}]({url})")
729-
else:
730-
attachment_links.append(filename)
721+
# Use direct URL for proper embedding in logviewer
722+
attachment_urls.append(url.split("?")[0])
731723
if len(snap.attachments) > 3:
732-
attachment_links.append(f"(+{len(snap.attachments) - 3} more)")
733-
formatted_part += f"📎 {', '.join(attachment_links)}\n"
724+
formatted_part += f"📎 (+{len(snap.attachments) - 3} more attachments)\n"
725+
# Add URLs on separate lines for proper embedding
726+
for url in attachment_urls:
727+
formatted_part += f"{url}\n"
734728
forwarded_parts.append(formatted_part)
735729

736730
if forwarded_parts:
737731
return "\n".join(forwarded_parts)
738732

739733
# Handle single-message forward
740-
elif getattr(message, "type", None) == getattr(discord.MessageType, "forward", None):
734+
elif hasattr(message, "type") and message.type == getattr(discord.MessageType, "forward", None):
741735
ref = getattr(message, "reference", None)
742736
if (
743737
ref
@@ -755,26 +749,27 @@ def extract_forwarded_content(message) -> typing.Optional[str]:
755749
if len(ref_content) > 500:
756750
ref_content = ref_content[:497] + "..."
757751
return f"**{ref_author_name}:** {ref_content}"
758-
elif getattr(ref_msg, "embeds", None):
752+
elif hasattr(ref_msg, "embeds") and ref_msg.embeds:
759753
for embed in ref_msg.embeds:
760754
if hasattr(embed, "description") and embed.description:
761755
embed_desc = embed.description
762756
if len(embed_desc) > 300:
763757
embed_desc = embed_desc[:297] + "..."
764758
return f"**{ref_author_name}:** {embed_desc}"
765-
elif getattr(ref_msg, "attachments", None):
766-
attachment_links = []
759+
elif hasattr(ref_msg, "attachments") and ref_msg.attachments:
760+
attachment_urls = []
767761
for a in ref_msg.attachments[:3]:
768-
filename = getattr(a, "filename", "Unknown")
769762
url = getattr(a, "url", None)
770763
if url:
771-
url = url.split("?")[0]
772-
attachment_links.append(f"[{filename}]({url})")
773-
else:
774-
attachment_links.append(filename)
764+
# Use direct URL for proper embedding in logviewer
765+
attachment_urls.append(url.split("?")[0])
766+
result = f"**{ref_author_name}:** 📎\n"
775767
if len(ref_msg.attachments) > 3:
776-
attachment_links.append(f"(+{len(ref_msg.attachments) - 3} more)")
777-
return f"**{ref_author_name}:** 📎 {', '.join(attachment_links)}"
768+
result += f"(+{len(ref_msg.attachments) - 3} more attachments)\n"
769+
# Add URLs on separate lines for proper embedding
770+
for url in attachment_urls:
771+
result += f"{url}\n"
772+
return result
778773
except Exception as e:
779774
# Log and continue; failing to extract a reference preview shouldn't break flow
780775
logger.debug("Failed to extract reference preview: %s", e)

0 commit comments

Comments
 (0)