aboutsummaryrefslogtreecommitdiffstats
path: root/bot/decorators.py
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2020-03-15 14:32:12 +0100
committerGravatar kwzrd <[email protected]>2020-03-15 14:32:12 +0100
commit6e8b239f407bf079e35a03df41d55c042b026406 (patch)
treeedbd9d5f919a19af2a0c793902ed0b8f4f29bbfa /bot/decorators.py
parentDeseasonify: lock branding commands to moderation roles (diff)
Deseasonify: improve `in_month` command check
Raise a custom exception if the command fails. This is then handled in the error handler, and the user will be informed of which months allow the invoked command.
Diffstat (limited to 'bot/decorators.py')
-rw-r--r--bot/decorators.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/bot/decorators.py b/bot/decorators.py
index 8a1f00ee..f031d404 100644
--- a/bot/decorators.py
+++ b/bot/decorators.py
@@ -21,6 +21,12 @@ class InChannelCheckFailure(CheckFailure):
pass
+class InMonthCheckFailure(CheckFailure):
+ """Check failure for when a command is invoked outside of its allowed month."""
+
+ pass
+
+
def in_month(*allowed_months: Month) -> typing.Callable:
"""
Check whether the command was invoked in one of `enabled_months`.
@@ -31,11 +37,16 @@ def in_month(*allowed_months: Month) -> typing.Callable:
current_month = datetime.utcnow().month
can_run = current_month in allowed_months
+ human_months = ", ".join(m.name for m in allowed_months)
log.debug(
- f"Command '{ctx.command}' is locked to months {allowed_months}. "
+ f"Command '{ctx.command}' is locked to months {human_months}. "
f"Invoking it in month {current_month} is {'allowed' if can_run else 'disallowed'}."
)
- return can_run
+ if can_run:
+ return True
+ else:
+ raise InMonthCheckFailure(f"Command can only be used in {human_months}")
+
return commands.check(predicate)