Made if statements cleaner (imo)

Tried to convert unban command into using def
pull/8/head
sgoudham 4 years ago
parent 189f75b3e1
commit 6d550d9c60

@ -12,7 +12,7 @@ from db import connection
from settings import enso_embedmod_colours, get_modlog_for_guild, storeRoles, clearRoles from settings import enso_embedmod_colours, get_modlog_for_guild, storeRoles, clearRoles
async def check(ctx, members, action): async def check(ctx, members):
""" """
Check Function Check Function
@ -30,7 +30,7 @@ async def check(ctx, members, action):
return True return True
elif ctx.author in members: elif ctx.author in members:
embed = Embed(description=f"**❌ You Can't {action} Yourself Baka! ❌**", embed = Embed(description=f"**❌ Forbidden Action ❌**",
colour=enso_embedmod_colours) colour=enso_embedmod_colours)
await ctx.send(embed=embed) await ctx.send(embed=embed)
return True return True
@ -65,8 +65,10 @@ async def ummute_members(message, targets, reason):
result = await cur.fetchone() result = await cur.fetchone()
role_ids = result[4] role_ids = result[4]
# Get all the roles of the user before they were muted from the database
roles = [message.guild.get_role(int(id_)) for id_ in role_ids.split(", ") if len(id_)] roles = [message.guild.get_role(int(id_)) for id_ in role_ids.split(", ") if len(id_)]
# Clear all the roles of the user
await clearRoles(ctx=message, member=target, pool=pool) await clearRoles(ctx=message, member=target, pool=pool)
await target.edit(roles=roles) await target.edit(roles=roles)
@ -205,6 +207,59 @@ async def ban_members(message, targets, reason):
await message.channel.send(embed=embed) await message.channel.send(embed=embed)
async def unban_members(self, message, targets, reason):
"""
Method to allow members to be unbanned
2 embeds will be sent, one to the channel that the user is in
And if the user has the modlogs channel setup, an embed will be logged there
"""
# Get the list of banned users from the server
bans = await message.guild.bans()
ban_ids = list(map(lambda m: m.user.id, bans))
for target in targets:
if target not in ban_ids:
embed = Embed(description="❌ **Member Is Not In Unban's List!** ❌",
colour=enso_embedmod_colours)
await message.channel.send(embed=embed)
else:
# Get the member and unban them
user = await self.bot.fetch_user(target)
await message.guild.unban(user, reason=reason)
# Send confirmation to the channel that the user is in
embed = Embed(description="✅ **{}** Was Unbanned! ✅".format(user),
colour=enso_embedmod_colours)
await message.channel.send(embed=embed)
# Get the channel of the modlog within the guild
modlog = get_modlog_for_guild(str(message.guild.id))
if modlog is None:
pass
else:
channel = message.guild.get_channel(int(modlog))
embed = Embed(title="Member Unbanned",
colour=enso_embedmod_colours,
timestamp=datetime.datetime.utcnow())
embed.set_thumbnail(url=user.avatar_url)
fields = [("Member", user, False),
("Actioned by", message.author.mention, False),
("Reason", reason, False)]
for name, value, inline in fields:
embed.add_field(name=name, value=value, inline=inline)
await channel.send(embed=embed)
async def kick_members(message, targets, reason): async def kick_members(message, targets, reason):
""" """
@ -274,12 +329,10 @@ class Moderation(Cog):
Multiple Members can be Kicked at Once Multiple Members can be Kicked at Once
""" """
if await check(ctx, members, action="Kick"): if not await check(ctx, members):
return with ctx.typing():
# Send embed of the kicked member
with ctx.typing(): await kick_members(ctx.message, members, reason)
# Send embed of the kicked member
await kick_members(ctx.message, members, reason)
@command(name="mute", aliases=["Mute"]) @command(name="mute", aliases=["Mute"])
@has_guild_permissions(manage_roles=True) @has_guild_permissions(manage_roles=True)
@ -290,23 +343,21 @@ class Moderation(Cog):
Multiple Members can be Muted At Once Multiple Members can be Muted At Once
""" """
if await check(ctx, members, action="Mute"): if not await check(ctx, members):
return with ctx.typing():
role = discord.utils.get(ctx.guild.roles, name="Muted")
with ctx.typing(): if role is None:
role = discord.utils.get(ctx.guild.roles, name="Muted") # Setting up the role permissions for the Muted Role
if role is None: muted = await ctx.guild.create_role(name="Muted")
# Setting up the role permissions for the Muted Role # Removes permission to send messages in all channels
muted = await ctx.guild.create_role(name="Muted") for channel in ctx.guild.channels:
# Removes permission to send messages in all channels await channel.set_permissions(muted, send_messages=False, read_messages=True)
for channel in ctx.guild.channels:
await channel.set_permissions(muted, send_messages=False, read_messages=True) # Send embed of the kicked member
await mute_members(ctx.message, members, reason, muted)
# Send embed of the kicked member else:
await mute_members(ctx.message, members, reason, muted) # Send embed of the kicked member
else: await mute_members(ctx.message, members, reason, role)
# Send embed of the kicked member
await mute_members(ctx.message, members, reason, role)
@command(name="unmute", aliases=["Unmute"]) @command(name="unmute", aliases=["Unmute"])
@has_guild_permissions(manage_roles=True) @has_guild_permissions(manage_roles=True)
@ -318,25 +369,23 @@ class Moderation(Cog):
""" """
unmute = False unmute = False
if await check(ctx, members, action="Unmute"): if not await check(ctx, members):
return with ctx.typing():
role = discord.utils.get(ctx.guild.roles, name="Muted")
with ctx.typing(): if role is None:
role = discord.utils.get(ctx.guild.roles, name="Muted") embed = Embed(description="**❌ No Muted Role Was Found! ❌**",
if role is None: colour=enso_embedmod_colours)
embed = Embed(description="**❌ No Muted Role Was Found! ❌**", await ctx.send(embed=embed)
colour=enso_embedmod_colours) else:
await ctx.send(embed=embed) await ctx.message.delete()
else: for member in members:
await ctx.message.delete() if role in member.roles:
for member in members: await ummute_members(ctx.message, members, reason)
if role in member.roles: unmute = True
await ummute_members(ctx.message, members, reason) if role not in member.roles and unmute is False:
unmute = True embed = Embed(description=f"**❌ {member.mention} Is Not Muted! ❌**",
if role not in member.roles and unmute is False: colour=enso_embedmod_colours)
embed = Embed(description=f"**❌ {member.mention} Is Not Muted! ❌**", await ctx.send(embed=embed)
colour=enso_embedmod_colours)
await ctx.send(embed=embed)
@command(name="ban", aliases=["Ban"], usage="`<member>...` `[reason]`") @command(name="ban", aliases=["Ban"], usage="`<member>...` `[reason]`")
@guild_only() @guild_only()
@ -349,12 +398,10 @@ class Moderation(Cog):
Multiple Members can be banned at once Multiple Members can be banned at once
""" """
if await check(ctx, members, action="Ban"): if await check(ctx, members):
return with ctx.typing():
# Send embed of the Banned member
with ctx.typing(): await ban_members(ctx.message, members, reason)
# Send embed of the Banned member
await ban_members(ctx.message, members, reason)
@command(name="unban", aliases=["Unban"], usage="`<member>...` `[reason]`") @command(name="unban", aliases=["Unban"], usage="`<member>...` `[reason]`")
@guild_only() @guild_only()
@ -366,49 +413,10 @@ class Moderation(Cog):
Unban Member(s) from Server Unban Member(s) from Server
Multiple Members can be Unbanned At Once Multiple Members can be Unbanned At Once
""" """
if not check(ctx, members):
# Get the list of banned users from the server with ctx.typing():
bans = await ctx.guild.bans()
ban_ids = list(map(lambda m: m.user.id, bans))
for member in members:
if member not in ban_ids:
embed = Embed(description="❌ **Member Is Not In Unban's List!** ❌",
colour=enso_embedmod_colours)
await ctx.send(embed=embed)
else:
# Get the member and unban them
user = await self.bot.fetch_user(member)
await ctx.guild.unban(user, reason=reason)
await ctx.message.delete() await ctx.message.delete()
# Send confirmation to the channel that the user is in await unban_members(self, ctx.message, members, reason)
embed = Embed(description="✅ **{}** Was Unbanned! ✅".format(user),
colour=enso_embedmod_colours)
await ctx.send(embed=embed)
# Get the channel of the modlog within the guild
modlog = get_modlog_for_guild(str(ctx.guild.id))
if modlog is None:
pass
else:
channel = ctx.guild.get_channel(int(modlog))
embed = Embed(title="Member Unbanned",
colour=enso_embedmod_colours,
timestamp=datetime.datetime.utcnow())
embed.set_thumbnail(url=user.avatar_url)
fields = [("Member", user, False),
("Actioned by", ctx.author.mention, False),
("Reason", reason, False)]
for name, value, inline in fields:
embed.add_field(name=name, value=value, inline=inline)
await channel.send(embed=embed)
@command(name="purge", aliases=["Purge"]) @command(name="purge", aliases=["Purge"])
@guild_only() @guild_only()

Loading…
Cancel
Save