diff options
author | 2024-02-18 21:48:25 +0100 | |
---|---|---|
committer | 2024-03-21 14:29:03 +0100 | |
commit | e04a70d746016e144a5a72b87cdaa4ac0d54b8fc (patch) | |
tree | 4894e66409175af79a63d40789b09417b6603a8c | |
parent | implement the handler for the BadArgument exception (diff) |
implement the handler for the CheckFailure exception
-rw-r--r-- | bot/command_error_handlers/__init__.py | 2 | ||||
-rw-r--r-- | bot/command_error_handlers/check_failure.py | 40 | ||||
-rw-r--r-- | bot/exts/core/error_handler.py | 21 |
3 files changed, 43 insertions, 20 deletions
diff --git a/bot/command_error_handlers/__init__.py b/bot/command_error_handlers/__init__.py index f1911050..cd6be708 100644 --- a/bot/command_error_handlers/__init__.py +++ b/bot/command_error_handlers/__init__.py @@ -4,6 +4,7 @@ from bot.bot import Bot from .api_error import APIErrorHandler from .bad_argument import BadArgumentErrorHandler +from .check_failure import CheckFailureErrorHandler from .command_not_found import CommandNotFoundErrorHandler from .command_on_cooldown import CommandOnCooldownErrorHandler from .default import DefaultCommandErrorHandler @@ -23,4 +24,5 @@ def bootstrap_command_error_manager(bot: Bot) -> CommandErrorManager: manager.register_handler(CommandOnCooldownErrorHandler()) manager.register_handler(UserNotPlayingErrorHandler()) manager.register_handler(BadArgumentErrorHandler()) + manager.register_handler(CheckFailureErrorHandler()) return manager diff --git a/bot/command_error_handlers/check_failure.py b/bot/command_error_handlers/check_failure.py new file mode 100644 index 00000000..0ab6c83f --- /dev/null +++ b/bot/command_error_handlers/check_failure.py @@ -0,0 +1,40 @@ +from typing import NoReturn + +from discord import Embed, Interaction +from discord.ext.commands import CheckFailure, Context, NoPrivateMessage +from pydis_core.utils.error_handling.commands import AbstractCommandErrorHandler + +from bot.constants import Channels, NEGATIVE_REPLIES +from bot.utils.decorators import InChannelCheckFailure, InMonthCheckFailure + +from ._utils import create_error_embed + + +class CheckFailureErrorHandler(AbstractCommandErrorHandler): + """An handler for the CheckFailure 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, CheckFailure) + + async def handle_text_command_error(self, context: Context, error: Exception) -> NoReturn: + """Handle error raised in the context of text commands.""" + error_embed = self._get_error_embed(error) + await context.send(embed=error_embed, delete_after=7.5) + return + + async def handle_app_command_error(self, interaction: Interaction, error: Exception) -> NoReturn: + """Handle error raised in the context of app commands.""" + await interaction.response.send_message(embed=self._get_error_embed(error)) + + @staticmethod + def _get_error_embed(error: Exception) -> Embed: + if isinstance(error, InChannelCheckFailure | InMonthCheckFailure): + return create_error_embed(str(error), NEGATIVE_REPLIES) + if isinstance(error, NoPrivateMessage): + return create_error_embed( + "This command can only be used in the server. " + f"Go to <#{Channels.sir_lancebot_playground}> instead!", + NEGATIVE_REPLIES + ) + return create_error_embed("You are not authorized to use this command.", NEGATIVE_REPLIES) diff --git a/bot/exts/core/error_handler.py b/bot/exts/core/error_handler.py index 6b725f04..d8e2380e 100644 --- a/bot/exts/core/error_handler.py +++ b/bot/exts/core/error_handler.py @@ -6,8 +6,7 @@ from discord.ext import commands from pydis_core.utils.logging import get_logger from bot.bot import Bot -from bot.constants import Channels, Colours, ERROR_REPLIES, NEGATIVE_REPLIES -from bot.utils.decorators import InChannelCheckFailure, InMonthCheckFailure +from bot.constants import Colours, ERROR_REPLIES, NEGATIVE_REPLIES log = get_logger(__name__) @@ -56,28 +55,10 @@ class CommandErrorHandler(commands.Cog): f"Channel: {ctx.channel}" ) - if isinstance(error, InChannelCheckFailure | InMonthCheckFailure): - await ctx.send(embed=self.error_embed(str(error), NEGATIVE_REPLIES), 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 - if isinstance(error, commands.NoPrivateMessage): - await ctx.send( - embed=self.error_embed( - "This command can only be used in the server. " - f"Go to <#{Channels.sir_lancebot_playground}> instead!", - NEGATIVE_REPLIES - ) - ) - return - - if isinstance(error, commands.CheckFailure): - await ctx.send(embed=self.error_embed("You are not authorized to use this command.", NEGATIVE_REPLIES)) - return - await self.bot.command_error_manager.handle_error(error, ctx) |