![](/img/trans.png)
[英]How can i use the wait_for(“reaction_add”) function only for a specific message? discord.py
[英]Discord.py: wait_for('reaction_add') not working as intended
我正在嘗試在 discord.py 上制作一個常見問題解答機器人,到目前為止進展順利。 我想添加一個額外的功能,當機器人檢測到常見問題解答時,機器人不會直接發送答案,而是會發送一條提示消息,其中包含兩種反應——豎起大拇指和不喜歡——並取決於所選的反應由用戶,機器人要么發送答案,要么刪除提示消息。
現在,當詢問常見問題解答時,機器人會檢測到它並發送提示詢問用戶是否想要答案,甚至對其做出反應。 問題是,一旦機器人完成對拇指向下表情符號的反應,提示消息就會被刪除。 我希望它等待用戶做出反應並相應地繼續。
我究竟做錯了什么? 請幫忙。 提前致謝!
@bot.event
async def on_message(message):
await bot.process_commands(message)
if (message.author.bot):
return
if(isQuestion(message.content)):
(answer_text, question_text) = answer_question(message.content)
if answer_text:
botmessage = await message.channel.send(f"""Do you want the answer to: {question_text} ?""")
await botmessage.add_reaction('\N{THUMBS UP SIGN}')
await botmessage.add_reaction('\N{THUMBS DOWN SIGN}')
def checkUp(reaction, user):
return user == message.author and str(reaction.emoji) == '\N{THUMBS UP SIGN}' or str(reaction.emoji) == '\N{THUMBS DOWN SIGN}'
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=checkUp)
except asyncio.TimeoutError:
await botmessage.delete()
else:
print(reaction.emoji)
if reaction.emoji == '\N{THUMBS UP SIGN}':
await botmessage.delete()
await message.channel.send(answer_text)
elif reaction.emoji == '\N{THUMBS DOWN SIGN}':
await botmessage.delete()
問題很可能在於您的“檢查”function,因為您缺少括號。
user == message.author and str(reaction.emoji) == '\N{THUMBS UP SIGN}' or str(reaction.emoji) == '\N{THUMBS DOWN SIGN}'
當遇到這樣的 boolean 鏈時,我不確定 python 的默認行為,但它可能是這樣放置括號的:
(user == message.author and str(reaction.emoji) == '\N{THUMBS UP SIGN}') or (str(reaction.emoji) == '\N{THUMBS DOWN SIGN}')
你看到問題了嗎? 現在,只要有人對消息做出反應,檢查就會返回 True。 這意味着它可以對自己之前的反應做出反應(盡管您之前可能已經執行過此操作,但我們在這里討論的是異步)。
修復:這樣放置括號:
user == message.author and (str(reaction.emoji) == '\N{THUMBS UP SIGN}' or str(reaction.emoji) == '\N{THUMBS DOWN SIGN}')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.