Skip to content

Commit

Permalink
Merge pull request #12 from hackthebox/9-applicationcommandinvokeerro…
Browse files Browse the repository at this point in the history
…r-application-command-raised-an-exception-noresultfound-mute-not-found-for-user

Handle NoResultFound error in command execution
  • Loading branch information
dimoschi authored May 3, 2023
2 parents 8e6ecf2 + d9fd8b4 commit 1016e28
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 1016e28

Please sign in to comment.