From 6f0ac487cc0e6fd2284707761395d57c3c0c23cc Mon Sep 17 00:00:00 2001 From: stijndcl Date: Fri, 26 Aug 2022 22:55:42 +0200 Subject: [PATCH] Handle custom exceptions --- didier/didier.py | 26 ++++++++++++++++++++++++++ didier/exceptions/__init__.py | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/didier/didier.py b/didier/didier.py index 3217b61..337b342 100644 --- a/didier/didier.py +++ b/didier/didier.py @@ -4,6 +4,7 @@ import os import discord import motor.motor_asyncio from aiohttp import ClientSession +from discord.app_commands import AppCommandError from discord.ext import commands from sqlalchemy.ext.asyncio import AsyncSession @@ -12,6 +13,7 @@ from database.crud import custom_commands from database.engine import DBSession, mongo_client from database.utils.caches import CacheManager from didier.data.embeds.error_embed import create_error_embed +from didier.exceptions import HTTPException, NoMatch from didier.utils.discord.prefix import get_prefix __all__ = ["Didier"] @@ -46,6 +48,8 @@ class Didier(commands.Bot): command_prefix=get_prefix, case_insensitive=True, intents=intents, activity=activity, status=status ) + self.tree.on_error = self.on_app_command_error + @property def postgres_session(self) -> AsyncSession: """Obtain a session for the PostgreSQL database""" @@ -197,6 +201,18 @@ class Didier(commands.Bot): """Event triggered when a new thread is created""" await thread.join() + async def on_app_command_error(self, interaction: discord.Interaction, exception: AppCommandError): + """Event triggered when an application command errors""" + # If commands have their own error handler, let it handle the error instead + if hasattr(interaction.command, "on_error"): + return + + if isinstance(exception, (NoMatch, discord.app_commands.CommandInvokeError)): + if interaction.response.is_done(): + return await interaction.response.send_message(str(exception.original), ephemeral=True) + else: + return await interaction.followup.send(str(exception.original), ephemeral=True) + async def on_command_error(self, ctx: commands.Context, exception: commands.CommandError, /): """Event triggered when a regular command errors""" # If working locally, print everything to your console @@ -219,6 +235,16 @@ class Didier(commands.Bot): ): return + # Responses to things that go wrong during processing of commands + if isinstance(exception, commands.CommandInvokeError) and isinstance( + exception.original, + ( + NoMatch, + HTTPException, + ), + ): + return await ctx.reply(str(exception.original), mention_author=False) + # Print everything that we care about to the logs/stderr await super().on_command_error(ctx, exception) diff --git a/didier/exceptions/__init__.py b/didier/exceptions/__init__.py index e69de29..4321cae 100644 --- a/didier/exceptions/__init__.py +++ b/didier/exceptions/__init__.py @@ -0,0 +1,5 @@ +from .http_exception import HTTPException +from .missing_env import MissingEnvironmentVariable +from .no_match import NoMatch, expect + +__all__ = ["HTTPException", "MissingEnvironmentVariable", "NoMatch", "expect"]