【问题标题】:How do you check if a specific user reacts to a specific message [discord.py]您如何检查特定用户是否对特定消息做出反应 [discord.py]
【发布时间】:2020-11-20 02:26:56
【问题描述】:

我看过类似的问题,但没有一个答案对我有用。我正在开发一个机器人,您可以在其中输入(prefix)suggest (suggestion),然后它会发送一条消息,询问您是否确定要发送建议。如果他们用机器人添加的反应(复选标记)对该消息做出反应,那么它会将建议发送到一个频道。

简而言之,我如何创建一个触发器,如果​​特定人使用特定表情符号对特定消息做出反应,然后它会发送输出?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    要使其反应检查消息具体,您需要在检查功能中使用reaction.message == message

    这是基于 Libby 回答的完整示例。

    message = await ctx.send("Are you sure you want to submit this suggestion?")
    await message.add_reaction("✅")
    confirmation = await bot.wait_for("reaction_add", check=check) 
    channel = bot.get_channel(channel_id) # Put suggestion channel ID here.
    
    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["✅"] and reaction.message == message
    
    if confirmation:
        await channel.send(suggestion)
    

    【讨论】:

      【解决方案2】:

      您可以使用wait_for 函数让机器人等待作者对复选标记做出反应。

      首先,进行检查以确保仅当消息作者使用复选标记做出反应时,机器人才会将建议发送到建议频道:

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

      然后放上发送确认信息的代码,并带有wait_for功能:

      message = await ctx.send("Are you sure you want to submit this suggestion?")
      await message.add_reaction("✅")
      confirmation = await bot.wait_for("reaction_add", check=check) 
      channel = bot.get_channel(channel_id) # Put suggestion channel ID here.
      

      最后,输入告诉机器人一旦消息作者做出反应后该做什么的代码:

      if confirmation:
          await channel.send(suggestion)
      

      【讨论】:

      • 问题是即使用户添加了对其他消息的反应,机器人仍然会做某事,而不是机器人发送的消息。
      猜你喜欢
      • 1970-01-01
      • 2019-05-24
      • 2019-02-03
      • 1970-01-01
      • 2020-03-28
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      相关资源
      最近更新 更多