Collect checks into a single file to reduce code repetition

This commit is contained in:
Ave Ozkal 2018-12-26 10:55:42 +03:00
parent b35765d296
commit fef08b1dbf
No known key found for this signature in database
GPG key ID: 09356ABAA42C842B
7 changed files with 27 additions and 41 deletions

View file

@ -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()

21
cogs/checks.py Normal file
View file

@ -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)

View file

@ -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()

View file

@ -1,8 +1,5 @@
import discord
import json
from discord.ext import commands
from sys import argv
from datetime import datetime
import config

View file

@ -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)

View file

@ -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):

View file

@ -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):