discordbot(python)如何使用wait_fo来等待消息或反应



我正在为我的discord机器人制作一种琐事命令,用户可以随机得到一个缺少字母的单词,他们必须猜测这个单词是什么才能获胜。

然而,我也想对机器人的琐事消息做出反应,用户可以按下该消息立即透露这个词是什么,这样他们就可以输入并获胜。

我基本上在寻找一种使用wait_for-coroutine等待反应的方法,该反应将显示单词和提示获胜者的消息。(反应的使用是可选的,所以它并不总是保证只用于猜测单词(。

这是我迄今为止所拥有的。


wordChosen = random.choice(randomWords)
def check(m): # Checks if the user entered the correct word.
if m.content.lower() != wordChosen:
return False
elif m.channel != channel:
return False
else: # User met the requirements.
return True

try:
msg = await self.client.wait_for(
"message",
timeout=60,
check=check
)
except:
asyncio.TimeoutError
await wordMessage.edit(content=f"You guys took too long!. The word was {wordChosen}.")
else:
if msg:
await msg.channel.send(f"{msg.author.mention} guessed the word!")

except部分之后,应该放asyncio.TimeoutError。而不是放在:之后。

意思是:

except asyncio.TimeoutError:
await wordMessage.edit(content=f"You guys took too long!. The word was {wordChosen}.")

最新更新