繁体   English   中英

discord.py中的错误处理问题

[英]Problem with error handling in discord.py

我正在尝试使用以下代码在我的 discord.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!')

您缺少@client.command装饰器中的括号。

尝试将其更改为@client.command() ,您应该可以使用 go。


参考:

你必须这样做

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM