diff options
-rw-r--r-- | arthur/exts/error_handler/error_handler.py | 60 | ||||
-rw-r--r-- | arthur/utils.py | 2 |
2 files changed, 61 insertions, 1 deletions
diff --git a/arthur/exts/error_handler/error_handler.py b/arthur/exts/error_handler/error_handler.py new file mode 100644 index 0000000..2905fc3 --- /dev/null +++ b/arthur/exts/error_handler/error_handler.py @@ -0,0 +1,60 @@ +"""This cog provides error handling for King Arthur.""" +from discord import Message +from arthur.utils import generate_error_embed +from discord.ext import commands +from discord.ext.commands import Cog + +from arthur.bot import KingArthur + + +class ErrorHandler(Cog): + """Error handling for King Arthur.""" + + def __init__(self, bot: KingArthur) -> None: + self.bot = bot + + async def _add_error_reaction(self, message: Message) -> None: + await message.add_reaction("\N{CROSS MARK}") + + @Cog.listener() + async def on_command_error(self, ctx: commands.Context, error: commands.CommandError) -> None: + """Handle exceptions raised during command processing.""" + if isinstance(error, commands.CommandNotFound): + return + if isinstance(error, commands.MissingRequiredArgument): + await self._add_error_reaction(ctx.message) + await ctx.send_help(ctx.command) + elif isinstance(error, commands.BadArgument): + await self._add_error_reaction(ctx.message) + await ctx.send_help(ctx.command) + elif isinstance(error, commands.CheckFailure): + await self._add_error_reaction(ctx.message) + elif isinstance(error, commands.NoPrivateMessage): + await self._add_error_reaction(ctx.message) + elif isinstance(error, commands.CommandOnCooldown): + await self._add_error_reaction(ctx.message) + elif isinstance(error, commands.DisabledCommand): + await self._add_error_reaction(ctx.message) + elif isinstance(error, commands.CommandInvokeError): + await self._add_error_reaction(ctx.message) + await ctx.send( + embed=generate_error_embed( + description=( + f"Command raised an error: `{error.original.__class__.__name__}:" + f" {error.original}`" + ) + ) + ) + else: + await ctx.send( + embed=generate_error_embed( + description=( + f"Unknown exception occurred: `{error.__class__.__name__}:" + f" {error}`" + ) + ) + ) + +def setup(bot: KingArthur) -> None: + """Add cog to bot.""" + bot.add_cog(ErrorHandler(bot)) diff --git a/arthur/utils.py b/arthur/utils.py index ea545b5..c81a47a 100644 --- a/arthur/utils.py +++ b/arthur/utils.py @@ -7,7 +7,7 @@ from discord.colour import Colour def generate_error_embed( - title: str = "'Tis but a scratch!", description: str = "An error occurred" + *, title: str = "'Tis but a scratch!", description: str = "An error occurred" ) -> Embed: """Generate an error embed to return to Discord.""" return Embed(title=title, description=description, colour=Colour.red()) |