1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Administrator-py/administrator/check.py

73 lines
1.9 KiB
Python
Raw Normal View History

2021-02-03 14:46:22 +01:00
import functools
from discord import Permissions
from discord.ext import commands
2021-02-03 14:46:22 +01:00
from discord.ext.commands import NoPrivateMessage, NotOwner, MissingPermissions
import db
class ExtensionDisabled(commands.CheckFailure):
pass
def is_enabled():
2021-02-03 14:46:22 +01:00
def check(func):
@functools.wraps(func)
async def wrapped(*args):
ctx = args[1]
if ctx.guild:
s = db.Session()
es = s.query(db.ExtensionState).get((args[0].qualified_name, ctx.guild.id))
s.close()
if es and not es.state:
raise ExtensionDisabled()
return await func(*args)
return wrapped
return check
def is_owner():
def check(func):
@functools.wraps(func)
async def wrapped(*args):
ctx = args[1]
if not await ctx._discord.is_owner(ctx.author):
raise NotOwner('You do not own this bot.')
return await func(*args)
return wrapped
return check
def guild_only():
def check(func):
@functools.wraps(func)
async def wrapped(*args):
if args[1].guild is None:
raise NoPrivateMessage()
return await func(*args)
return wrapped
return check
def has_permissions(**perms):
invalid = set(perms) - set(Permissions.VALID_FLAGS)
if invalid:
raise TypeError('Invalid permission(s): %s' % (', '.join(invalid)))
def check(func):
@functools.wraps(func)
async def wrapped(*args):
ctx = args[1]
ch = ctx.channel
permissions = ch.permissions_for(ctx.author)
missing = [perm for perm, value in perms.items() if getattr(permissions, perm) != value]
if not missing:
return await func(*args)
raise MissingPermissions(missing)
return wrapped
return check