Reaction edit, only edit the most recent command in discord.py

Viewed 323

Most of this is fine, but how do I make it only edit the most recent message that the user has sent? If someone types the same command twice, then reacts on the 2nd message, it will edit both messages as long as it's in the 60 second window before it times out. Here is the code:

        px = await ctx.send(embed=e)
        for name in reactions:
            emoji = get(ctx.guild.emojis, name=name)
            try: await px.add_reaction(emoji or name)
            except: return


        def check(reaction, user):
            return user == ctx.author and str(reaction.emoji) in reactions

        while True:
            try:
                reaction, user = await self.bot.wait_for("reaction_add", timeout=60, check=check)
                if str(reaction.emoji) == "right":
                    p += 1
                    # doing the command again but + 1 page
                    await px.edit(embed=e)
                elif str(reaction.emoji) == "left":
                    p -= 1
                    # doing the command again but - 1 page
                    await px.edit(embed=e)
                else:
                    await px.remove_reaction(reaction, user)
            except asyncio.TimeoutError:
                break

I only want it to edit the most recent use of the command from that user. Thanks.

1 Answers

On my phone so hard to type. But in check add

px.id == reaction.message.id

Px should be a message object with an ID

This will ensure that when they click a reaction on an embed the correct handler receives it. You said you only wanted the latest to handle it... this would not quite do that. But any reactions on the latest would be handled for the latest, and any reactions on an earlier one would be handled by the earlier handlers. If that makes sense.

Related