Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with error handling in discord.py

Im trying to create error messages in my discord python bot with this code:

@purge.error
async def clear_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send('Please specify the amount of messages you want to clear. Usage: //clear <number>')
    if isinstance(error, commands.MissingPermissions):
        await ctx.send('You do not have manage_messages permssion')

but im getting this error in the console:

Traceback (most recent call last):
  File "c:/projects/bad bot/bot.py", line 132, in <module>
    @purge.error
AttributeError: 'function' object has no attribute 'error'

and I don't understand why is it happening.

If needed the purge command:

@client.command
@commands.has_permissions(manage_messages=True)
async def purge(ctx, amount : int):
    await ctx.channel.purge(limit=amount)
    await ctx.send('Done!')
like image 916
Lemon553311-dev Avatar asked Mar 02 '23 10:03

Lemon553311-dev


2 Answers

You're missing the parentheses in your @client.command decorator.

Try changing it to @client.command() and you should be good to go.


Reference:

  • commands.command()
like image 149
Diggy. Avatar answered Mar 18 '23 00:03

Diggy.


You gotta do it like this

@client.event
async def on_command_error(ctx, error):
    pass

@client.command()
@commands.has_permissions(manage_messages=True)
async def purge(ctx, amount : int):
    await ctx.channel.purge(limit=amount)
    await ctx.send('Done!')

@purge.error
async def clear_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        await ctx.send('Please specify the amount of messages you want to clear. Usage: //clear <number>')
    if isinstance(error, commands.MissingPermissions):
        await ctx.send('**You do not have manage_messages permssion!**')
like image 25
7 7 7 7 Is A Qt Avatar answered Mar 18 '23 02:03

7 7 7 7 Is A Qt