【问题标题】:On_message wait for the user to react to the message or timeout before bot sends another message with the reaction againon_message 等待用户对消息做出反应或超时,然后机器人再次发送具有反应的另一条消息
【发布时间】:2021-03-04 11:18:40
【问题描述】:

如果你 DM 机器人,它会发送一条消息并添加一些反应,然后等待用户添加反应,然后再做某事。

我的问题是,如果用户再次 DM 机器人,它会发送另一条消息,如果用户对任何机器人消息做出反应,机器人会根据您从机器人收到的消息数量执行两次或更多操作。

如果当前有一条带有反应的消息,是否有办法让机器人不发送另一条消息,并等待用户先做出反应,然后机器人才能再次发送另一条带有反应的消息?谢谢。

async def on_message(self, message):
    if message.author.bot:
        return

    if isinstance(message.channel, discord.DMChannel):
        dm = await message.channel.send('test')
        await dm.add_reaction("1️⃣")
   
    def check(reaction, user):
        return user == message.author and str(reaction.emoji) in ["1️⃣"]

    while True:
        try:
            reaction, user = await self.client.wait_for("reaction_add", timeout=15, check=check)
            if str(reaction.emoji) == "1️⃣":
                # do something here
 
        except asyncio.TimeoutError:
            await dm.delete()
            break

【问题讨论】:

  • while 循环的目的是什么?

标签: python discord.py


【解决方案1】:

我可能有点晚了,但这里有一个对我有用的解决方案

在您的“检查”语句中,您应该将其添加到其中

and reaction.message == msg

所以它看起来像这样

def check(reaction, user):
    return user == message.author and str(reaction.emoji) in ["1️⃣"] and reaction.message == msg

【讨论】:

    【解决方案2】:

    通过为每条消息建立一个数据库来修复它。感谢那些试图提供帮助的人!

    【讨论】:

      【解决方案3】:

      语法错误,为什么要加while循环?

      这里是固定代码:

      @commands.Cog.listener()
      async def on_message(self, message):
          if message.author.bot:
              return
      
          if isinstance(message.channel, discord.DMChannel):
              # Sending the message and adding reactions
              msg = await message.channel.send('Test')
              await msg.add_reaction('1️⃣')
      
              check = lambda reaction, user: user == message.author and str(reaction) in your_list_of_reactions
      
              try:
                  # Waiting for the reaction
                  reaction, user = await self.client.wait_for('reaction_add', check=check, timeout=15.0)
      
                  if str(reaction) == "1️⃣":
                      # do something
      
              except asyncio.TimeoutError:
                  await msg.delete()
      
          # Adding this so the rest commands will work
          await bot.process_commands(message)
          
      

      【讨论】:

      • 感谢卢卡斯的回复!我试过了,但还是一样。每当您 dm 机器人时,它会不断给出消息 + 添加反应,当您对其做出反应时,它会根据机器人发送的消息数量 + 添加反应多次执行 #do 操作。我放了while循环,所以他们可以点击反应2、3、4、5它提供信息,而1制作一个频道。还尝试使用 time = (datetime.datetime.utcnow() - self.last_timeStamp).total_seconds(),它可以阻止消息,但问题是其他人无法向机器人发送消息。
      • 如果你想制作这样的动态反应系统,你应该看看discord.ext.menus,它仍处于测试阶段,还没有文档,但discord.py discord server 中有一些例子做?tag menus?tag embed menu 在#testing 中查看一些示例。安装它python -m pip install -U git+https://github.com/Rapptz/discord-ext-menus
      • 谢谢!我会仔细看看的!检查了 github,我认为,在 DM 中无法使用。
      猜你喜欢
      • 2021-06-12
      • 2021-07-04
      • 2019-07-13
      • 2023-03-14
      • 2020-02-23
      • 2021-04-11
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多