如何在discord.py中响应错误的用户命令

问题描述 投票:-1回答:1

如果用户键入/foo且该命令不存在,如何发送消息“此命令不存在”?

这可能很简单,但我有点困惑。

如果您需要更多信息,请发表评论。

谢谢!

python discord.py
1个回答
1
投票

你可以定义一个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扩展来编写命令(您应该这样做)。

© www.soinside.com 2019 - 2024. All rights reserved.