Skip to content

Commit

Permalink
[ExtendedModLog] 2.12.5 Improve invite logging
Browse files Browse the repository at this point in the history
- Include invite url in description of logged invites.
- Try and mention if an invite was generated by a widget that we can see.
- Try and mention the user who created the invite.
- If an invite is deleted try and look for them in the audit log.
- Put invite code in code blocks to more easily copy.
- Try and mention the user who deleted a message.
- Include reason on deleted cached messages if available.
  • Loading branch information
TrustyJAID committed Aug 31, 2024
1 parent 63e8065 commit 7d4f246
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TrustyJAID's Cogs for [Red-DiscordBot](https://github.com/Cog-Creators/Red-Disc
| Elements | 1.1.1 | <details><summary>Periodic table of elements</summary>Get a plethora of information about elements on the periodic table.</details> | TrustyJAID |
| Encoding | 1.3.1 | <details><summary>Encode messages into various types of encoding.</summary>Encode messages into various types of encoding. Encoding types include: DNA, binary, Caeser cipher, hex, base 64, character, and braille.</details> | TrustyJAID |
| EventPoster | 2.1.4 | <details><summary>Admin approved announcments/events</summary>Allow users to setup and host events to be approved by admins.</details> | TrustyJAID |
| ExtendedModLog | 2.12.4 | <details><summary>ExtendedModLog, track changes made in the server.</summary>Log changes within the server using extended modlogs, an extension of RedBot cores modlog.</details> | RePulsR and TrustyJAID |
| ExtendedModLog | 2.12.5 | <details><summary>ExtendedModLog, track changes made in the server.</summary>Log changes within the server using extended modlogs, an extension of RedBot cores modlog.</details> | RePulsR and TrustyJAID |
| Fenrir | 1.2.0 | <details><summary>Give users the option to kick, ban, or insult themselves via reactions.</summary>Create reaction messages to kick or ban users! https://tenor.com/view/order66-gif-9116581</details> | TrustyJAID |
| Fun | 1.4.0 | <details><summary>Various fun commands like react, textflip, and regional</summary>All sorts of commands that users may find fun or useful</details> | Appu and TrustyJAID |
| Hockey | 4.4.0 | <details><summary>Hockey commands</summary>A cog to gather hockey scores, schedules, player data and more!</details> | TrustyJAID |
Expand Down
77 changes: 72 additions & 5 deletions extendedmodlog/eventmixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ async def _cached_message_delete(
return
time = message.created_at
perp = None
reason = None
if channel.permissions_for(guild.me).view_audit_log and check_audit_log:
action = discord.AuditLogAction.message_delete
entry = await self.get_audit_log_entry(guild, message.author, action)
Expand Down Expand Up @@ -498,7 +499,9 @@ async def _cached_message_delete(
embed.add_field(name=_("Channel"), value=message_channel.mention)
embed.add_field(name=_("Author"), value=message.author.mention)
if perp:
embed.add_field(name=_("Deleted by"), value=perp)
embed.add_field(name=_("Deleted by"), value=perp.mention)
if reason:
embed.add_field(name=_("Reason"), value=reason)
if message.attachments:
files = "\n".join(f"- {inline(a.filename)}" for a in message.attachments)
embed.add_field(name=_("Attachments"), value=files[:1024])
Expand Down Expand Up @@ -1081,7 +1084,13 @@ async def get_audit_log_entry(
self,
guild: discord.Guild,
target: Union[
discord.abc.GuildChannel, discord.Member, discord.User, discord.Role, int, None
discord.abc.GuildChannel,
discord.Member,
discord.User,
discord.Role,
discord.Invite,
int,
None,
],
action: discord.AuditLogAction,
*,
Expand All @@ -1090,6 +1099,8 @@ async def get_audit_log_entry(
entry = None
if isinstance(target, int) or target is None:
target_id = target
elif isinstance(target, discord.Invite):
target_id = target.code
else:
target_id = target.id

Expand All @@ -1106,6 +1117,9 @@ async def get_audit_log_entry(
if target_id == getattr(log.target, "id", None):
logger.trace("Found entry through cache")
entry = log
if target_id == getattr(log.target, "code", None):
logger.trace("Found invite code entry through cache")
entry = log

if entry is None:
async for log in guild.audit_logs(limit=5, action=action):
Expand All @@ -1116,6 +1130,9 @@ async def get_audit_log_entry(
logger.trace("Found perp through fetch")
entry = log
break
if target_id == getattr(log.target, "code", None):
logger.trace("Found invite code entry through fetch")
entry = log
return entry

@commands.Cog.listener()
Expand Down Expand Up @@ -2005,7 +2022,9 @@ async def on_invite_create(self, invite: discord.Invite) -> None:
"""
New in discord.py 1.3
"""
guild = invite.guild
if invite.guild is None:
return
guild = self.bot.get_guild(invite.guild.id)
if guild.id not in self.settings:
return
if await self.bot.cog_disabled_in_guild(self, guild):
Expand Down Expand Up @@ -2047,6 +2066,7 @@ async def on_invite_create(self, invite: discord.Invite) -> None:
"max_uses": _("Max Uses:"),
"max_age": _("Max Age:"),
"temporary": _("Temporary:"),
"scheduled_event": _("Scheduled Event:"),
}
invite_time = invite.created_at or datetime.datetime.now(datetime.timezone.utc)
msg = _("{emoji} {time} Invite created ").format(
Expand All @@ -2059,15 +2079,33 @@ async def on_invite_create(self, invite: discord.Invite) -> None:
timestamp=invite_time,
)
worth_updating = False

if getattr(invite, "inviter", None):
embed.description = _("{author} created an invite for {channel}.").format(
author=invite.inviter.mention, channel=invite.channel.mention
author=getattr(invite.inviter, "mention", invite.inviter),
channel=getattr(invite.channel, "mention", str(invite.channel)),
)
elif guild.widget_enabled and guild.widget_channel:
embed.description = _("Widget in {channel} created a new invite.").format(
channel=guild.widget_channel.mention
)
if embed.description is None:
embed.description = invite.url
else:
embed.description += f"\n{invite.url}"
for attr, name in invite_attrs.items():
before_attr = getattr(invite, attr)
if before_attr:
if attr == "max_age":
before_attr = humanize_timedelta(seconds=before_attr)
if attr == "channel":
before_attr = getattr(before_attr, "mention", before_attr)
if attr == "inviter":
before_attr = getattr(before_attr, "mention", before_attr)
if attr == "code":
before_attr = box(before_attr)
if attr == "scheduled_event":
before_attr = getattr(invite.scheduled_event, "url", "")
worth_updating = True
msg += f"{name} {before_attr}\n"
embed.add_field(name=name, value=str(before_attr))
Expand All @@ -2083,7 +2121,9 @@ async def on_invite_delete(self, invite: discord.Invite) -> None:
"""
New in discord.py 1.3
"""
guild = invite.guild
if invite.guild is None:
return
guild = self.bot.get_guild(invite.guild.id)
if guild.id not in self.settings:
return
if await self.bot.cog_disabled_in_guild(self, guild):
Expand All @@ -2110,6 +2150,7 @@ async def on_invite_delete(self, invite: discord.Invite) -> None:
"uses": _("Used: "),
"max_age": _("Max Age:"),
"temporary": _("Temporary:"),
"scheduled_event": _("Scheduled Event:"),
}
invite_time = invite.created_at or datetime.datetime.now(datetime.timezone.utc)
msg = _("{emoji} {time} Invite deleted ").format(
Expand All @@ -2125,15 +2166,41 @@ async def on_invite_delete(self, invite: discord.Invite) -> None:
embed.description = _("{author} deleted or used up an invite for {channel}.").format(
author=invite.inviter.mention, channel=invite.channel.mention
)
elif guild.widget_enabled and guild.widget_channel:
embed.description = _("Widget in {channel} invite deleted or used up.").format(
channel=guild.widget_channel.mention
)
if embed.description is None:
embed.description = invite.url
else:
embed.description += f"\n{invite.url}"
entry = await self.get_audit_log_entry(guild, invite, discord.AuditLogAction.invite_delete)
perp = getattr(entry, "user", None)
reason = getattr(entry, "reason", None)
worth_updating = False
for attr, name in invite_attrs.items():
before_attr = getattr(invite, attr)
if before_attr:
if attr == "max_age":
before_attr = humanize_timedelta(seconds=before_attr)
if attr == "channel":
before_attr = getattr(before_attr, "mention", before_attr)
if attr == "inviter":
before_attr = getattr(before_attr, "mention", before_attr)
if attr == "code":
before_attr = box(before_attr)
if attr == "scheduled_event":
before_attr = getattr(invite.scheduled_event, "url", "")
worth_updating = True
msg += f"{name} {before_attr}\n"
embed.add_field(name=name, value=str(before_attr))
if perp:
perp_str = getattr(perp, "mention", str(perp))
msg += _("Deleted by") + f" {perp}.\n"
embed.add_field(name=_("Deleted by"), value=perp_str)
if reason:
msg += _("Reason") + f": {perp}\n"
embed.add_field(name=_("Reason"), value=reason)
if not worth_updating:
return
if embed_links:
Expand Down
2 changes: 1 addition & 1 deletion extendedmodlog/extendedmodlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ExtendedModLog(EventMixin, commands.Cog):
"""

__author__ = ["RePulsar", "TrustyJAID"]
__version__ = "2.12.4"
__version__ = "2.12.5"

def __init__(self, bot):
self.bot = bot
Expand Down

0 comments on commit 7d4f246

Please sign in to comment.