如何使用Discord.py监控同一功能中的反应和反应移除

2024-05-14 16:20:28 发布

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

我目前有一个功能,可以轮询消息的反应,并使用Discord.py将用户添加到列表中。代码如下:

    @commands.Cog.listener()
    async def on_message(self, message):
        editMessage = message
        tankBoosters = []
        healBoosters = []
        dpsBooster = []
        boosters = []
        # we do not want the bot to reply to itself
        if message.author != self.bot.user:
            return
        if len(message.embeds) > 0:
            for embed in message.embeds:
                if "Boost is Ready" in embed.fields:
                    return
                else:
                    pass
            for x in message.embeds:
                if '<:tank:801416324306829312>' and '<:healer:801416334243921971>' and '<:dps:801416343848615947>' in x.description:
                    await message.add_reaction('<:tank:801416324306829312>')
                    await message.add_reaction('<:healer:801416334243921971>')
                    await message.add_reaction('<:dps:801416343848615947>')
                    embedToEdit = x

        def check(reaction, user):
            return str(reaction.emoji) in ['<:tank:801416324306829312>', '<:healer:801416334243921971>', '<:dps:801416343848615947>'] and user != self.bot.user

        boosters = tankBoosters + healBoosters + dpsBooster
        while len(boosters) != 4:
            if len(boosters) != 4:
                reaction, user = await self.bot.wait_for('reaction_add', check=check)
                print(message.reactions)
                if reaction.emoji.name == 'tank' and len(tankBoosters) == 0:
                    tankBoosters.append(user)
                if reaction.emoji.name == 'healer' and len(healBoosters) == 0:
                    healBoosters.append(user)
                if reaction.emoji.name == 'dps' and len(dpsBooster) < 2:
                    dpsBooster.append(user)

            if len(tankBoosters) == 1 and len(healBoosters) == 1 and len(dpsBooster) == 2:
                message = f"<:tank:801416324306829312> {tankBoosters[0].mention} \n <:healer:801416334243921971> {healBoosters[0].mention} \n <:dps:801416343848615947> {dpsBooster[0].mention} \n <:dps:801416343848615947> {dpsBooster[1].mention}"
                embedToEdit.add_field(name="Boost is Ready", value=message, inline=False)
                await editMessage.edit(embed=embed)

这很好,但我需要做的是,当从消息中删除反应时,将用户从相应的列表(坦克、治疗和dps)中删除

也就是说,发布一条消息,3辆坦克、2名治疗者和6名DPS通过发布对消息的反应来“注册”该消息,并将其添加到各自的列表中。然后,1个油箱和2个DPS通过消除其对消息的反应来“取消签名”。当用户删除他们的反应时,我需要从列表中删除这些用户。我已经研究过使用message.reactions[0].users(),但是根据VS代码调试终端,message.reactions[0].users()<discord.iterators.ReactionIterator object at 0x011B5DF0> ,不幸的是,我对python或discord了解不够,无法理解


Tags: andin消息messagelenifawaituser
2条回答

对于那些感兴趣或沮丧地寻找如何做到这一点的答案的人,我最终稍微改变了我的方法。我没有在on_message函数中执行所有逻辑,而是删除了该函数并使用了on_raw_reaction_addon_raw_reaction_remove,我通过将用户存储在全局数组中进行跟踪,一旦满足某些条件,这些数组将被清空

这确实意味着代码一次只能处理一篇文章,但这就是我现在所需要做的:)

我想你可以试试这个:

@bot.event
async def on_raw_reaction_remove(payload):
    reaction = str(payload.emoji)
    msg_id = payload.message_id
    user_id = payload.user_id
    guild_id = payload.guild_id
    exists = db.exists(msg_id)

当有人删除他的反应,你知道什么反应,用户ID

相关问题 更多 >

    热门问题