From 09fa983ea33ba856afc67eef3203b97eb13675a2 Mon Sep 17 00:00:00 2001 From: Matthew Cohen Date: Tue, 13 Aug 2019 14:03:40 -0400 Subject: [PATCH] Use audit logs to track manual (un)bans Adds the ability to track if a ban/unban were made manually through the discord client, and track the action normally as though it was performed with a command --- cogs/core.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ cogs/utility.py | 1 + 2 files changed, 46 insertions(+) diff --git a/cogs/core.py b/cogs/core.py index 5d541736..f2975d7f 100644 --- a/cogs/core.py +++ b/cogs/core.py @@ -138,6 +138,27 @@ async def on_member_ban(self, guild, user): # TODO: make all guild ID mentions t if guild.id != 238080556708003851: return + db = mclient.bowser.puns + if not db.find_one({'user': user.id, 'type': 'ban', 'active': True, 'timestamp': {'$gt': time.time() - 60}}): + # Manual ban + audited = False + async for entry in guild.audit_logs(action=discord.AuditLogAction.ban): + if entry.target == user: + audited = entry + break + + if audited: + reason = '-No reason specified-' if not audited.reason else audited.reason + await utils.issue_pun(audited.target.id, audited.user.id, 'ban', reason) + + embed = discord.Embed(description='User was manually banned through Discord', color=discord.Color(0xD0021B), timestamp=datetime.datetime.utcnow()) + embed.set_author(name=f'Ban | {audited.target}') + embed.add_field(name='User', value=audited.target.mention, inline=True) + embed.add_field(name='Moderator', value=audited.user.mention, inline=True) + embed.add_field(name='Reason', value=reason) + + await self.modLogs.send(embed=embed) + embed = discord.Embed(color=discord.Color(0xD0021B), timestamp=datetime.datetime.utcnow()) embed.set_author(name=f'{user} ({user.id})', icon_url=user.avatar_url) embed.add_field(name='Mention', value=f'<@{user.id}>') @@ -149,6 +170,30 @@ async def on_member_unban(self, guild, user): if guild.id != 238080556708003851: return + db = mclient.bowser.puns + if not db.find_one({'user': user.id, 'type': 'unban', 'timestamp': {'$gt': time.time() - 60}}): + # Manual unban + audited = False + async for entry in guild.audit_logs(action=discord.AuditLogAction.unban): + if entry.target == user: + audited = entry + break + + if audited: + reason = '-No reason specified-' if not audited.reason else audited.reason + await utils.issue_pun(audited.target.id, audited.user.id, 'unban', reason, active=False) + db.update_one({'user': audited.target.id, 'type': 'ban', 'active': True}, {'$set':{ + 'active': False + }}) + + embed = discord.Embed(description='User was manually unbanned through Discord', color=discord.Color(0x4A90E2), timestamp=datetime.datetime.utcnow()) + embed.set_author(name=f'Unban | {audited.target}') + embed.add_field(name='User', value=audited.target.mention, inline=True) + embed.add_field(name='Moderator', value=audited.user.mention, inline=True) + embed.add_field(name='Reason', value=reason) + + await self.modLogs.send(embed=embed) + embed = discord.Embed(color=discord.Color(0x88FF00), timestamp=datetime.datetime.utcnow()) embed.set_author(name=f'{user} ({user.id})', icon_url=user.avatar_url) embed.add_field(name='Mention', value=f'<@{user.id}>') diff --git a/cogs/utility.py b/cogs/utility.py index 49652a48..dda8cedf 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -207,6 +207,7 @@ def confirm_check(reaction, member): def message_filter(message): return True if not memberList or message.author.id in memberList else False + await ctx.message.delete() deleted = await ctx.channel.purge(limit=messages, check=message_filter, bulk=True) m = await ctx.send(f'{config.greenTick} Clean action complete')