tuxbot-bot/tuxbot/core/utils/checks.py

101 lines
2.2 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
2021-04-21 18:28:09 +02:00
__all__ = (
2020-06-05 00:29:14 +02:00
"bot_has_permissions",
"has_permissions",
"is_owner",
"is_mod",
"is_admin",
"check_permissions",
"guild_owner_or_permissions",
2021-04-21 18:28:09 +02:00
)
2020-06-05 00:29:14 +02:00
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 ? as @check"""
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 is_user_admin(ctx):
"""Is the user Admin ? as function"""
if await ctx.bot.is_owner(ctx.author):
return True
permissions: discord.Permissions = ctx.channel.permissions_for(ctx.author)
return permissions.administrator
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)