Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

python - How to monitor both reactions and removal of reactions in the same function using Discord.py

I currently have a function which is polling a message for reactions and adding users to a list based on that using Discord.py. Here is the code below:

    @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} 
 <:healer:801416334243921971> {healBoosters[0].mention} 
 <:dps:801416343848615947> {dpsBooster[0].mention} 
 <:dps:801416343848615947> {dpsBooster[1].mention}"
                embedToEdit.add_field(name="Boost is Ready", value=message, inline=False)
                await editMessage.edit(embed=embed)

This is working fine, but what I need to do be able to do is remove users from the respective lists (tank, heal and dps) when a reaction is removed from the message.

I.e. a message is posted and 3 tanks, 2 healers and 6 DPS "sign up" to the message by posting reactions to the message and they are appended to their respective lists. Then 1 tank and 2 DPS "unsign" by removing their reaction to the message. I need to remove those users from the list when they remove their reaction. I have looked into using message.reactions[0].users() but according to the VS Code debug terminal, message.reactions[0].users() is <discord.iterators.ReactionIterator object at 0x011B5DF0> , which I unfortunately don't know enough about python or discord to understand!

question from:https://stackoverflow.com/questions/65845176/how-to-monitor-both-reactions-and-removal-of-reactions-in-the-same-function-usin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you can try this:

@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)

When someone remove his reaction, you know what reaction, the user ID...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...