問題描述
如果用戶鍵入 /foo
并且該命令不存在,我如何發(fā)送消息說該命令不存在"?
If user types /foo
and that command does not exist, how do I send the message saying "This command does not exist"?
這可能很簡(jiǎn)單,但我有點(diǎn)困惑.
This is maybe pretty simple but I'm bit confused.
如果您需要更多信息,請(qǐng)發(fā)表評(píng)論.
If you need more info please comment.
謝謝!
推薦答案
你可以定義一個(gè) on_command_error
事件(請(qǐng)注意,與記錄的重寫分支相比,異步分支上的參數(shù)順序相反)如果出現(xiàn) CommandError
,將被調(diào)用.
You can define an on_command_error
event (Note that the order of arguments is reversed on the async branch, compared to the documented rewrite branch) that will be called if there if a CommandError
is raised.
然后您可以簽入 錯(cuò)誤處理程序 如果錯(cuò)誤是 CommandNotFound
錯(cuò)誤并相應(yīng)處理:
You can then check in that error handler if the error is a CommandNotFound
error and handle it accordingly:
@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
這假設(shè)您正在使用 discord.ext.commands
擴(kuò)展來編寫命令(您應(yīng)該這樣做).
This assumes you are using the discord.ext.commands
extension to write your commands (which you should).
這篇關(guān)于如何響應(yīng) discord.py 中的錯(cuò)誤用戶命令的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!