Lemon553311-dev
Lemon553311-dev

Reputation: 77

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!')

Upvotes: 5

Views: 11166

Answers (2)

7 7 7 7 Is A Qt
7 7 7 7 Is A Qt

Reputation: 29

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!**')

Upvotes: 2

Diggy.
Diggy.

Reputation: 6944

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

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


Reference:

Upvotes: 6

Related Questions