Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle NoResultFound error in command execution #12

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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