1
Fork 0
mirror of https://github.com/allthingslinux/tux.git synced 2024-10-03 09:03:12 +00:00
tux/.archive/ctx_error_handler.py
kzndotsh 5e67bc6306 feat(ctx_error_handler.py): add new error handler for command context
refactor(error_handler.py): update error handling logic to improve error messages and logging
fix(error_handler.py): handle app command errors separately to avoid conflicts
style(error_handler.py): improve code readability and maintainability by simplifying error handling logic

feat(test_error_handler.py): add new file for testing error handling in discord.py
This new file contains a Cog class with commands to raise every type of discord.py error for testing purposes.

feat: add error handling commands to ErrorTests cog in discord bot

This commit adds a series of commands to the ErrorTests cog in the discord bot. Each command raises a specific exception when invoked, allowing for testing and debugging of error handling mechanisms. The exceptions covered include DisabledCommand, CommandInvokeError, CommandOnCooldown, MaxConcurrencyReached, various ExtensionErrors, ClientException, and CommandRegistrationError.

refactor(error_handler.py): rename ErrorHandler to UnifiedErrorHandler for clarity
feat(error_handler.py): add support for traditional command errors and app command errors
fix(error_handler.py): improve error message for better user experience
style(error_handler.py): refactor code for better readability and maintainability
2024-05-01 04:10:58 +00:00

63 lines
2 KiB
Python

import contextlib
import sys
import traceback
import discord
from discord.ext import commands
class ContextCommandErrorHandler(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
@commands.Cog.listener()
async def on_command_error(self, ctx: commands.Context[commands.Bot], error: Exception) -> None:
"""
Handles errors that occur during command execution.
Args:
ctx: The context in which the error occurred.
error: The exception that was raised.
Returns:
None
Raises:
None
"""
# If the command has its own error handler, or the cog has its own error handler, return
if hasattr(ctx.command, "on_error") or (
ctx.cog and ctx.cog._get_overridden_method(ctx.cog.cog_command_error) is not None
):
return
# Ignore these errors
ignored = (commands.CommandNotFound,)
# Get the original exception if it exists
error = getattr(error, "original", error)
# If the error is in the ignored tuple, return
if isinstance(error, ignored):
return
# If the command has been disabled, send a reply to the user
if isinstance(error, commands.DisabledCommand):
await ctx.send(f"{ctx.command} has been disabled.")
# Private message error
elif isinstance(error, commands.NoPrivateMessage):
with contextlib.suppress(discord.HTTPException):
await ctx.author.send(f"{ctx.command} can not be used in Private Messages.")
# elif isinstance(error, commands.BadArgument):
# if ctx.command and ctx.command.qualified_name == "tag list":
# await ctx.send("I could not find that member. Please try again.")
else:
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
async def setup(bot: commands.Bot):
await bot.add_cog(ContextCommandErrorHandler(bot))