diff --git a/changelog.md b/changelog.md index ac9b36a2..9f74aa9d 100644 --- a/changelog.md +++ b/changelog.md @@ -38,11 +38,21 @@ Changes since 2026.11.07 ### Moderator - Fixed edge case bugs when warning people causing the warn command to not work +- Warn modlogs now show total warnings after event +- Fixed edge case bug where a warn could be both applied and not applied at the same time ### Modmail - Removed the invidiual ban commands in favor of a joint feature ban system. - Made a new system for confirming new modmail threads +### Modlog +- Fixes edge case bugs if the moderator or member deleted their accounts +- Adds new channel field to all events + +### Notes +- Note modlogs now show total notes after event +- Note modlogs now use the note as the reason instead of adding a different field + ### Report - Added ability to ban people from report diff --git a/database/schema.py b/database/schema.py index 04976319..2a5a2d47 100644 --- a/database/schema.py +++ b/database/schema.py @@ -230,6 +230,7 @@ class ModLog(Table, tablename="modlog"): data (Varchar): Any extra data specific to the event logged moderator_id (Varchar): The ID of the moderator who banned member_id (Varchar): The ID of the user who was banned + channel_id (Varchar): The ID of the channel related to this event action_time (Timestamptz): The date and time of the ban until_time (Timestamptz): The time this action will expire unique_guild_case (Unique): Prevents duplicate case numbers within a guild @@ -243,6 +244,7 @@ class ModLog(Table, tablename="modlog"): data: Varchar = Varchar(length=None, null=True, default=None) moderator_id: Varchar = Varchar(length=None, null=True, default=None) member_id: Varchar = Varchar(length=None, null=True, default=None) + channel_id: Varchar = Varchar(length=None, null=True, default=None) action_time: Timestamptz = Timestamptz() until_time: Timestamptz = Timestamptz(null=True, default=None) diff --git a/modules/moderation/automod.py b/modules/moderation/automod.py index 10b2edf0..c72f94e0 100644 --- a/modules/moderation/automod.py +++ b/modules/moderation/automod.py @@ -173,6 +173,7 @@ async def response( action_type="timeout", guild=ctx.guild, member=ctx.author, + channel=ctx.channel, reason=total_punishment.violation_string, expires_at=expires_at, data=f"**Violating message:** {ctx.message.clean_content[:200]}", @@ -201,6 +202,7 @@ async def response( action_type="warn", guild=ctx.guild, member=ctx.author, + channel=ctx.channel, reason=total_punishment.violation_string, data=f"**Violating message:** {ctx.message.clean_content[:200]}", ) @@ -245,6 +247,7 @@ async def response( action_type="ban", guild=ctx.guild, member=ctx.author, + channel=ctx.channel, reason=total_punishment.violation_string, data=f"**Violating message:** {ctx.message.clean_content[:100]}", ) @@ -255,6 +258,7 @@ async def response( action_type="automod notice", guild=ctx.guild, member=ctx.author, + channel=ctx.channel, reason=total_punishment.violation_string, data=f"**Violating message:** {ctx.message.clean_content[:200]}", ) diff --git a/modules/moderation/featureban.py b/modules/moderation/featureban.py index be8a2322..51c13717 100644 --- a/modules/moderation/featureban.py +++ b/modules/moderation/featureban.py @@ -267,6 +267,7 @@ async def update_feature_ban( guild=interaction.guild, member=user, moderator=interaction.user, + channel=interaction.channel, reason=reason, ) diff --git a/modules/moderation/honeypot.py b/modules/moderation/honeypot.py index 48873233..0c0de8c5 100644 --- a/modules/moderation/honeypot.py +++ b/modules/moderation/honeypot.py @@ -66,6 +66,7 @@ async def response( action_type="Honeypot", guild=ctx.guild, member=ctx.author, + channel=ctx.channel, data=( f"**Content:** {ctx.message.clean_content[:500]}\n" f"**Attachments:** {len(ctx.message.attachments)}" diff --git a/modules/moderation/moderator.py b/modules/moderation/moderator.py index 9f657ab2..0b353fc5 100644 --- a/modules/moderation/moderator.py +++ b/modules/moderation/moderator.py @@ -113,6 +113,7 @@ async def handle_ban_user( guild=interaction.guild, member=target, moderator=interaction.user, + channel=interaction.channel, reason=reason, ) @@ -175,6 +176,7 @@ async def handle_unban_user( guild=interaction.guild, member=target, moderator=interaction.user, + channel=interaction.channel, reason=reason, ) @@ -233,6 +235,7 @@ async def handle_kick_user( guild=interaction.guild, member=target, moderator=interaction.user, + channel=interaction.channel, reason=reason, ) @@ -332,6 +335,7 @@ async def handle_mute_user( guild=interaction.guild, member=target, moderator=interaction.user, + channel=interaction.channel, reason=reason, expires_at=expires_at, ) @@ -402,6 +406,7 @@ async def handle_unmute_user( guild=interaction.guild, member=target, moderator=interaction.user, + channel=interaction.channel, reason=reason, ) if not result: @@ -458,13 +463,19 @@ async def handle_warn_user( ) return + warnings = await moderation.get_all_warnings( + self.bot, target, interaction.guild + ) + await modlog.log_action( bot=self.bot, action_type="warn", guild=interaction.guild, member=target, moderator=interaction.user, + channel=interaction.channel, reason=reason, + data=f"**Total warnings:** {len(warnings)}", ) warning_count = len( await moderation.get_all_warnings(self.bot, target, interaction.guild) @@ -516,6 +527,7 @@ async def handle_warn_user( guild=interaction.guild, member=target, moderator=interaction.user, + channel=interaction.channel, reason=ban_reason, ) else: @@ -595,20 +607,28 @@ async def handle_unwarn_user( result = await moderation.unwarn_user( bot_object=self.bot, user=target, warning=warning ) + + if not result: + embed = auxiliary.prepare_deny_embed( + message=f"Something went wrong when unwarning {target}" + ) + await interaction.response.send_message(embed=embed) + return + + warnings = await moderation.get_all_warnings( + self.bot, target, interaction.guild + ) + await modlog.log_action( bot=self.bot, action_type="unwarn", guild=interaction.guild, member=target, moderator=interaction.user, + channel=interaction.channel, reason=reason, + data=f"**Remaining warnings:** {len(warnings)}", ) - if not result: - embed = auxiliary.prepare_deny_embed( - message=f"Something went wrong when unwarning {target}" - ) - await interaction.response.send_message(embed=embed) - return embed = generate_response_embed(user=target, action="unwarn", reason=reason) await interaction.response.send_message(content=target.mention, embed=embed) @@ -702,6 +722,7 @@ async def handle_warning_clear( guild=interaction.guild, member=target, moderator=interaction.user, + channel=interaction.channel, reason=reason, data=f"**Total warnings:** {len(warnings)}", ) diff --git a/modules/moderation/modlog.py b/modules/moderation/modlog.py index 27683546..99339efc 100644 --- a/modules/moderation/modlog.py +++ b/modules/moderation/modlog.py @@ -377,6 +377,7 @@ async def on_automod_action(self: Self, execution: discord.AutoModAction) -> Non # I hate everything about this rule = await execution.fetch_rule() member = await rule.guild.fetch_member(execution.user_id) + channel = execution.guild.get_channel_or_thread(execution.channel_id) if any( action.type == discord.AutoModRuleActionType.timeout for action in rule.actions @@ -390,6 +391,7 @@ async def on_automod_action(self: Self, execution: discord.AutoModAction) -> Non action_type="timeout", guild=rule.guild, member=member, + channel=channel, reason=rule.name, expires_at=expires_at, data=f"**Violating content:** {execution.content[:200]}", @@ -408,6 +410,7 @@ async def on_automod_action(self: Self, execution: discord.AutoModAction) -> Non action_type="quarantine", guild=rule.guild, member=member, + channel=channel, reason=rule.name, data=f"**Violating name:** {member.display_name}", ) @@ -421,6 +424,7 @@ async def on_automod_action(self: Self, execution: discord.AutoModAction) -> Non action_type="automod block", guild=rule.guild, member=member, + channel=channel, reason=rule.name, data=f"**Violating content:** {execution.content[:200]}", ) @@ -430,6 +434,7 @@ async def on_automod_action(self: Self, execution: discord.AutoModAction) -> Non action_type="automod notice", guild=rule.guild, member=member, + channel=channel, reason=rule.name, data=f"**Violating content:** {execution.content[:200]}", ) @@ -509,12 +514,17 @@ async def generate_action_embed( ) description_strs = [] - # If this action has a member punished, display if + # Keep the recorded ID when an account can no longer be fetched. if action_entry.member_id: - member_account = await bot.fetch_user(int(action_entry.member_id)) - description_strs.append( - f"**Offender:** {member_account.name} {member_account.mention} ({member_account.id})" - ) + try: + member_account = await bot.fetch_user(int(action_entry.member_id)) + description_strs.append( + f"**Offender:** {member_account.name} {member_account.mention} ({member_account.id})" + ) + except (ValueError, discord.HTTPException, discord.InvalidData): + description_strs.append( + f"**Offender:** Unavailable user ({action_entry.member_id})" + ) # Always display the reason line, just if no reason explicilty display that if action_entry.reason: @@ -525,14 +535,42 @@ async def generate_action_embed( # If a moderator has been tied to this action, display that # If no moderator has been listed, this must be automod if action_entry.moderator_id: - moderator_account = await bot.fetch_user(int(action_entry.moderator_id)) - description_strs.append( - f"**Responsible moderator:** {moderator_account.name} " - f"{moderator_account.mention} ({moderator_account.id})" - ) + try: + moderator_account = await bot.fetch_user(int(action_entry.moderator_id)) + description_strs.append( + f"**Responsible moderator:** {moderator_account.name} " + f"{moderator_account.mention} ({moderator_account.id})" + ) + except (ValueError, discord.HTTPException, discord.InvalidData): + description_strs.append( + f"**Responsible moderator:** Unavailable user ({action_entry.moderator_id})" + ) else: description_strs.append("**Responsible moderator:** No associated moderator") + if action_entry.channel_id: + channel = None + try: + guild = bot.get_guild(int(action_entry.guild_id)) + channel_id = int(action_entry.channel_id) + if guild: + channel = guild.get_channel_or_thread(channel_id) + if channel is None: + channel = await guild.fetch_channel(channel_id) + except (ValueError, discord.HTTPException, discord.InvalidData): + pass + + if channel: + description_strs.append( + f"**Channel:** {channel.name} {channel.mention} ({channel.id})" + ) + else: + description_strs.append( + f"**Channel:** Unavailable channel ({action_entry.channel_id})" + ) + else: + description_strs.append("**Channel:** No associated channel") + # If this action has extra data, display it as is. if action_entry.data: # This might need special handling for specific events, we will see @@ -589,6 +627,7 @@ async def log_action( guild: discord.Guild, member: discord.abc.User | None = None, moderator: discord.abc.User | None = None, + channel: discord.abc.GuildChannel | None = None, reason: str = "", data: str = "", expires_at: datetime.datetime = None, @@ -601,6 +640,7 @@ async def log_action( guild (discord.Guild): The guild this action occured in member (discord.abc.User | None, optional): The member being punished, if applicable. moderator (discord.abc.User | None, optional): The moderator taking action, if applicable. + channel (discord.abc.GuildChannel | None, optional): The channel the action is assicoated with, if applicable. reason (str, optional): The reason this action was taken, if applicable. data (str, optional): Any extra data associated with this action, if applicable. expires_at (datetime.datetime, optional): When this action expires, if applicable. @@ -614,10 +654,13 @@ async def log_action( # If the action has a member or moderator, get the IDs member_id = None moderator_id = None + channel_id = None if member: member_id = str(member.id) if moderator: moderator_id = str(moderator.id) + if channel: + channel_id = str(channel.id) # This is done to prevent race conditions where two actions get the same case number # We can detect the unique error and just try again. If so, this means @@ -634,6 +677,7 @@ async def log_action( data=data, moderator_id=moderator_id, member_id=member_id, + channel_id=channel_id, until_time=expires_at, ) await entry.save() diff --git a/modules/moderation/notes.py b/modules/moderation/notes.py index f1c94232..0f3b3fbf 100644 --- a/modules/moderation/notes.py +++ b/modules/moderation/notes.py @@ -155,13 +155,17 @@ async def set_note( await note.save() + notes = await moderation.get_all_notes(self.bot, user, interaction.guild) + await modlog.log_action( bot=self.bot, action_type="note", guild=interaction.guild, + reason=body, member=user, moderator=interaction.user, - data=f"**Note:** {body}", + channel=interaction.channel, + data=f"**Total notes:** {len(notes)}", ) role = discord.utils.get( @@ -237,6 +241,7 @@ async def clear_notes( guild=interaction.guild, member=user, moderator=interaction.user, + channel=interaction.channel, data=f"**Total notes:** {len(notes)}", ) diff --git a/modules/moderation/purge.py b/modules/moderation/purge.py index 936babec..6f764484 100644 --- a/modules/moderation/purge.py +++ b/modules/moderation/purge.py @@ -82,6 +82,8 @@ async def purge_command( action_type="purge", guild=interaction.guild, moderator=interaction.user, + channel=interaction.channel, + data=f"**Messages deleted:** {len(deleted)}", ) await interaction.followup.edit_message( message_id=sent_message.id, diff --git a/modules/moderation/report.py b/modules/moderation/report.py index 9b0d42f2..0261b5b1 100644 --- a/modules/moderation/report.py +++ b/modules/moderation/report.py @@ -157,6 +157,7 @@ async def report_command( bot=self.bot, action_type="reported", guild=interaction.guild, + channel=interaction.channel, reason=reason, member=user, )