Skip to content

Commit

Permalink
Use audit logs to track manual (un)bans
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MattBSG committed Aug 13, 2019
1 parent 7d9c6cf commit 09fa983
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
45 changes: 45 additions & 0 deletions cogs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}>')
Expand All @@ -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}>')
Expand Down
1 change: 1 addition & 0 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

1 comment on commit 09fa983

@MattBSG
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.