捕获和响应 discord.py wait_for_messages超时的方法是什么?



在我的不和谐机器人中,我正在处理一个命令,您可以在其中执行类似 !command 的操作,然后机器人回复"执行命令?[y/n]",听你说"y",然后做某事。我正在尝试为此函数添加超时,但我无法捕捉到此超时何时发生并触发事件,例如当超时发生时,机器人说"确认超时"并继续。

我已经尝试了elif userinput is None:elif userinput is '':,两者都没有触发。 我在Raspbian stretch上使用 discord.py 0.16.12和Python 3.6.1。

这是我目前拥有的:

msgconfirm = await client.say(f"[{context.message.author.mention}] Confirm? [y/n]")
userinput = await client.wait_for_message(timeout=5, author=context.message.author)
if userinput.clean_content.lower() == 'y':
do something
elif userinput is None:
do something
else:
do something

目前,当超时触发时,它只是忽略我发送的下一条消息,但不触发elif userinput is None:。我正在寻找它来触发该块,因此我可以添加类似await client.say(f"[{context.message.author.mention}] Confirmation timed out.

我已经尝试使用此页面上的答案,尽管它们都不起作用。该页面上的代码格式也不正确。

经过一番移动,我发现如果你只是把if userinput == None:`` at the top of the if section, and move theif userinput,clean_content.lower() == 'y':"放到当前的 elif 行,一切正常。工作示例:

if userinput is None:
do something
elif userinput.clean_content.lower() == 'y':
do something
else:
do something

最新更新