discord add role to user djs code example
Example 1: 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 2: add role to channel discord.js
channel.permissionOverwrites.get(message.author.id).delete();