1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 18:23:12 +00:00

Create ErrorHandler to handle both prefix and slash errors

This commit is contained in:
wlinator 2024-03-19 08:46:52 +01:00
parent 19cc083fa4
commit 8a3c78232f
3 changed files with 58 additions and 37 deletions

49
handlers/ErrorHandler.py Normal file
View file

@ -0,0 +1,49 @@
import logging
import traceback
import sys
import discord
from discord.ext import commands
from lib.embeds.error import GenericErrors
logs = logging.getLogger('Racu.Core')
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
seconds = error.retry_after
minutes = seconds // 60
seconds %= 60
cooldown = "{:02d}:{:02d}".format(int(minutes), int(seconds))
await ctx.respond(embed=GenericErrors.command_on_cooldown(ctx, cooldown))
elif isinstance(error, commands.MissingPermissions):
await ctx.respond(embed=GenericErrors.missing_permissions(ctx))
elif isinstance(error, commands.BotMissingPermissions):
await ctx.respond(embed=GenericErrors.bot_missing_permissions(ctx))
elif isinstance(error, commands.PrivateMessageOnly):
await ctx.respond(embed=GenericErrors.private_message_only(ctx))
elif isinstance(error, commands.NoPrivateMessage):
await ctx.respond(embed=GenericErrors.guild_only(ctx))
elif isinstance(error, (discord.CheckFailure, commands.CheckFailure)):
logs.info(f"[CommandHandler] {ctx.author.name} check failure: \"/{ctx.command.qualified_name}\"")
elif isinstance(error, (commands.MissingRequiredArgument, commands.BadArgument, commands.CommandNotFound)):
pass
else:
await ctx.respond(embed=GenericErrors.default_exception(ctx))
traceback.print_tb(error.original.__traceback__)
logs.error(f"[CommandHandler] on_command_error: {error}")
async def on_error(event: str, *args, **kwargs) -> None:
logs.error(f"[EventHandler] on_error INFO: errors.event.{event} | '*args': {args} | '**kwargs': {kwargs}")
logs.error(f"[EventHandler] on_error EXCEPTION: {sys.exc_info()}")

43
main.py
View file

@ -12,7 +12,7 @@ from lib.embeds.greet import Greet
from config import json_loader
from handlers.ReactionHandler import ReactionHandler
from handlers.XPHandler import XPHandler
from handlers import LoggingHandler
from handlers import LoggingHandler, ErrorHandler
from services.GuildConfig import GuildConfig
from services.Help import RacuHelp
@ -94,7 +94,7 @@ async def on_member_join(member):
logs.info(f"[GreetingHandler] Message not sent in '{member.guild.name}'. Channel ID may be invalid. {e}")
@client.event
@client.listen()
async def on_command_completion(ctx) -> None:
"""
This code is executed when a slash_command has been successfully executed.
@ -120,46 +120,19 @@ async def on_command_completion(ctx) -> None:
logs.info(f"[CommandHandler] {ctx.author.name} successfully did \"/{executed_command}\". | direct message")
@client.event
@client.listen()
async def on_command_error(ctx, error) -> None:
if isinstance(error, commands.CommandOnCooldown):
await ErrorHandler.on_command_error(ctx, error)
seconds = error.retry_after
minutes = seconds // 60
seconds %= 60
cooldown = "{:02d}:{:02d}".format(int(minutes), int(seconds))
await ctx.respond(embed=GenericErrors.command_on_cooldown(ctx, cooldown))
elif isinstance(error, commands.MissingPermissions):
await ctx.respond(embed=GenericErrors.missing_permissions(ctx))
elif isinstance(error, commands.BotMissingPermissions):
await ctx.respond(embed=GenericErrors.bot_missing_permissions(ctx))
elif isinstance(error, commands.PrivateMessageOnly):
await ctx.respond(embed=GenericErrors.private_message_only(ctx))
elif isinstance(error, commands.NoPrivateMessage):
await ctx.respond(embed=GenericErrors.guild_only(ctx))
elif isinstance(error, (discord.CheckFailure, commands.CheckFailure)):
logs.info(f"[CommandHandler] {ctx.author.name} check failure: \"/{ctx.command.qualified_name}\"")
elif isinstance(error, (commands.MissingRequiredArgument, commands.BadArgument, commands.CommandNotFound)):
pass
else:
await ctx.respond(embed=GenericErrors.default_exception(ctx))
traceback.print_tb(error.original.__traceback__)
logs.error(f"[CommandHandler] on_command_error: {error}")
@client.listen()
async def on_application_command_error(ctx, error) -> None:
await ErrorHandler.on_command_error(ctx, error)
@client.event
async def on_error(event: str, *args, **kwargs) -> None:
logs.error(f"[EventHandler] on_error INFO: errors.event.{event} | '*args': {args} | '**kwargs': {kwargs}")
logs.error(f"[EventHandler] on_error EXCEPTION: {sys.exc_info()}")
await ErrorHandler.on_error(event, *args, **kwargs)
# load all json

View file

@ -18,7 +18,6 @@ class Misc(commands.Cog):
description="Simple status check.",
help="Simple status check, this command will not return the latency of the bot process as this is "
"fairly irrelevant. If the bot replies, it's good to go.",
guild_only=True
)
@commands.check(checks.channel)
async def ping(self, ctx):
@ -28,8 +27,8 @@ class Misc(commands.Cog):
name="uptime",
description="Racu uptime",
help="See how long Racu has been online, the uptime shown will reset when the Misc module is reloaded.",
guild_only=True
)
@commands.guild_only()
@commands.check(checks.channel)
async def uptime(self, ctx):