refactor(welcome-cog): Use cog-wide error handler
Migrates individual command error handlers to the cog-wide `cog_command_error` method. This simplifies error handling by automatically catching errors from all commands within the cog. Adds `ephemeral=True` to error messages sent to the user and logs the `original_error` for better debugging.
This commit is contained in:
parent
4c17db72a8
commit
db5c171c11
@ -435,29 +435,28 @@ class WelcomeCog(commands.Cog):
|
||||
await ctx.send("An unexpected error occurred.", ephemeral=True)
|
||||
|
||||
# Error Handling for this Cog
|
||||
@set_welcome.error
|
||||
@disable_welcome.error
|
||||
@set_goodbye.error
|
||||
@disable_goodbye.error
|
||||
@testmessage.error
|
||||
async def on_command_error(self, ctx: commands.Context, error):
|
||||
async def cog_command_error(self, ctx: commands.Context, error: commands.CommandError):
|
||||
"""Handles errors for all commands in this cog."""
|
||||
if isinstance(error, commands.MissingPermissions):
|
||||
await ctx.send("You need Administrator permissions to use this command.")
|
||||
await ctx.send("You need Administrator permissions to use this command.", ephemeral=True)
|
||||
elif isinstance(error, commands.BadArgument):
|
||||
await ctx.send(
|
||||
f"Invalid argument provided. Check the command help: `{ctx.prefix}help {ctx.command.name}`"
|
||||
f"Invalid argument provided. Check the command help: `{ctx.prefix}help {ctx.command.name}`",
|
||||
ephemeral=True
|
||||
)
|
||||
elif isinstance(error, commands.MissingRequiredArgument):
|
||||
await ctx.send(
|
||||
f"Missing required argument. Check the command help: `{ctx.prefix}help {ctx.command.name}`"
|
||||
f"Missing required argument. Check the command help: `{ctx.prefix}help {ctx.command.name}`",
|
||||
ephemeral=True
|
||||
)
|
||||
elif isinstance(error, commands.NoPrivateMessage):
|
||||
await ctx.send("This command cannot be used in private messages.")
|
||||
await ctx.send("This command cannot be used in private messages.", ephemeral=True)
|
||||
else:
|
||||
original_error = getattr(error, 'original', error)
|
||||
log.error(
|
||||
f"Unhandled error in WelcomeCog command '{ctx.command.name}': {error}"
|
||||
f"Unhandled error in WelcomeCog command '{ctx.command.name}': {original_error}"
|
||||
)
|
||||
await ctx.send("An unexpected error occurred. Please check the logs.")
|
||||
await ctx.send("An unexpected error occurred. Please check the logs.", ephemeral=True)
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
|
Loading…
x
Reference in New Issue
Block a user