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 traceback
import inspect import inspect
import re import re
import config from cogs.checks import check_if_bot_manager
class AdminCog: class AdminCog:
@ -12,22 +12,12 @@ class AdminCog:
self.last_eval_result = None self.last_eval_result = None
self.previous_eval_code = 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.guild_only()
@commands.check(check_if_bot_manager) @commands.check(check_if_bot_manager)
@commands.command(name='exit', hidden=True) @commands.command(name='exit', hidden=True)
async def _exit(self, ctx): async def _exit(self, ctx):
"""Shuts down the bot, bot manager only.""" """Shuts down the bot, bot manager only."""
await ctx.send(":wave: Exiting bot, goodbye!") await ctx.send(":wave: Goodbye!")
await self.bot.logout() await self.bot.logout()
@commands.guild_only() @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 from discord.ext import commands
import config import config
import discord import discord
from cogs.checks import check_if_staff
class Lockdown: class Lockdown:
def __init__(self, bot): def __init__(self, bot):
self.bot = 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.guild_only()
@commands.check(check_if_staff) @commands.check(check_if_staff)
@commands.command() @commands.command()

View file

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

View file

@ -1,8 +1,8 @@
import random import random
import config
import discord import discord
from discord.ext import commands from discord.ext import commands
import math import math
from cogs.checks import check_if_staff_or_ot
class Meme: class Meme:
@ -13,13 +13,6 @@ class Meme:
def __init__(self, bot): def __init__(self, bot):
self.bot = 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): def c_to_f(self, c):
"""this is where we take memes too far""" """this is where we take memes too far"""
return math.floor(9.0 / 5.0 * c + 32) return math.floor(9.0 / 5.0 * c + 32)

View file

@ -3,20 +3,14 @@ from discord.ext import commands
import config import config
import json import json
import time import time
from cogs.checks import check_if_staff
class ModCog: class ModCog:
def __init__(self, bot): def __init__(self, bot):
self.bot = 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): 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) return any(r.id in config.staff_role_ids for r in target.roles)
async def add_restriction(self, member, rst): async def add_restriction(self, member, rst):

View file

@ -4,8 +4,8 @@ import asyncio
import config import config
import random import random
from inspect import cleandoc from inspect import cleandoc
import config
import hashlib import hashlib
from cogs.checks import check_if_staff
welcome_header = """ welcome_header = """
@ -103,11 +103,6 @@ class Verification:
def __init__(self, bot): def __init__(self, bot):
self.bot = 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.check(check_if_staff)
@commands.command() @commands.command()
async def reset(self, ctx, limit: int = 100, force: bool = False): async def reset(self, ctx, limit: int = 100, force: bool = False):