I have a question that's very weird in my case. I'm using python3.9 and discord.py v1.7.3. I want to use the on_command_error example from here but sadly, the last line doesn't work. In pycharm I get Cannot find reference '__traceback__' in 'None' and if I anyways try to just run the code, it print's it correctly to the console of my discord bot, but if I try to print the last line to a discord channel, it's sending the string None. I searched a lot and asked on the official discord.py support server, but nobody could give me an answer.
This is my entire cog:
import sys
import traceback
import discord
from discord.ext import commands
from datetime import datetime
class ErrorHandler(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
# 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
# For this error example we check to see where it came from...
elif isinstance(error, commands.BadArgument):
if ctx.command.qualified_name == 'tag list': # Check if the command being invoked is 'tag list'
await ctx.send('I could not find that member. Please try again.')
else:
# All other Errors not returned come here. And we can just print the default TraceBack.
traceback1 = traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
error_channel = self.client.get_channel(123456789)
await error_channel.send(traceback1)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
def setup(client):
client.add_cog(ErrorHandler(client))
Example Traceback from the console:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/discord_slash/client.py", line 1352, in invoke_command
await func.invoke(ctx, **args)
File "/usr/local/lib/python3.9/site-packages/discord_slash/model.py", line 209, in invoke
return await self.func(self.cog, *args, **kwargs)
File "/Bots/gift-bot/cogs/stats_commands.py", line 1412, in leaderboard_slash
await self.leaderboard_def(ctx, typ)
File "/Bots/gift-bot/cogs/stats_commands.py", line 539, in leaderboard_def
await msg.edit(embed=win_embed, components=None)
AttributeError: 'NoneType' object has no attribute 'edit'