tuxbot-bot/tuxbot/core/checks.py

90 lines
1.9 KiB
Python
Raw Permalink Normal View History

from typing import Dict
2020-06-05 00:29:14 +02:00
import discord
from discord.ext import commands
from discord.ext.commands import (
bot_has_permissions,
has_permissions,
is_owner,
)
from tuxbot.core.utils.functions.extra import ContextPlus
__all__ = [
"bot_has_permissions",
"has_permissions",
"is_owner",
"is_mod",
"is_admin",
"check_permissions",
"guild_owner_or_permissions",
]
def is_mod():
"""Is the user a moderator ?"""
2020-06-06 18:51:47 +02:00
2020-06-05 00:29:14 +02:00
async def pred(ctx):
if await ctx.bot.is_owner(ctx.author):
return True
permissions: discord.Permissions = ctx.channel.permissions_for(
ctx.author
)
2020-06-05 00:29:14 +02:00
return permissions.manage_messages
return commands.check(pred)
def is_admin():
"""Is the user admin ?"""
2020-06-06 18:51:47 +02:00
2020-06-05 00:29:14 +02:00
async def pred(ctx):
if await ctx.bot.is_owner(ctx.author):
return True
permissions: discord.Permissions = ctx.channel.permissions_for(
ctx.author
)
2020-06-05 00:29:14 +02:00
return permissions.administrator
return commands.check(pred)
async def check_permissions(ctx: ContextPlus, **perms: Dict[str, bool]):
2020-06-06 02:00:16 +02:00
"""Does a user have any perms ?
Parameters
----------
ctx:ContextPlus
Command context.
**perms:dict
Perms to verify.
"""
2020-06-05 00:29:14 +02:00
if await ctx.bot.is_owner(ctx.author):
return True
if not perms:
2020-06-05 00:29:14 +02:00
return False
2020-06-05 00:29:14 +02:00
resolved = ctx.channel.permissions_for(ctx.author)
return all(
getattr(resolved, name, None) == value for name, value in perms.items()
)
2020-06-05 00:29:14 +02:00
def guild_owner_or_permissions(**perms: Dict[str, bool]):
2020-06-06 02:00:16 +02:00
"""Is a user the guild's owner or does this user have any perms ?
Parameters
----------
**perms:dict
Perms to verify.
"""
2020-06-06 18:51:47 +02:00
2020-06-05 00:29:14 +02:00
async def pred(ctx):
if ctx.author is ctx.guild.owner:
return True
return await check_permissions(ctx, **perms)
return commands.check(pred)