From 807dd882af72b6b996ec34c997bfdfb1111423ae Mon Sep 17 00:00:00 2001 From: Ave Ozkal Date: Thu, 21 Feb 2019 01:20:03 +0300 Subject: [PATCH] lockdown: reduce code repetition --- cogs/lockdown.py | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/cogs/lockdown.py b/cogs/lockdown.py index 2043c94..a59e8bd 100644 --- a/cogs/lockdown.py +++ b/cogs/lockdown.py @@ -8,17 +8,21 @@ class Lockdown: def __init__(self, bot): self.bot = bot + async def set_sendmessgae(self, channel: discord.TextChannel, + role, allow_send, issuer): + try: + roleobj = channel.guild.get_role(role) + overrides = channel.overwrites_for(roleobj) + overrides.send_messages = allow_send + await channel.set_permissions(roleobj, + overwrite=overrides, + reason=str(issuer)) + except: + pass + async def unlock_for_staff(self, channel: discord.TextChannel, issuer): for role in config.staff_role_ids: - try: - roleobj = channel.guild.get_role(role) - overrides = channel.overwrites_for(roleobj) - overrides.send_messages = True - await channel.set_permissions(roleobj, - overwrite=overrides, - reason=str(issuer)) - except: - pass + await self.set_sendmessage(channel, role, True, issuer) @commands.guild_only() @commands.check(check_if_staff) @@ -39,12 +43,7 @@ class Lockdown: roles = [config.named_roles["participant"]] for role in roles: - roleobj = channel.guild.get_role(role) - overrides = channel.overwrites_for(roleobj) - overrides.send_messages = False - await channel.set_permissions(roleobj, - overwrite=overrides, - reason=str(ctx.author)) + await self.set_sendmessage(channel, role, False, ctx.author) await self.unlock_for_staff(channel, ctx.author) @@ -78,12 +77,7 @@ class Lockdown: await self.unlock_for_staff(channel, ctx.author) for role in roles: - roleobj = channel.guild.get_role(role) - overrides = channel.overwrites_for(roleobj) - overrides.send_messages = True - await channel.set_permissions(roleobj, - overwrite=overrides, - reason=str(ctx.author)) + await self.set_sendmessage(channel, role, True, ctx.author) safe_name = await commands.clean_content().convert(ctx, str(ctx.author)) await ctx.send("🔓 Channel unlocked.")