aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar shtlrs <[email protected]>2024-02-18 21:30:24 +0100
committerGravatar shtlrs <[email protected]>2024-03-20 19:15:04 +0100
commit74d0484abed4f756beaebeca278b22f491131a5e (patch)
tree2d8323f29226875e16e61fe84f1764713a05e371
parentimplement the handler for the UserNotPlayingError exception (diff)
implement the handler for the ApiError exception
-rw-r--r--bot/command_error_handlers/__init__.py6
-rw-r--r--bot/command_error_handlers/api_error.py36
-rw-r--r--bot/exts/core/error_handler.py10
3 files changed, 42 insertions, 10 deletions
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)