检查用户列表是否对消息做出了反应不和.py雷弗瑞

2024-10-03 17:16:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为一个bot发出一个命令,当消息中提到的每个人都对bot的响应做出反应时,它会提到原始消息的作者

我就是这么想的

        if msg.content.startswith('/iniciar'):
            async with msg.channel.typing():
                mentions = ""
                for mention in msg.mentions:
                    mentions = mentions + " " + mention.mention
                bot_msg: discord.Message = await msg.channel.send(mentions + ' confirmem presença reagindo abaixo.')
                await bot_msg.add_reaction('✅')
                for mention in msg.mentions:
                    def check(reaction, user):
                        return user == mention and str(reaction.emoji) == '✅'
                    try:
                        reaction, user = await client.wait_for('reaction_add', check=check)
                    finally:
                        reactionusers: list = await reaction.users().flatten()
                        reactionusers.remove(reactionusers[0])
                        print(reactionusers)
                        print(msg.mentions)
                        if reactionusers == msg.mentions:
                            await msg.channel.send(msg.author.mention)
                        else:
                            return


Tags: insend消息forifcheckbotchannel
1条回答
网友
1楼 · 发布于 2024-10-03 17:16:38

好吧,首先,您需要考虑当前实现中的一些事情。首先,您可能需要保存您希望bot在某处监视的消息ID。不管这是SQL,一个架子,随便什么

为此,当调用/iniciar函数时,将消息ID保存在您喜欢的任何长期存储方法中(bot可以检查的地方)。在

然后你要考虑如何激活这个代码块,我在你当前的代码中看不到这个代码块。出于您的目的,我建议使用on_reaction_add,其中有文档:https://discordpy.readthedocs.io/en/latest/api.html#discord.on_reaction_add

使用Reaction.message获取message对象,并将消息中提到的人的相关信息读入列表(类似于您的操作方式)。在

当用户对消息作出反应并调用on_reaction_add时:

  1. 检查正在响应的消息是否在存储解决方案中,如果是您正在等待在继续时看到反应的消息
  2. 然后检查响应的用户是否在该消息中提到的用户列表中
  3. 如果是,那么让机器人提到消息作者
  4. 最后检查是否所有的用户都有反应,如果有,从SQL/Shelve/任何存储中删除消息,这样就不会有进一步的反应提到作者了。在

相关问题 更多 >