blob: 0b61e7d33a0e2faf6a64a32732b6d99bf94c0e8f (
plain) (
tree)
|
|
import sys
import traceback
import discord
from discord.ext import commands
class Errorhandler(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.
"""
# This prevents any commands with local handlers being handled here in on_command_error.
if hasattr(ctx.command, "on_error"):
return
# This prevents any cogs with an overwritten cog_command_error being handled here.
cog = ctx.cog
if cog:
if cog._get_overridden_method(cog.cog_command_error) is not None:
return
ignored = (commands.CommandNotFound,)
# Allows us to check for original exceptions raised and sent to CommandInvokeError.
# If nothing is found. We keep the exception passed to on_command_error.
error = getattr(error, "original", error)
# Anything in ignored will return and prevent anything happening.
if isinstance(error, ignored):
return
if isinstance(error, commands.DisabledCommand):
await ctx.send(f"{ctx.command} has been disabled.")
elif isinstance(error, commands.NoPrivateMessage):
try:
await ctx.author.send(
f"{ctx.command} can not be used in Private Messages."
)
except discord.HTTPException:
pass
elif isinstance(error, commands.CommandOnCooldown):
await ctx.send(
f"{ctx.author.mention}, you have to wait {round(error.retry_after, 2)} seconds before using this again"
)
else:
# All other Errors not returned come here. And we can just print the default TraceBack.
print(
"Ignoring exception in command {}:".format(ctx.command), file=sys.stderr
)
traceback.print_exception(
type(error), error, error.__traceback__, file=sys.stderr
)
async def setup(bot):
await bot.add_cog(Errorhandler(bot))
|