2020-08-29 01:01:34 +02:00
|
|
|
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():
|
2020-06-06 02:00:16 +02:00
|
|
|
"""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
|
2020-08-29 01:01:34 +02:00
|
|
|
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():
|
2020-06-06 02:00:16 +02:00
|
|
|
"""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
|
2020-08-29 01:01:34 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
elif not perms:
|
|
|
|
return False
|
|
|
|
resolved = ctx.channel.permissions_for(ctx.author)
|
|
|
|
|
2020-08-29 01:01:34 +02:00
|
|
|
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)
|