showing results for - "assign role on reaction by id discord js"
Maximiliano
20 Nov 2016
1const Discord = require('discord.js');
2const 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)
3const prefix = "-";
4
5client.on('messageReactionAdd', async (reaction, user) => {
6    if (reaction.partial) { //this whole section just checks if the reaction is partial
7        try {
8            await reaction.fetch(); //fetches reaction because not every reaction is stored in the cache
9        } catch (error) {
10            console.error('Fetching message failed: ', error);
11            return;
12        }
13    }
14    if (!user.bot) {
15        if (reaction.emoji.id == yourEmojID) { //if the user reacted with the right emoji
16
17            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)
18
19            const { guild } = reaction.message //store the guild of the reaction in variable
20
21            const member = guild.members.cache.find(member => member.id === user.id); //find the member who reacted (because user and member are seperate things)
22
23            member.roles.add(role); //assign selected role to member
24
25        }
26    }
27})