【问题标题】:How do I get my bot to respond only to reactions on a specific message? | discord.py如何让我的机器人仅响应对特定消息的反应? |不和谐.py
【发布时间】:2020-12-01 15:25:50
【问题描述】:

我正在尝试为朋友实现一个机器人,其中“info”命令将在一个嵌入页面上显示用户的信息,而下一个将显示用户拥有的任何字符(通过 gspread 读取)。

mx = await ctx.send(embed=contents[0])   
  await mx.add_reaction("◀")
  await mx.add_reaction("▶")

  def check(reaction, user):
    return user == ctx.author and str(reaction.emoji) in ["◀", "▶"]

  while True:
    try:
      reaction, user = await client.wait_for("reaction_add", timeout=20, check=check)
      if str(reaction.emoji) == "▶" and cur_page != pages:
        cur_page += 1
        await mx.edit(embed=contents[cur_page - 1])
        await mx.remove_reaction(reaction, user)
      elif str(reaction.emoji) == "◀" and cur_page > 1:
        cur_page -= 1
        await mx.edit(embed=contents[cur_page - 1])
        await mx.remove_reaction(reaction, user)
      else:
        await mx.remove_reaction(reaction, user)
    except asyncio.TimeoutError:
      await mx.clear_reaction("◀")
      await mx.clear_reaction("▶")
      break 

问题是,每当用户发送多个信息命令时,响应时,它会导致编辑所有仍处于活动状态的消息,而不仅仅是他们正在响应的消息。我也试过了:

def check(reaction, user):
    return user == ctx.author and str(reaction.emoji) in ["◀", "▶"]
    mx == reaction.message

但它并没有解决问题。我还尝试了 .json 转储并将 mx.id 替换为用户的最新消息,但这返回了相同的问题。任何帮助将不胜感激!

【问题讨论】:

    标签: python python-3.x discord.py discord.py-rewrite


    【解决方案1】:

    mx == reaction.message 也应该是 return 语句的一部分。

    固定代码:

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["◀", "▶"] and mx == reaction.message
    

    【讨论】:

    • 你是一个绝对的救星,非常感谢!
    猜你喜欢
    • 2021-03-03
    • 2021-11-28
    • 2021-07-07
    • 2020-09-03
    • 2020-06-01
    • 1970-01-01
    • 2021-05-24
    • 2020-12-06
    • 2021-03-21
    相关资源
    最近更新 更多