0
votes

I have a code that allows a user to add a reaction to get a role. However, I want my bot to remove the role if a reaction is removed, and I don't know how to do that. Here is my current code.

const Discord = require('discord.js');
const keepAlive = require('./server');
const client = new Discord.Client();

client.on('message', message => {
    if (message.author !== null && message.author.bot) return;
    if (message.toString().toLowerCase().includes('start reaction roles')) {
        message.delete()
        const embed = new Discord.MessageEmbed()
            .setColor('#0099ff')
            .setTitle('React to get notified')
            .setDescription(`Hello! Please react to this message to get a role. Here are the roles that you can get by reacting:\n> ???? | React with this emoji to get <@&727665971443269635> notifications.\n> ⚠️ | React with this emoji to get <@&727633811709362187>.\nYou can chose one of the emojis, or both of them.\n\n**NOTE:**\nCurrently, this command does not support removing your roles. If you want to remove your role, click on the emoji. Then, click on your profile, and click the X next to the role you want to remove. You can always get it back by reacting.`)
        message.channel.send(embed).then(sentMessage => {
            sentMessage.react("????").then(() => sentMessage.react("⚠️")).then(() => {
                const filter = (reaction, user) => {
                    return true;
                }
                const collector = sentMessage.createReactionCollector(filter);
                collector.on('collect', (reaction, user) => {
                    if (reaction.emoji.name === '????'){
                        role = message.guild.roles.cache.find(role => role.name === "ne1 here");
                        message.member.roles.add(role);
                    }
                    else if (reaction.emoji.name === '⚠️'){
                        role = message.guild.roles.cache.find(role => role.name === "HR notifications");
                        message.member.roles.add(role);
                    }
                })
            })
            .catch(() => console.error('One of the emojis failed to react.'));
        })
    }
});

keepAlive();
client.login(process.env.TOKEN);

If someone could tell me how to do this, that would be great.

1
You'll have to listen for the messageReactionRemove event and use partials to fetch the message where the reaction was removed. discordjs.guide/popular-topics/partials.html github.com/discordjs/discord.js/blob/master/docs/topics/…aquamaru

1 Answers

0
votes

I don't know how to do this with only discord.js, couldn't find anything about this in de documentation. But you could try to use this npm module. And use the documentation of the npm module. I think that is faster and easier.

Hope I helped.