Search code examples
pythondiscorddiscord.py

Discord PY Bot, Creating a list from reactions of a specific message


I'm creating a bot, and in it there is a command that adds a reaction to a message. I'm wondering how to create a list of all those who clicked on the specific reaction. I have been unsure of the variable(s) to use to retrieve the reactors.

@client.command(pass_context=True)
@has_permissions(administrator=True)
async def reaction(ctx):
    msg = await ctx.send("**React '✅' to this message to sign up!**")
    reactions = ['✅']
    for emoji in reactions: 
        await msg.add_reaction(emoji)

Here is the code of the reaction message. Is it possible for discord to list the people who reacted? If so, how would it be done? Apologies if I missed it in the documentation of discord.py, I was unable to find it if so. Thank you :)


Solution

  • Using on_reaction_add will do this. This of course depends on how your bot is structured, but have a list which you add all the users who reacted to the message to. I'd advise you to use cogs if you aren't already as you have two variables which you need to store outside of the reaction function, some_list and msg. You could use global variables but I advise against this.

    Below is a general outline of what your code would look like, be aware that this code may not work by just pasting it in.

    some_list = []
    msg = None
    
    @client.command(pass_context=True)
    @has_permissions(administrator=True)
    async def reaction(ctx):
        global msg
        msg = await ctx.send("**React '✅' to this message to sign up!**")
        reactions = ['✅']
        for emoji in reactions: 
            await msg.add_reaction(emoji)
    
    @client.event
    async def on_reaction_add(reaction, user):
        if reaction.message == msg:
            some_list.append(user)