From d9fd8b40b50450c50e739b539ca54b83fe056979 Mon Sep 17 00:00:00 2001 From: Dimosthenis Schizas Date: Wed, 3 May 2023 13:51:09 +0300 Subject: [PATCH] Handle NoResultFound error in command execution --- src/bot.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/bot.py b/src/bot.py index ee0dadc..8944d54 100644 --- a/src/bot.py +++ b/src/bot.py @@ -5,17 +5,11 @@ import discord from aiohttp import AsyncResolver, ClientSession, TCPConnector from discord import ApplicationContext, Cog, DiscordException, Embed, HTTPException -from discord.ext.commands import Bot as DiscordBot from discord.ext.commands import ( - CommandNotFound, - CommandOnCooldown, - DefaultHelpCommand, - MissingAnyRole, - MissingPermissions, - MissingRequiredArgument, - NoPrivateMessage, - UserInputError, + Bot as DiscordBot, CommandNotFound, CommandOnCooldown, DefaultHelpCommand, + MissingAnyRole, MissingPermissions, MissingRequiredArgument, NoPrivateMessage, UserInputError, ) +from sqlalchemy.exc import NoResultFound from src import trace_config from src.core import constants, settings @@ -78,13 +72,15 @@ async def on_application_command_error(self, ctx: ApplicationContext, error: Dis message = "This command cannot be run in a DM." elif isinstance(error, CommandOnCooldown): message = f"You are on cooldown. Try again in {error.retry_after:.2f}s" + elif isinstance(error, NoResultFound): + message = f"The requested object could not be found." errored_commands.labels(ctx.command.name).inc() if message is None: raise error else: - log.debug(f'A user caused and error which was handled. Message: "{message}".') + log.debug(f"A user caused an error which was handled.", exc_info=error) await ctx.respond(message, delete_after=15, ephemeral=True) async def on_application_command_completion(self, ctx: ApplicationContext) -> None: