aboutsummaryrefslogtreecommitdiffstats
path: root/bot/decorators.py
blob: b84b2c3602135d74a82edb1aab6b0dc15ec7b3f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import logging

from discord.ext import commands
from discord.ext.commands import Context

log = logging.getLogger(__name__)


def with_role(*role_ids: int):
    async def predicate(ctx: Context):
        if not ctx.guild:  # Return False in a DM
            log.debug(f"{ctx.author} tried to use the '{ctx.command.name}'command from a DM. "
                      "This command is restricted by the with_role decorator. Rejecting request.")
            return False

        for role in ctx.author.roles:
            if role.id in role_ids:
                log.debug(f"{ctx.author} has the '{role.name}' role, and passes the check.")
                return True

        log.debug(f"{ctx.author} does not have the required role to use "
                  f"the '{ctx.command.name}' command, so the request is rejected.")
        return False
    return commands.check(predicate)


def without_role(*role_ids: int):
    async def predicate(ctx: Context):
        if not ctx.guild:  # Return False in a DM
            log.debug(f"{ctx.author} tried to use the '{ctx.command.name}' command from a DM. "
                      "This command is restricted by the without_role decorator. Rejecting request.")
            return False

        author_roles = [role.id for role in ctx.author.roles]
        check = all(role not in author_roles for role in role_ids)
        log.debug(f"{ctx.author} tried to call the '{ctx.command.name}' command. "
                  f"The result of the without_role check was {check}.")
        return check
    return commands.check(predicate)


def in_channel(channel_id):
    async def predicate(ctx: Context):
        check = ctx.channel.id == channel_id
        log.debug(f"{ctx.author} tried to call the '{ctx.command.name}' command. "
                  f"The result of the in_channel check was {check}.")
        return check
    return commands.check(predicate)