From 76abb7cab2c28bda3ad5e939b3d07250cdd99d7f Mon Sep 17 00:00:00 2001 From: Zephyrkul <23347632+Zephyrkul@users.noreply.github.com> Date: Tue, 26 Dec 2023 16:06:48 -0600 Subject: [PATCH] [modlog] Use new audit log event (#5970) Co-authored-by: zephyrkul --- redbot/core/modlog.py | 82 ++++++------------------------------------- 1 file changed, 10 insertions(+), 72 deletions(-) diff --git a/redbot/core/modlog.py b/redbot/core/modlog.py index c3f37a650cf..bd0a2fea8ae 100644 --- a/redbot/core/modlog.py +++ b/redbot/core/modlog.py @@ -98,8 +98,11 @@ async def _init(bot: Red): await _migrate_config(from_version=await _config.schema_version(), to_version=_SCHEMA_VERSION) await register_casetypes(all_generics) - async def on_member_ban(guild: discord.Guild, member: discord.Member): - if guild.unavailable or not guild.me.guild_permissions.view_audit_log: + async def on_audit_log_entry_create(entry: discord.AuditLogEntry): + guild = entry.guild + if guild.unavailable: + return + if entry.action not in (discord.AuditLogAction.ban, discord.AuditLogAction.unban): return try: @@ -107,79 +110,14 @@ async def on_member_ban(guild: discord.Guild, member: discord.Member): except RuntimeError: return # No modlog channel so no point in continuing - when = datetime.now(timezone.utc) - before = when + timedelta(minutes=1) - after = when - timedelta(minutes=1) - await asyncio.sleep(10) # prevent small delays from causing a 5 minute delay on entry - - attempts = 0 - # wait up to an hour to find a matching case - while attempts < 12 and guild.me.guild_permissions.view_audit_log: - attempts += 1 - try: - entry = await discord.utils.find( - lambda e: e.target.id == member.id and after < e.created_at < before, - guild.audit_logs( - action=discord.AuditLogAction.ban, before=before, after=after - ), - ) - except discord.Forbidden: - break - except discord.HTTPException: - pass - else: - if entry: - if entry.user.id != guild.me.id: - # Don't create modlog entires for the bot's own bans, cogs do this. - mod, reason = entry.user, entry.reason - date = entry.created_at - await create_case(_bot_ref, guild, date, "ban", member, mod, reason) - return - - await asyncio.sleep(300) - - async def on_member_unban(guild: discord.Guild, user: discord.User): - if guild.unavailable or not guild.me.guild_permissions.view_audit_log: + # Don't create modlog entires for the bot's own bans, cogs do this. + if entry.user_id == guild.me.id: return - try: - await get_modlog_channel(guild) - except RuntimeError: - return # No modlog channel so no point in continuing + mod, reason, date = entry.user, entry.reason, entry.created_at + await create_case(_bot_ref, guild, date, entry.action.name, entry.target, mod, reason) - when = datetime.now(timezone.utc) - before = when + timedelta(minutes=1) - after = when - timedelta(minutes=1) - await asyncio.sleep(10) # prevent small delays from causing a 5 minute delay on entry - - attempts = 0 - # wait up to an hour to find a matching case - while attempts < 12 and guild.me.guild_permissions.view_audit_log: - attempts += 1 - try: - entry = await discord.utils.find( - lambda e: e.target.id == user.id and after < e.created_at < before, - guild.audit_logs( - action=discord.AuditLogAction.unban, before=before, after=after - ), - ) - except discord.Forbidden: - break - except discord.HTTPException: - pass - else: - if entry: - if entry.user.id != guild.me.id: - # Don't create modlog entires for the bot's own unbans, cogs do this. - mod, reason = entry.user, entry.reason - date = entry.created_at - await create_case(_bot_ref, guild, date, "unban", user, mod, reason) - return - - await asyncio.sleep(300) - - bot.add_listener(on_member_ban) - bot.add_listener(on_member_unban) + bot.add_listener(on_audit_log_entry_create) async def handle_auditype_key():