【问题标题】:Discord.py | Avoid getting more than one reaction by a same user to a message sent by a bot不和谐.py |避免同一用户对机器人发送的消息做出多个反应
【发布时间】:2020-05-19 13:20:00
【问题描述】:

我正在尝试制作一个将在嵌入中发送的命令,并且该命令有两个反应,一个勾号和一个叉号,我想让用户只对其中一个反应做出反应,而不是对它们都做出反应。我还需要一些帮助来建立一个系统,以确保做出反应的人具有特定的角色。任何帮助将不胜感激!

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    这可以使用on_raw_reaction_add() 事件来实现。

    @bot.event
    async def on_raw_reaction_add(payload): # checks whenever a reaction is added to a message
                                            # whether the message is in the cache or not
    
         # check which channel the reaction was added in
        if payload.channel_id == 112233445566778899:
    
            channel = await bot.fetch_channel(payload.channel_id)
            message = await channel.fetch_message(payload.message_id)
    
            # iterating through each reaction in the message
            for r in message.reactions:
    
                # checks the reactant isn't a bot and the emoji isn't the one they just reacted with
                if payload.member in await r.users().flatten() and not payload.member.bot and str(r) != str(payload.emoji):
    
                    # removes the reaction
                    await message.remove_reaction(r.emoji, payload.member)
    

    参考资料:

    【讨论】:

      【解决方案2】:

      我认为您可以存储 userId 或所有用户唯一的东西。 然后创建一个函数检查这个ID是否出现了不止一次或其他逻辑。

      https://discordpy.readthedocs.io/en/latest/api.html#userUser API

      在这里,您可以从该对象中获取用户 ID。

      此外,您可以从客户收到的消息中获取作者 ID。

      def on_message(message):
          print(message.author.id)
      

      【讨论】:

        猜你喜欢
        • 2021-03-21
        • 2021-08-28
        • 2021-07-04
        • 2021-05-08
        • 2021-03-13
        • 2020-11-20
        • 2021-10-31
        • 2022-11-02
        • 2020-09-03
        相关资源
        最近更新 更多