diff options
author | 2024-02-18 21:00:37 +0100 | |
---|---|---|
committer | 2024-03-20 19:15:03 +0100 | |
commit | f06f82f5cbf10bafd855ec8bc4245a7fb1b965af (patch) | |
tree | bc4d5c59d57be41f397f49a1aa2fe6eecf4b338e | |
parent | register default and command not found error handlers (diff) |
implement the MovedCommandErrorHandler
-rw-r--r-- | bot/command_error_handlers/__init__.py | 2 | ||||
-rw-r--r-- | bot/command_error_handlers/_utils.py | 17 | ||||
-rw-r--r-- | bot/command_error_handlers/moved_command.py | 40 | ||||
-rw-r--r-- | bot/exts/core/error_handler.py | 10 |
4 files changed, 60 insertions, 9 deletions
diff --git a/bot/command_error_handlers/__init__.py b/bot/command_error_handlers/__init__.py index a5181636..32c713bd 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 .command_not_found import CommandNotFoundErrorHandler from .default import DefaultCommandErrorHandler +from .moved_command import MovedCommandErrorHandler def bootstrap_command_error_manager(bot: Bot) -> CommandErrorManager: @@ -11,4 +12,5 @@ def bootstrap_command_error_manager(bot: Bot) -> CommandErrorManager: default_handler = DefaultCommandErrorHandler() manager = CommandErrorManager(default=default_handler) manager.register_handler(CommandNotFoundErrorHandler(bot)) + manager.register_handler(MovedCommandErrorHandler()) return manager diff --git a/bot/command_error_handlers/_utils.py b/bot/command_error_handlers/_utils.py new file mode 100644 index 00000000..3a51bfee --- /dev/null +++ b/bot/command_error_handlers/_utils.py @@ -0,0 +1,17 @@ +import random +from collections.abc import Iterable + +from discord import Embed + +from bot.constants import Colours, ERROR_REPLIES + + +def create_error_embed(message: str, title: Iterable | str = ERROR_REPLIES) -> Embed: + """Build a basic embed with red colour and either a random error title or a title provided.""" + embed = Embed(colour=Colours.soft_red) + if isinstance(title, str): + embed.title = title + else: + embed.title = random.choice(title) + embed.description = message + return embed diff --git a/bot/command_error_handlers/moved_command.py b/bot/command_error_handlers/moved_command.py new file mode 100644 index 00000000..d01acae4 --- /dev/null +++ b/bot/command_error_handlers/moved_command.py @@ -0,0 +1,40 @@ +from typing import NoReturn + +from discord import Embed, 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 MovedCommandError + +from ._utils import create_error_embed + + +class MovedCommandErrorHandler(AbstractCommandErrorHandler): + """An handler for the MovedCommand 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, MovedCommandError) + + 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=self._get_error_embed(context.prefix, context.command.qualified_name, error.new_command_name) + ) + + 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("/", interaction.command.name, error.new_command_name) + ) + + @staticmethod + def _get_error_embed(prefix: str, command_name: str, new_command_name: str) -> Embed: + return create_error_embed( + message=( + f"This command, `{prefix}{command_name}` has moved to `{new_command_name}`.\n" + f"Please use `{new_command_name}` instead." + ), + title=NEGATIVE_REPLIES + ) diff --git a/bot/exts/core/error_handler.py b/bot/exts/core/error_handler.py index ce62070b..0e5d60fc 100644 --- a/bot/exts/core/error_handler.py +++ b/bot/exts/core/error_handler.py @@ -9,7 +9,7 @@ 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, MovedCommandError, UserNotPlayingError +from bot.utils.exceptions import APIError, UserNotPlayingError log = get_logger(__name__) @@ -125,14 +125,6 @@ class CommandErrorHandler(commands.Cog): ) return - if isinstance(error, MovedCommandError): - description = ( - f"This command, `{ctx.prefix}{ctx.command.qualified_name}` has moved to `{error.new_command_name}`.\n" - f"Please use `{error.new_command_name}` instead." - ) - await ctx.send(embed=self.error_embed(description, NEGATIVE_REPLIES)) - return - await self.bot.command_error_manager.handle_error(error, ctx) |