mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[modlog] Use new audit log event (#5970)
Co-authored-by: zephyrkul <zephyrkul@users.noreply.github.com>
This commit is contained in:
parent
ecccea6781
commit
76abb7cab2
@ -98,8 +98,11 @@ async def _init(bot: Red):
|
|||||||
await _migrate_config(from_version=await _config.schema_version(), to_version=_SCHEMA_VERSION)
|
await _migrate_config(from_version=await _config.schema_version(), to_version=_SCHEMA_VERSION)
|
||||||
await register_casetypes(all_generics)
|
await register_casetypes(all_generics)
|
||||||
|
|
||||||
async def on_member_ban(guild: discord.Guild, member: discord.Member):
|
async def on_audit_log_entry_create(entry: discord.AuditLogEntry):
|
||||||
if guild.unavailable or not guild.me.guild_permissions.view_audit_log:
|
guild = entry.guild
|
||||||
|
if guild.unavailable:
|
||||||
|
return
|
||||||
|
if entry.action not in (discord.AuditLogAction.ban, discord.AuditLogAction.unban):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -107,79 +110,14 @@ async def _init(bot: Red):
|
|||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
return # No modlog channel so no point in continuing
|
return # No modlog channel so no point in continuing
|
||||||
|
|
||||||
when = datetime.now(timezone.utc)
|
# Don't create modlog entires for the bot's own bans, cogs do this.
|
||||||
before = when + timedelta(minutes=1)
|
if entry.user_id == guild.me.id:
|
||||||
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:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
mod, reason, date = entry.user, entry.reason, entry.created_at
|
||||||
await get_modlog_channel(guild)
|
await create_case(_bot_ref, guild, date, entry.action.name, entry.target, mod, reason)
|
||||||
except RuntimeError:
|
|
||||||
return # No modlog channel so no point in continuing
|
|
||||||
|
|
||||||
when = datetime.now(timezone.utc)
|
bot.add_listener(on_audit_log_entry_create)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
async def handle_auditype_key():
|
async def handle_auditype_key():
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user