diff options
author | 2020-02-22 20:23:43 +0100 | |
---|---|---|
committer | 2020-02-22 20:23:43 +0100 | |
commit | 428e91667f2f4b87cb1dfc811b44787d0c182e43 (patch) | |
tree | b4a5c40ccaa3a176864b90ece0f1a9e29e764b34 /bot/decorators.py | |
parent | Add IntEnum for the 12 months (diff) |
Implement in_month command check
Commands decorated with in_month can only be used in one of the allowed
months.
Diffstat (limited to 'bot/decorators.py')
-rw-r--r-- | bot/decorators.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/bot/decorators.py b/bot/decorators.py index d0371df4..8a1f00ee 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -2,6 +2,7 @@ import logging import random import typing from asyncio import Lock +from datetime import datetime from functools import wraps from weakref import WeakValueDictionary @@ -9,7 +10,7 @@ from discord import Colour, Embed from discord.ext import commands from discord.ext.commands import CheckFailure, Context -from bot.constants import ERROR_REPLIES +from bot.constants import ERROR_REPLIES, Month log = logging.getLogger(__name__) @@ -20,6 +21,24 @@ class InChannelCheckFailure(CheckFailure): pass +def in_month(*allowed_months: Month) -> typing.Callable: + """ + Check whether the command was invoked in one of `enabled_months`. + + Uses the current UTC month at the time of running the predicate. + """ + async def predicate(ctx: Context) -> bool: + current_month = datetime.utcnow().month + can_run = current_month in allowed_months + + log.debug( + f"Command '{ctx.command}' is locked to months {allowed_months}. " + f"Invoking it in month {current_month} is {'allowed' if can_run else 'disallowed'}." + ) + return can_run + return commands.check(predicate) + + def with_role(*role_ids: int) -> typing.Callable: """Check to see whether the invoking user has any of the roles specified in role_ids.""" async def predicate(ctx: Context) -> bool: |