[英]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.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.