0
votes

Currently, the user is able to choose as many roles given within the section, however, I would like to be able to do something along the lines of: (roleA OR roleB) AND (roleC OR roleD) AND roleE etc

The above is all meant to be triggered by checking what reactions they've already submitted and removing them if the new selection contradicts the current.

Users are able to apply reaction roles to themselves by reacting to a specified message. Depending on whether a certain choice is made, it's to be able to add/remove from another role i.e

How old are you? - 18-24 - 25-30 - 31+

What's your Gender? - Male - Female

What Continent are you in? - IDK, I'm running out of - Bogus Questions - To fill in space.

if user clicks, 25-30, but then realises they're 24, and click that instead, I'd like the prior reaction & role to be removed without manual interference required. Not only 1 option will be available, so would like to have multiple selections available as well.

bot.on("raw", event =>{
    console.log(event);
    const eventName = event.t;
    if(eventName === 'MESSAGE_REACTION_ADD')
    {
        if(event.d.message_id === '<REMOVED ID>')
        {
            var reactionChannel = bot.channels.get(event.d.channel_id);
            if(reactionChannel.messages.has(event.d.message_id))
                return;
            else {
                reactionChannel.fetchMessage(event.d.message_id)
                .then(msg => {
                    var msgReaction = msg.reactions.get(event.d.emoji.name + ":" + event.d.emoji.id);
                    var user = bot.users.get(event.d.user_id);
                    bot.emit('messageReactionAdd', msgReaction, user);
                 })
                .catch(err => console.log(err));
            }
        }
    }
    else if (eventName === 'MESSAGE_REACTION_REMOVE')
    {
        if(event.d.message_id === '<REMOVED ID>')
        {
            var reactionChannel = bot.channels.get(event.d.channel_id);
            if(reactionChannel.messages.has(event.d.message_id))
                return;
            else{
                reactionChannel.fetchMessage(event.d.message_id)
                .then(msg => {
                    var msgReaction = msg.reactions.get(event.d.emoji.name + ":" + event.d.emoji.id);
                    var user = bot.users.get(event.d.user_id);
                    bot.emit('messageReactionRemove', msgReaction, user);
                 })
                .catch(err => console.log(err));
            }
        }
    }
});

bot.on('messageReactionAdd', (messageReaction, user) => {
    var roleName = messageReaction.emoji.name;
    var role = messageReaction.message.guild.roles.find(role => role.name.toLowerCase() === roleName.toLowerCase());



    if(role)
    {
        var member = messageReaction.message.guild.members.find(member => member.id === user.id);

        if(member)
        {
            member.addRole(role.id);
            console.log('Success. Added role.');
        }
    }
});

bot.on('messageReactionRemove', (messageReaction, user) => {
    var roleName = messageReaction.emoji.name;
    var role = messageReaction.message.guild.roles.find(role => role.name.toLowerCase() === roleName.toLowerCase());

    if(role)
    {
        var member = messageReaction.message.guild.members.find(member => member.id === user.id);
        if(member)
        {
            member.removeRole(role.id);
            console.log('Success. Removed role.');
        }
    }
});```
1

1 Answers

1
votes

In your messageReactionAdd event, you can try to find a reaction or role applied by/to the user that corresponds with a specific choice. If there is one, that means they had already chose that answer. You can then remove them before adding the new role to the user. If not, the code should continue as usual.

bot.on('messageReactionAdd', async (messageReaction, user) => { // async needed for 'await'
  const name = messageReaction.emoji.name.toLowerCase();
  const message = messageReaction.message;

  const role = message.guild.roles.find(role => role.name.toLowerCase() === name);
  const member = message.guild.member(user);

  if (!role || !member) return;

  const emojis = message.reactions.map(emoji => emoji.name);

  const conflictingReaction = message.reactions.find(reaction => reaction.users.get(user.id) && emojis.includes(reaction.emoji.name));
  const conflictingRole = member.roles.find(role => emojis.includes(role.name.toLowerCase());

  try {
    if (conflictingReaction || conflictingRole) {
      await conflictingReaction.remove(user);
      await member.removeRole(conflictingRole);
    }

    await member.addRole(role);

    console.log('Success.');
  } catch(err) {
    console.error(err);
  }
});