diff options
author | 2024-02-18 21:22:32 +0100 | |
---|---|---|
committer | 2024-03-21 14:29:02 +0100 | |
commit | 7ec113a292cb0bdcbe462c1d1c905ac907fa0b49 (patch) | |
tree | 6d2a0744254ccc7874b407f5fb6848bfbc1fa9c2 | |
parent | implement the handler for the UserInputError exception (diff) |
implement the handler for the CommandOnCooldown exception
-rw-r--r-- | bot/command_error_handlers/command_on_cooldown.py | 31 | ||||
-rw-r--r-- | bot/exts/core/error_handler.py | 10 |
2 files changed, 31 insertions, 10 deletions
diff --git a/bot/command_error_handlers/command_on_cooldown.py b/bot/command_error_handlers/command_on_cooldown.py new file mode 100644 index 00000000..37b6b302 --- /dev/null +++ b/bot/command_error_handlers/command_on_cooldown.py @@ -0,0 +1,31 @@ +import math +from typing import NoReturn + +from discord import Interaction +from discord.ext.commands import CommandOnCooldown, Context +from pydis_core.utils.error_handling.commands import AbstractCommandErrorHandler + +from bot.constants import NEGATIVE_REPLIES + +from ._utils import create_error_embed + + +class CommandOnCooldownErrorHandler(AbstractCommandErrorHandler): + """An handler for the CommandOnCooldown error.""" + + async def should_handle_error(self, error: Exception) -> bool: + """A predicate that determines whether the error should be handled or not.""" + return isinstance(error, CommandOnCooldown) + + async def handle_text_command_error(self, context: Context, error: Exception) -> NoReturn: + """Handle error raised in the context of text commands.""" + mins, secs = divmod(math.ceil(error.retry_after), 60) + embed = create_error_embed( + f"This command is on cooldown:\nPlease retry in {mins} minutes {secs} seconds.", + NEGATIVE_REPLIES + ) + await context.send(embed=embed, delete_after=7.5) + + async def handle_app_command_error(self, interaction: Interaction, error: Exception) -> NoReturn: + """Handle error raised in the context of app commands.""" + raise Exception from error diff --git a/bot/exts/core/error_handler.py b/bot/exts/core/error_handler.py index e9a9d768..d1b098ba 100644 --- a/bot/exts/core/error_handler.py +++ b/bot/exts/core/error_handler.py @@ -1,4 +1,3 @@ -import math import random from collections.abc import Iterable @@ -67,15 +66,6 @@ class CommandErrorHandler(commands.Cog): await ctx.send(embed=self.error_embed(str(error), NEGATIVE_REPLIES), delete_after=7.5) return - if isinstance(error, commands.CommandOnCooldown): - mins, secs = divmod(math.ceil(error.retry_after), 60) - embed = self.error_embed( - f"This command is on cooldown:\nPlease retry in {mins} minutes {secs} seconds.", - NEGATIVE_REPLIES - ) - await ctx.send(embed=embed, delete_after=7.5) - return - if isinstance(error, commands.DisabledCommand): await ctx.send(embed=self.error_embed("This command has been disabled.", NEGATIVE_REPLIES)) return |