Closed Swiddis closed 5 months ago
Looking at the discord.py docs here this looks to be a possible solution example:
@leaderboard.error
async def leaderboard_error(self, ctx, error):
if isinstance(error, commands.BadArgument):
await ctx.send(
"Error: The passed arguments for .leaderboard were not valid!\n"
"Expected Format: '.leaderboard (debt|cranes) (number>=1)'\n"
"Example: '.leaderboard debt 10'\n"
"You can use '.help leaderboard' for more information.\n"
)
Also avoids entry into discord.log.
Every command would need to have its own local error handler (customizable, but verbose maybe?), and an agreed on message format would be needed.
Would this be considered a sufficient solution?
Was also looking at the solution in the Nikola post:
import discord
import sys
import traceback
from discord.ext import commands
class CommandErrHandler(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
"""The event triggered when an error is raised while invoking a command.
Parameters
------------
ctx: commands.Context
The context used for command invocation.
error: commands.CommandError
The Exception raised.
"""
if isinstance(error, discord.ext.commands.CommandNotFound):
await ctx.send('I do not know that command?!')
else:
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
It has the benefit that it can wrap around every command, and e.g. handle common error types like all the "Yell at Toast" game checks
Would you like to take this @MO-W59? I'll assign this to myself soon if not
Both solutions have advantages (customizability vs repetition), main goal is avoiding too many error logs with a secondary goal of removing some repetitive handling like if not bot.game
. There might be some good second-order improvements to usability but I'm also okay with small PRs in the name of incremental improvement.
@Swiddis I would be happy to see what I can come up with.
I'm new to the discord API but I like the wrapper solution idea, sounds better for maintainability in my opinion.
Currently, the bot raises exceptions when it encounters an unknown command or invalid syntax. There should be some sort of error handling, both to help users (the bot actually answers invalid queries instead of staying silent) and to stop flooding the logs with exceptions.