From fef08b1dbfd90d7f9f4151200aa2a63b51009c98 Mon Sep 17 00:00:00 2001 From: Ave Ozkal Date: Wed, 26 Dec 2018 10:55:42 +0300 Subject: [PATCH] Collect checks into a single file to reduce code repetition --- cogs/admin.py | 14 ++------------ cogs/checks.py | 21 +++++++++++++++++++++ cogs/lockdown.py | 6 +----- cogs/logs.py | 3 --- cogs/meme.py | 9 +-------- cogs/mod.py | 8 +------- cogs/verification.py | 7 +------ 7 files changed, 27 insertions(+), 41 deletions(-) create mode 100644 cogs/checks.py diff --git a/cogs/admin.py b/cogs/admin.py index 2f6b16e..6eded7e 100644 --- a/cogs/admin.py +++ b/cogs/admin.py @@ -3,7 +3,7 @@ from discord.ext import commands import traceback import inspect import re -import config +from cogs.checks import check_if_bot_manager class AdminCog: @@ -12,22 +12,12 @@ class AdminCog: self.last_eval_result = None self.previous_eval_code = None - def check_if_staff(ctx): - if not ctx.guild: - return False - return any(r.id in config.staff_role_ids for r in ctx.author.roles) - - def check_if_bot_manager(ctx): - if not ctx.guild: - return False - return any(r.id == config.bot_manager_role_id for r in ctx.author.roles) - @commands.guild_only() @commands.check(check_if_bot_manager) @commands.command(name='exit', hidden=True) async def _exit(self, ctx): """Shuts down the bot, bot manager only.""" - await ctx.send(":wave: Exiting bot, goodbye!") + await ctx.send(":wave: Goodbye!") await self.bot.logout() @commands.guild_only() diff --git a/cogs/checks.py b/cogs/checks.py new file mode 100644 index 0000000..3bed73c --- /dev/null +++ b/cogs/checks.py @@ -0,0 +1,21 @@ +import config + + +def check_if_staff(ctx): + if not ctx.guild: + return False + return any(r.id in config.staff_role_ids for r in ctx.author.roles) + + +def check_if_bot_manager(ctx): + if not ctx.guild: + return False + return any(r.id == config.bot_manager_role_id for r in ctx.author.roles) + + +def check_if_staff_or_ot(ctx): + if not ctx.guild: + return True + is_ot = (ctx.channel.name == "off-topic") + is_staff = any(r.id in config.staff_role_ids for r in ctx.author.roles) + return (is_ot or is_staff) diff --git a/cogs/lockdown.py b/cogs/lockdown.py index 9ce5ce5..eb29c7c 100644 --- a/cogs/lockdown.py +++ b/cogs/lockdown.py @@ -1,17 +1,13 @@ from discord.ext import commands import config import discord +from cogs.checks import check_if_staff class Lockdown: def __init__(self, bot): self.bot = bot - def check_if_staff(ctx): - if not ctx.guild: - return False - return any(r.id in config.staff_role_ids for r in ctx.author.roles) - @commands.guild_only() @commands.check(check_if_staff) @commands.command() diff --git a/cogs/logs.py b/cogs/logs.py index 3c02916..be31b12 100644 --- a/cogs/logs.py +++ b/cogs/logs.py @@ -1,8 +1,5 @@ import discord import json -from discord.ext import commands -from sys import argv -from datetime import datetime import config diff --git a/cogs/meme.py b/cogs/meme.py index 9694aae..7c9f774 100644 --- a/cogs/meme.py +++ b/cogs/meme.py @@ -1,8 +1,8 @@ import random -import config import discord from discord.ext import commands import math +from cogs.checks import check_if_staff_or_ot class Meme: @@ -13,13 +13,6 @@ class Meme: def __init__(self, bot): self.bot = bot - def check_if_staff_or_ot(ctx): - if not ctx.guild: - return True - is_ot = (ctx.channel.name == "off-topic") - is_staff = any(r.id in config.staff_role_ids for r in ctx.author.roles) - return (is_ot or is_staff) - def c_to_f(self, c): """this is where we take memes too far""" return math.floor(9.0 / 5.0 * c + 32) diff --git a/cogs/mod.py b/cogs/mod.py index 852d72e..e30469f 100644 --- a/cogs/mod.py +++ b/cogs/mod.py @@ -3,20 +3,14 @@ from discord.ext import commands import config import json import time +from cogs.checks import check_if_staff class ModCog: def __init__(self, bot): self.bot = bot - def check_if_staff(ctx): - if not ctx.guild: - return False - return any(r.id in config.staff_role_ids for r in ctx.author.roles) - def check_if_target_is_staff(self, target): - if not ctx.guild: - return False return any(r.id in config.staff_role_ids for r in target.roles) async def add_restriction(self, member, rst): diff --git a/cogs/verification.py b/cogs/verification.py index da2200e..43096f7 100644 --- a/cogs/verification.py +++ b/cogs/verification.py @@ -4,8 +4,8 @@ import asyncio import config import random from inspect import cleandoc -import config import hashlib +from cogs.checks import check_if_staff welcome_header = """ @@ -103,11 +103,6 @@ class Verification: def __init__(self, bot): self.bot = bot - def check_if_staff(ctx): - if not ctx.guild: - return False - return any(r.id in config.staff_role_ids for r in ctx.author.roles) - @commands.check(check_if_staff) @commands.command() async def reset(self, ctx, limit: int = 100, force: bool = False):