From 74d0484abed4f756beaebeca278b22f491131a5e Mon Sep 17 00:00:00 2001 From: shtlrs Date: Sun, 18 Feb 2024 21:30:24 +0100 Subject: implement the handler for the ApiError exception --- bot/command_error_handlers/__init__.py | 6 ++++++ bot/command_error_handlers/api_error.py | 36 +++++++++++++++++++++++++++++++++ bot/exts/core/error_handler.py | 10 --------- 3 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 bot/command_error_handlers/api_error.py diff --git a/bot/command_error_handlers/__init__.py b/bot/command_error_handlers/__init__.py index 16d53373..afd30ec5 100644 --- a/bot/command_error_handlers/__init__.py +++ b/bot/command_error_handlers/__init__.py @@ -2,10 +2,13 @@ from pydis_core.utils.error_handling.commands import CommandErrorManager from bot.bot import Bot +from .api_error import APIErrorHandler from .command_not_found import CommandNotFoundErrorHandler +from .command_on_cooldown import CommandOnCooldownErrorHandler from .default import DefaultCommandErrorHandler from .moved_command import MovedCommandErrorHandler from .user_input_error import UserInputErrorHandler +from .user_not_playing import UserNotPlayingErrorHandler def bootstrap_command_error_manager(bot: Bot) -> CommandErrorManager: @@ -15,4 +18,7 @@ def bootstrap_command_error_manager(bot: Bot) -> CommandErrorManager: manager.register_handler(CommandNotFoundErrorHandler(bot)) manager.register_handler(MovedCommandErrorHandler()) manager.register_handler(UserInputErrorHandler()) + manager.register_handler(APIErrorHandler()) + manager.register_handler(CommandOnCooldownErrorHandler()) + manager.register_handler(UserNotPlayingErrorHandler()) return manager diff --git a/bot/command_error_handlers/api_error.py b/bot/command_error_handlers/api_error.py new file mode 100644 index 00000000..3c131896 --- /dev/null +++ b/bot/command_error_handlers/api_error.py @@ -0,0 +1,36 @@ +from typing import NoReturn + +from discord import Interaction +from discord.ext.commands import Context +from pydis_core.utils.error_handling.commands import AbstractCommandErrorHandler + +from bot.constants import NEGATIVE_REPLIES +from bot.utils.exceptions import APIError + +from ._utils import create_error_embed + + +class APIErrorHandler(AbstractCommandErrorHandler): + """An handler for the APIError 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, APIError) + + async def handle_text_command_error(self, context: Context, error: Exception) -> NoReturn: + """Handle error raised in the context of text commands.""" + await context.send( + embed=create_error_embed( + f"There was an error when communicating with the {error.api}", + NEGATIVE_REPLIES + ) + ) + + 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=create_error_embed( + f"There was an error when communicating with the {error.api}", + NEGATIVE_REPLIES + ) + ) diff --git a/bot/exts/core/error_handler.py b/bot/exts/core/error_handler.py index 17f9902c..d4330961 100644 --- a/bot/exts/core/error_handler.py +++ b/bot/exts/core/error_handler.py @@ -8,7 +8,6 @@ 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.utils.exceptions import APIError log = get_logger(__name__) @@ -93,15 +92,6 @@ class CommandErrorHandler(commands.Cog): await ctx.send(embed=self.error_embed("You are not authorized to use this command.", NEGATIVE_REPLIES)) return - if isinstance(error, APIError): - await ctx.send( - embed=self.error_embed( - f"There was an error when communicating with the {error.api}", - NEGATIVE_REPLIES - ) - ) - return - await self.bot.command_error_manager.handle_error(error, ctx) -- cgit v1.2.3