[modlog] Use new audit log event (#5970)

Co-authored-by: zephyrkul <zephyrkul@users.noreply.github.com>
This commit is contained in:
Zephyrkul 2023-12-26 16:06:48 -06:00 committed by GitHub
parent ecccea6781
commit 76abb7cab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 _init(bot: Red):
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)
if entry.user_id == guild.me.id:
return
await asyncio.sleep(300)
mod, reason, date = entry.user, entry.reason, entry.created_at
await create_case(_bot_ref, guild, date, entry.action.name, entry.target, mod, reason)
async def on_member_unban(guild: discord.Guild, user: discord.User):
if guild.unavailable or not guild.me.guild_permissions.view_audit_log:
return
try:
await get_modlog_channel(guild)
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 == 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():