discord.js reaction role code example

Example 1: get discord.js role

let role = message.guild.roles.cache.find(r => r.id === "Role ID");

// The member you want to add the role to
let member = message.mentions.members.first();

// Add role to the member
member.roles.add(role);

// Or add it to yourself
message.author.roles.add(role);

Example 2: assign role on reaction by id discord.js

const Discord = require('discord.js');
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] }); //partials arent really needed but I woudld reccomend using them because not every reaction is stored in the cache (it's new in v12)
const prefix = "-";

client.on('messageReactionAdd', async (reaction, user) => {
    if (reaction.partial) { //this whole section just checks if the reaction is partial
        try {
            await reaction.fetch(); //fetches reaction because not every reaction is stored in the cache
        } catch (error) {
            console.error('Fetching message failed: ', error);
            return;
        }
    }
    if (!user.bot) {
        if (reaction.emoji.id == yourEmojID) { //if the user reacted with the right emoji

            const role = reaction.message.guild.roles.cache.find(r => r.id === yourRoleID); //finds role you want to assign (you could also user .name instead of .id)

            const { guild } = reaction.message //store the guild of the reaction in variable

            const member = guild.members.cache.find(member => member.id === user.id); //find the member who reacted (because user and member are seperate things)

            member.roles.add(role); //assign selected role to member

        }
    }
})

Example 3: how to give a role to a new member in discord.js

var role = message.guild.roles.find(role => role.name === "MyRole");
message.member.addRole(role);