【问题标题】:Bot doesn't read the second reaction when user reacts to 2 reaction.. discord.py当用户对 2 个反应做出反应时,机器人不会读取第二个反应.. discord.py
【发布时间】:2021-06-28 02:51:05
【问题描述】:

我有一个代码,机器人需要读取用户对消息的两种反应,但使用此代码,机器人不会打印“第二次检查”,这意味着机器人不阅读第二次反应和在第一个实现后停止

reacttn = True
def check(reaction, user):
  return user == members.users[members.leader].user and reaction.message.id == ttreact.id
while reacttn == True:
  reaction, user = await client.wait_for("reaction_add", check=check)

  if len(members.users) == 2:
    if str(reaction.emoji) == "1️⃣":
      print("first check")
    if str(reaction.emoji) == "2️⃣":
      print("second check")
    await asyncio.sleep(5)
    reacttn = False

【问题讨论】:

    标签: list if-statement async-await discord discord.py


    【解决方案1】:

    你可以这样做:

    from collections.abc import Sequence
    
    
    def sequence(seq):
       if seq is None:
           return ()
       if isinstance(seq, Sequence) and not isninstance(seq, str):
           return seq
       else:
          return(seq,)
    
    def reaction_check:(message=None, emoji=None, author=None, ignore_bot=True):
        message = sequence(message)
        message = tuple(m.id for m in message)
        emoji = sequence(emoji)
        author = sequence(author)
        def check(reaction, user):
            if ignore_bot and user.bot:
                return False
            if message and reaction.message.id not in message:
                return False
            if emoji and reaction.emoji not in emoji:
                return False
            if author and user not in author:
                return False
            return True
        return check
    
    

    在命令中:

    msg = await ctx.send("react to this message!")
    
    await msg.add_reaction("1️⃣")
    await msg.add_reaction("2️⃣")
    
    check = reaction_check(message=msg, author=member, emoji=("1️⃣","2️⃣"))
    
    reaction, user = await client.wait_for("reaction_add", check=check)
    
    if reaction.emoji == "1️⃣":
       #first logic
    
    if reaction.emoji == "2️⃣":
       #second logic
    

    【讨论】:

    • 我做了和以前一样但相同的结果..我知道为什么..
    • 这很奇怪....尝试启用intents.reactions
    • 是的..我发现了问题..问题就像单独检查 wait_for 并单独检查单独的反应,因为 wait_for 只等到它成真..!
    猜你喜欢
    • 2020-11-24
    • 1970-01-01
    • 2020-04-16
    • 2021-12-31
    • 2021-11-14
    • 2020-11-07
    • 2021-12-18
    • 2021-05-01
    • 2021-07-04
    相关资源
    最近更新 更多