如果用户键入/foo
且该命令不存在,如何发送消息“此命令不存在”?
这可能很简单,但我有点困惑。
如果您需要更多信息,请发表评论。
谢谢!
你可以定义一个on_command_error
事件(请注意,在async分支上,参数的顺序是相反的,与记录的重写分支相比),如果引发了CommandError
,它将被调用。
然后你可以检查error handler,如果错误是CommandNotFound
错误并相应地处理它:
@bot.event
async def on_command_error(error, ctx):
if isinstance(error, commands.CommandNotFound):
await bot.send_message(ctx.message.channel, "No such command")
else:
raise error
这假设您使用discord.ext.commands
扩展来编写命令(您应该这样做)。