1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 20: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 config import json_loader
from handlers.ReactionHandler import ReactionHandler from handlers.ReactionHandler import ReactionHandler
from handlers.XPHandler import XPHandler from handlers.XPHandler import XPHandler
from handlers import LoggingHandler from handlers import LoggingHandler, ErrorHandler
from services.GuildConfig import GuildConfig from services.GuildConfig import GuildConfig
from services.Help import RacuHelp 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}") 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: async def on_command_completion(ctx) -> None:
""" """
This code is executed when a slash_command has been successfully executed. 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") 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: 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)) @client.listen()
async def on_application_command_error(ctx, error) -> None:
elif isinstance(error, commands.MissingPermissions): await ErrorHandler.on_command_error(ctx, error)
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.event @client.event
async def on_error(event: str, *args, **kwargs) -> None: async def on_error(event: str, *args, **kwargs) -> None:
logs.error(f"[EventHandler] on_error INFO: errors.event.{event} | '*args': {args} | '**kwargs': {kwargs}") await ErrorHandler.on_error(event, *args, **kwargs)
logs.error(f"[EventHandler] on_error EXCEPTION: {sys.exc_info()}")
# load all json # load all json

View file

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