discord.py中的错误处理问题

我正在尝试使用以下代码在不和谐的python机器人中创建错误消息:

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

但我在控制台中收到此错误:

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'

我不明白为什么会这样。

如果需要,清除命令:

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

尝试这个:

@bot.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def clean(ctx, limit: int):
        await ctx.channel.purge(limit=limit)
        await ctx.send('Cleared by {}'.format(ctx.author.mention))
        await ctx.message.delete()

@clean.error
async def clear_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You cant do that!")

according to: I have a error in discord.py (purge command)

点赞
评论
set
set

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

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

参考:

点赞
评论