Skip to content

Commit

Permalink
[modlog] Use new audit log event (#5970)
Browse files Browse the repository at this point in the history
Co-authored-by: zephyrkul <[email protected]>
  • Loading branch information
Zephyrkul and Zephyrkul authored Dec 26, 2023
1 parent ecccea6 commit 76abb7c
Showing 1 changed file with 10 additions and 72 deletions.
82 changes: 10 additions & 72 deletions redbot/core/modlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,88 +98,26 @@ 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:
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 == 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():
Expand Down

0 comments on commit 76abb7c

Please sign in to comment.