diff options
author | 2024-02-18 21:36:10 +0100 | |
---|---|---|
committer | 2024-03-20 19:15:04 +0100 | |
commit | 38a9e07013f7d49509c35198a4983d6cefb82c24 (patch) | |
tree | 54ce29582a398b64dc3198233808db2e099dffb9 | |
parent | fixup: correct UserNotPlayingError (diff) |
implement the handler for the BadArgument exception
-rw-r--r-- | bot/command_error_handlers/__init__.py | 2 | ||||
-rw-r--r-- | bot/command_error_handlers/bad_argument.py | 29 | ||||
-rw-r--r-- | bot/exts/core/error_handler.py | 14 |
3 files changed, 31 insertions, 14 deletions
diff --git a/bot/command_error_handlers/__init__.py b/bot/command_error_handlers/__init__.py index afd30ec5..f1911050 100644 --- a/bot/command_error_handlers/__init__.py +++ b/bot/command_error_handlers/__init__.py @@ -3,6 +3,7 @@ from pydis_core.utils.error_handling.commands import CommandErrorManager from bot.bot import Bot from .api_error import APIErrorHandler +from .bad_argument import BadArgumentErrorHandler from .command_not_found import CommandNotFoundErrorHandler from .command_on_cooldown import CommandOnCooldownErrorHandler from .default import DefaultCommandErrorHandler @@ -21,4 +22,5 @@ def bootstrap_command_error_manager(bot: Bot) -> CommandErrorManager: manager.register_handler(APIErrorHandler()) manager.register_handler(CommandOnCooldownErrorHandler()) manager.register_handler(UserNotPlayingErrorHandler()) + manager.register_handler(BadArgumentErrorHandler()) return manager diff --git a/bot/command_error_handlers/bad_argument.py b/bot/command_error_handlers/bad_argument.py new file mode 100644 index 00000000..1d5bb29b --- /dev/null +++ b/bot/command_error_handlers/bad_argument.py @@ -0,0 +1,29 @@ +from typing import NoReturn + +from discord import Interaction +from discord.ext.commands import BadArgument, Context +from pydis_core.utils.error_handling.commands import AbstractCommandErrorHandler + +from ._utils import create_error_embed, get_parent_command_and_subcontext, revert_cooldown_counter + + +class BadArgumentErrorHandler(AbstractCommandErrorHandler): + """An handler for the BadArgument 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, BadArgument) + + async def handle_text_command_error(self, context: Context, error: Exception) -> NoReturn: + """Handle error raised in the context of text commands.""" + revert_cooldown_counter(context.command, context.message) + parent_command, ctx = get_parent_command_and_subcontext(context) + embed = create_error_embed( + "The argument you provided was invalid: " + f"{error}\n\nUsage:\n```\n{ctx.prefix}{parent_command}{ctx.command} {ctx.command.signature}\n```" + ) + await context.send(embed=embed) + + async def handle_app_command_error(self, interaction: Interaction, error: Exception) -> NoReturn: + """Handle error raised in the context of app commands.""" + return diff --git a/bot/exts/core/error_handler.py b/bot/exts/core/error_handler.py index d4330961..6b725f04 100644 --- a/bot/exts/core/error_handler.py +++ b/bot/exts/core/error_handler.py @@ -48,11 +48,6 @@ class CommandErrorHandler(commands.Cog): log.debug(f"Command {ctx.command} had its error already handled locally; ignoring.") return - parent_command = "" - if subctx := getattr(ctx, "subcontext", None): - parent_command = f"{ctx.command} " - ctx = subctx - error = getattr(error, "original", error) log.debug( f"Error Encountered: {type(error).__name__} - {error!s}, " @@ -79,15 +74,6 @@ class CommandErrorHandler(commands.Cog): ) return - if isinstance(error, commands.BadArgument): - self.revert_cooldown_counter(ctx.command, ctx.message) - embed = self.error_embed( - "The argument you provided was invalid: " - f"{error}\n\nUsage:\n```\n{ctx.prefix}{parent_command}{ctx.command} {ctx.command.signature}\n```" - ) - await ctx.send(embed=embed) - return - if isinstance(error, commands.CheckFailure): await ctx.send(embed=self.error_embed("You are not authorized to use this command.", NEGATIVE_REPLIES)) return |