discord.js reaction role code example
Example 1: get discord.js role
let role = message.guild.roles.cache.find(r => r.id === "Role ID");
let member = message.mentions.members.first();
member.roles.add(role);
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'] });
const prefix = "-";
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
console.error('Fetching message failed: ', error);
return;
}
}
if (!user.bot) {
if (reaction.emoji.id == yourEmojID) {
const role = reaction.message.guild.roles.cache.find(r => r.id === yourRoleID);
const { guild } = reaction.message
const member = guild.members.cache.find(member => member.id === user.id);
member.roles.add(role);
}
}
})
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);