diff options
Diffstat (limited to 'arthur/exts')
| -rw-r--r-- | arthur/exts/error_handler/error_handler.py | 60 |
1 files changed, 60 insertions, 0 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)) |