Skip to content

Commit

Permalink
Merge pull request #55 from tweirtx/master
Browse files Browse the repository at this point in the history
Made self-deafens unable to @everyone in modlogs
  • Loading branch information
tweirtx committed Dec 8, 2017
2 parents 603c27a + 9726912 commit 5e8451d
Showing 1 changed file with 34 additions and 84 deletions.
118 changes: 34 additions & 84 deletions dozer/cogs/moderation.py
@@ -1,7 +1,8 @@
import asyncio, discord, re
import asyncio, discord, functools, re
from discord.ext.commands import BadArgument, has_permissions, bot_has_permissions, RoleConverter
from .. import db
from ._utils import *
from ..utils import clean


class SafeRoleConverter(RoleConverter):
Expand All @@ -15,8 +16,19 @@ async def convert(self, ctx, arg):
raise


# Todo: timed/self mutes
class Moderation(Cog):
async def modlogger(self, ctx, action, target, reason):
modlogmessage = "{} has {} {} because {}".format(ctx.author, action, target, reason)
modlogmessage = clean(ctx=ctx, text=modlogmessage)
with db.Session() as session:
modlogchannel = session.query(Guildmodlog).filter_by(id=ctx.guild.id).one_or_none()
await ctx.send(modlogmessage)
if modlogchannel is not None:
channel = ctx.guild.get_channel(modlogchannel.modlog_channel)
await channel.send(modlogmessage)
else:
await ctx.send("Please configure modlog channel to enable modlog functionality")

async def permoverride(self, user, **overwrites):
for i in user.guild.channels:
overwrite = i.overwrites_for(user)
Expand Down Expand Up @@ -52,52 +64,24 @@ async def punishmenttimer(self, ctx, timing, target, lookup):
@bot_has_permissions(ban_members=True)
async def ban(self, ctx, user_mentions: discord.User, *, reason="No reason provided"):
"Bans the user mentioned."
usertoban = user_mentions
howtounban = "When it's time to unban, here's the ID to unban: <@{} >".format(usertoban.id)
modlogmessage = "{} has been banned by {} because {}. {}".format(usertoban, ctx.author.mention, reason, howtounban)
await ctx.guild.ban(usertoban, reason=reason)
await ctx.send(modlogmessage)
with db.Session() as session:
modlogchannel = session.query(Guildmodlog).filter_by(id=ctx.guild.id).one_or_none()
if modlogchannel is not None:
channel = ctx.guild.get_channel(modlogchannel.modlog_channel)
await channel.send(modlogmessage)
else:
await ctx.send("Please configure modlog channel to enable modlog functionality")
await ctx.guild.ban(user_mentions, reason=reason)
await self.modlogger(ctx=ctx, action="banned", target=user_mentions, reason=reason)

@command()
@has_permissions(ban_members=True)
@bot_has_permissions(ban_members=True)
async def unban(self, ctx, user_mentions: discord.User, *, reason="No reason provided"):
"Unbans the user ID mentioned."
usertoban = user_mentions
await ctx.guild.unban(usertoban, reason=reason)
modlogmessage = "{} has been unbanned by {} because {}".format(usertoban, ctx.author.mention, reason)
await ctx.send(modlogmessage)
with db.Session() as session:
modlogchannel = session.query(Guildmodlog).filter_by(id=ctx.guild.id).one_or_none()
if modlogchannel is not None:
channel = ctx.guild.get_channel(modlogchannel.modlog_channel)
await channel.send(modlogmessage)
else:
await ctx.send("Please configure modlog channel to enable modlog functionality")
await ctx.guild.unban(user_mentions, reason=reason)
await self.modlogger(ctx=ctx, action="unbanned", target=user_mentions, reason=reason)

@command()
@has_permissions(kick_members=True)
@bot_has_permissions(kick_members=True)
async def kick(self, ctx, user_mentions: discord.User, *, reason="No reason provided"):
"Kicks the user mentioned."
usertokick = user_mentions
await ctx.guild.kick(usertokick, reason=reason)
modlogmessage = "{} has been kicked by {} because {}".format(usertokick, ctx.author.mention, reason)
await ctx.send(modlogmessage)
with db.Session() as session:
modlogchannel = session.query(Guildmodlog).filter_by(id=ctx.guild.id).one_or_none()
if modlogchannel is not None:
channel = ctx.guild.get_channel(modlogchannel.modlog_channel)
await channel.send(modlogmessage)
else:
await ctx.send("Please configure modlog channel to enable modlog functionality")
await ctx.guild.kick(user_mentions, reason=reason)
await self.modlogger(ctx=ctx, action="kicked", target=user_mentions, reason=reason)

@command()
@has_permissions(administrator=True)
Expand Down Expand Up @@ -370,85 +354,56 @@ async def prune(self, ctx, num_to_delete: int):
@has_permissions(kick_members=True)
@bot_has_permissions(manage_roles=True)
async def mute(self, ctx, member_mentions: discord.Member, *, reason="No reason provided"):
await self.permoverride(member_mentions, send_messages=False, add_reactions=False)
modlogmessage = "{} has been muted by {} because {}".format(member_mentions, ctx.author.display_name, reason)
with db.Session() as session:
user = session.query(Guildmute).filter_by(id=member_mentions.id).one_or_none()
if user is not None:
await ctx.send("User is already muted!")
else:
user = Guildmute(id=member_mentions.id, guild=ctx.guild.id)
session.add(user)
modlogchannel = session.query(Guildmodlog).filter_by(id=ctx.guild.id).one_or_none()
await ctx.send(modlogmessage)
if modlogchannel is not None:
channel = ctx.guild.get_channel(modlogchannel.modlog_channel)
await channel.send(modlogmessage)
else:
await ctx.send("Please configure modlog channel to enable modlog functionality")


await self.permoverride(member_mentions, send_messages=False, add_reactions=False)
await self.modlogger(ctx=ctx, action="muted", target=member_mentions, reason=reason)

@command()
@has_permissions(kick_members=True)
@bot_has_permissions(manage_roles=True)
async def unmute(self, ctx, member_mentions: discord.Member):
await self.permoverride(member_mentions, send_messages=None, add_reactions=None)
modlogmessage = "{} has been unmuted by {}".format(member_mentions, ctx.author.display_name)
async def unmute(self, ctx, member_mentions: discord.Member, reason="No reason provided"):
with db.Session() as session:
user = session.query(Guildmute).filter_by(id=member_mentions.id, guild=ctx.guild.id).one_or_none()
if user is not None:
session.delete(user)
await ctx.send(modlogmessage)
modlogchannel = session.query(Guildmodlog).filter_by(id=ctx.guild.id).one_or_none()
if modlogchannel is not None:
channel = ctx.guild.get_channel(modlogchannel.modlog_channel)
await channel.send(modlogmessage)
else:
await ctx.send("Please configure modlog channel to enable modlog functionality")
await self.permoverride(member_mentions, send_messages=None, add_reactions=None)
await self.modlogger(ctx=ctx, action="unmuted", target=member_mentions, reason=reason)
else:
await ctx.send("User is not muted!")


@command()
@has_permissions(kick_members=True)
@bot_has_permissions(manage_roles=True)
async def deafen(self, ctx, member_mentions: discord.Member, *, reason="No reason provided"):
await self.permoverride(member_mentions, read_messages=False)
modlogmessage = "{} has been deafened by {} because {}".format(member_mentions, ctx.author.display_name, reason)
with db.Session() as session:
user = session.query(Deafen).filter_by(id=member_mentions.id).one_or_none()
if user is not None:
await ctx.send("User is already deafened!")
else:
user = Deafen(id=member_mentions.id, guild=ctx.guild.id, self_inflicted=False)
session.add(user)
modlogchannel = session.query(Guildmodlog).filter_by(id=ctx.guild.id).one_or_none()
await ctx.send(modlogmessage)
if modlogchannel is not None:
channel = ctx.guild.get_channel(modlogchannel.modlog_channel)
await channel.send(modlogmessage)
else:
await ctx.send("Please configure modlog channel to enable modlog functionality")
await self.permoverride(member_mentions, read_messages=False)
await self.modlogger(ctx=ctx, action="deafened", target=member_mentions, reason=reason)

@command()
@bot_has_permissions(manage_roles=True)
async def selfdeafen(self, ctx, timing, *, reason="No reason provided"):
await ctx.send("Deafening {}...".format(ctx.author))
await self.permoverride(ctx.author, read_messages=False)
modlogmessage = "{} has deafened themselves because {}".format(ctx.author, reason)
with db.Session() as session:
user = session.query(Deafen).filter_by(id=ctx.author.id).one_or_none()
if user is not None:
await ctx.send("You are already deafened!")
else:
user = Deafen(id=ctx.author.id, guild=ctx.guild.id, self_inflicted=True)
session.add(user)
modlogchannel = session.query(Guildmodlog).filter_by(id=ctx.guild.id).one_or_none()
await ctx.send(modlogmessage)
if modlogchannel is not None:
channel = ctx.guild.get_channel(modlogchannel.modlog_channel)
await channel.send(modlogmessage)
await self.permoverride(user=ctx.author, read_messages=False)
await self.modlogger(ctx=ctx, action="deafened", target=ctx.author, reason=reason)
self.bot.loop.create_task(self.punishmenttimer(ctx, timing, ctx.author, lookup=Deafen))
selfdeafen.example_usage = """
``[prefix]selfdeafen time (1h5m, both optional) reason``: deafens you if you need to get work done
Expand All @@ -457,20 +412,15 @@ async def selfdeafen(self, ctx, timing, *, reason="No reason provided"):
@command()
@has_permissions(kick_members=True)
@bot_has_permissions(manage_roles=True)
async def undeafen(self, ctx, member_mentions: discord.Member):
await self.permoverride(member_mentions, read_messages=None)
modlogmessage = "{} has been undeafened by {}".format(member_mentions, ctx.author.display_name)
async def undeafen(self, ctx, member_mentions: discord.Member, reason="No reason provided"):
with db.Session() as session:
user = session.query(Deafen).filter_by(id=member_mentions.id, guild=ctx.guild.id).one_or_none()
if user is not None:
await self.permoverride(user=member_mentions, read_messages=None)
session.delete(user)
modlogchannel = session.query(Guildmodlog).filter_by(id=ctx.guild.id).one_or_none()
await ctx.send(modlogmessage)
if modlogchannel is not None:
channel = ctx.guild.get_channel(modlogchannel.modlog_channel)
await channel.send(modlogmessage)
else:
await ctx.send("Please configure modlog channel to enable modlog functionality")
if user.self_inflicted:
reason = "self deafen timer expired"
await self.modlogger(ctx=ctx, action="undeafened", target=member_mentions, reason=reason)
else:
await ctx.send("User is not deafened!")

Expand Down

0 comments on commit 5e8451d

Please sign in to comment.