Browse Source

Collect checks into a single file to reduce code repetition

main
Ave Ozkal 2 years ago
parent
commit
497ec3559a
7 changed files with 27 additions and 41 deletions
  1. +2
    -12
      cogs/admin.py
  2. +21
    -0
      cogs/checks.py
  3. +1
    -5
      cogs/lockdown.py
  4. +0
    -3
      cogs/logs.py
  5. +1
    -8
      cogs/meme.py
  6. +1
    -7
      cogs/mod.py
  7. +1
    -6
      cogs/verification.py

+ 2
- 12
cogs/admin.py 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
- 0
cogs/checks.py 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)

+ 1
- 5
cogs/lockdown.py 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()


+ 0
- 3
cogs/logs.py 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


+ 1
- 8
cogs/meme.py 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)


+ 1
- 7
cogs/mod.py 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):


+ 1
- 6
cogs/verification.py 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):


Loading…
Cancel
Save