alexander stevens
alexander stevens

Reputation: 13

Add role to user who reacts to bot message problem

I have a command and the bot responds with a message and adds a reaction to it, if the person who types the command reacts to it they get the role, but I want anyone to be able to react to it as I only want to type it once. it works but only for the person typing the command.

I've tried using other solutions but none other seem to work.

client.on("message", (message) => {

    client.on('messageReactionAdd', (reaction, user) => {
        if(reaction.emoji.name === "😶") {
            const guild = client.guilds.get('563851342532313088')
            const member = message.member;
            console.log(user.id);
            const role = guild.roles.find(role => role.name === 'Todosquad');
            member.addRole(role)
        }
    });

Console.log(user.id); prints everyone who reacted but it still won't give them the role.

Upvotes: 0

Views: 1129

Answers (1)

Anu6is
Anu6is

Reputation: 2658

The messageReactionAdd event has two parameters - MessageReaction and User.
In your example, those would be reaction and user. You are utilizing the reaction parameter to ensure that the reaction added in in fact the one you want. However, when it comes to adding the role to the person that added the reaction, you are referencing member which is actually the person that sent the message (message.member), not the person that added the reaction to the message.

To add the role to anyone that reacts, you'd need to
Change:

member.addRole(role)

To either an async event with: ( client.on('messageReactionAdd', async (reaction, user) => )

const guildmember = await guild.fetchMember(user);
guildmember.addRole(role);

Or do:

guild.fetchMember(user).then(guildmember => guildmember.addRole(role));

Edit: the addRole function only exists on the guildMember object since you can only add a role to someone in a guild.

Upvotes: 1

Related Questions