diff options
author | 2024-06-26 17:55:01 +0530 | |
---|---|---|
committer | 2024-06-26 17:55:01 +0530 | |
commit | 5a621e375df50e1ec04fc3ac0cf0f2ad89bb876a (patch) | |
tree | 4226a9c36a9d25ff632ed3eac3e646770e59236c | |
parent | Merge pull request #3103 from python-discord/dependabot/pip/pydis-core-11.2.0 (diff) |
Added message link to replied or parent message
Now we can check the replies to a deleted message to provide a better context as well as, if the deleted message is a reply to some parent message, it will show the respective parents message it was replying to in #mod-log channel
-rw-r--r-- | bot/exts/moderation/modlog.py | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/bot/exts/moderation/modlog.py b/bot/exts/moderation/modlog.py index 74bdc9db9..dcb2fa257 100644 --- a/bot/exts/moderation/modlog.py +++ b/bot/exts/moderation/modlog.py @@ -490,6 +490,7 @@ class ModLog(Cog, name="ModLog"): return channel.id in GuildConstant.modlog_blacklist + async def log_cached_deleted_message(self, message: discord.Message) -> None: """ Log the message's details to message change log. @@ -513,7 +514,7 @@ class ModLog(Cog, name="ModLog"): f"**Message ID:** `{message.id}`\n" f"**Sent at:** {format_dt(message.created_at)}\n" f"[Jump to message]({message.jump_url})\n" - "\n" + #"\n" ) else: response = ( @@ -522,14 +523,57 @@ class ModLog(Cog, name="ModLog"): f"**Message ID:** `{message.id}`\n" f"**Sent at:** {format_dt(message.created_at)}\n" f"[Jump to message]({message.jump_url})\n" - "\n" + #"\n" ) + # If the message is a reply, add the reference to the response + if message.reference is not None and message.reference.resolved is not None: + resolved_message = message.reference.resolved + + if isinstance(resolved_message, discord.Message): + jump_url = resolved_message.jump_url + if resolved_message.channel.category: + resolved_message_text = ( + f"{resolved_message.author} in " + f"{resolved_message.channel.category}/#{resolved_message.channel.name}:" + ) + else: + resolved_message_text = ( + f"{resolved_message.author} in " + f"#{resolved_message.channel.name}:" + ) + + #Shorten the message content if necessary + if len(resolved_message.clean_content) > 10: + resolved_clean_content = resolved_message.clean_content[:10] + "..." + else: + resolved_clean_content = resolved_message.clean_content + + reference_line = ( + f"**In reply to:** [{resolved_message_text}]({jump_url}) {resolved_clean_content}\n" + ) + response = reference_line + response + + # If the message is a parent messages and has replies to it, add the replies to the response + replies = await self.gather_replies(message) + if replies: + reply_lines: str = "" + for reply in replies: + if len(reply.clean_content) > 10: + short_content = reply.clean_content[:10] + "..." + reply_lines += f"\n- {format_user(reply.author)}[Jump to message]({reply.jump_url}) {short_content}" + else: + full_content = reply.clean_content + reply_lines += f"\n- {format_user(reply.author)}[Jump to message]({reply.jump_url}) {full_content}" + + response += "**Replies:**" + f"{reply_lines}\n" + if message.attachments: # Prepend the message metadata with the number of attachments response = f"**Attachments:** {len(message.attachments)}\n" + response # Shorten the message content if necessary + response += "\n**Deleted Message:**:\n" content = message.clean_content remaining_chars = 4090 - len(response) @@ -550,6 +594,14 @@ class ModLog(Cog, name="ModLog"): channel_id=Channels.message_log ) + async def gather_replies(self, message: discord.Message) -> list: + """Gather replies to the given message.""" + replies = [] + async for msg in message.channel.history(after=message.created_at, limit=100): + if msg.reference and msg.reference.message_id == message.id: + replies.append(msg) + return replies + async def log_uncached_deleted_message(self, event: discord.RawMessageDeleteEvent) -> None: """ Log the message's details to message change log. |