diff options
| author | 2020-03-15 14:32:12 +0100 | |
|---|---|---|
| committer | 2020-03-15 14:32:12 +0100 | |
| commit | 6e8b239f407bf079e35a03df41d55c042b026406 (patch) | |
| tree | edbd9d5f919a19af2a0c793902ed0b8f4f29bbfa | |
| parent | Deseasonify: 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.
| -rw-r--r-- | bot/decorators.py | 15 | ||||
| -rw-r--r-- | bot/seasons/evergreen/error_handler.py | 4 | 
2 files changed, 15 insertions, 4 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) diff --git a/bot/seasons/evergreen/error_handler.py b/bot/seasons/evergreen/error_handler.py index 0d8bb0bb..ba6ca5ec 100644 --- a/bot/seasons/evergreen/error_handler.py +++ b/bot/seasons/evergreen/error_handler.py @@ -7,7 +7,7 @@ from discord import Embed, Message  from discord.ext import commands  from bot.constants import Colours, ERROR_REPLIES, NEGATIVE_REPLIES -from bot.decorators import InChannelCheckFailure +from bot.decorators import InChannelCheckFailure, InMonthCheckFailure  log = logging.getLogger(__name__) @@ -55,7 +55,7 @@ class CommandErrorHandler(commands.Cog):          if isinstance(error, commands.CommandNotFound):              return -        if isinstance(error, InChannelCheckFailure): +        if isinstance(error, (InChannelCheckFailure, InMonthCheckFailure)):              await ctx.send(embed=self.error_embed(str(error), NEGATIVE_REPLIES), delete_after=7.5)              return | 
