leralins
leralins

Reputation: 21

Discord.js: only one reaction

I'm creating Discord bot which gives a specific role when users add a specific emoji reaction on a message.

I want to let users choose only one reaction;

or

if the user choose another one, other reactions of the same user should be removed.

I found code only for two situation:

  1. Remove all emoji reactions on a message.
    message.reactions.cache.get('emojiId').remove()
  1. Remove the reaction from the user.
    const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(userId));
try {
    for (const reaction of userReactions.values()) {
        await reaction.users.remove(userId);
    }
} catch (error) {
    console.error('Failed to remove reactions.');
}

Both of them doesn't suit me because they delete all reactiosn to the message.

A part of my code:

client.on('messageReactionAdd', async (reaction, user) => {
    if(reaction.partial) await reaction.fetch()
    const { message } = reaction
    if (message.id === rulesMessageId) {
        const member = message.channel.guild.members.cache.get(user.id)
        if (reaction.emoji.id === emojiIds.HEAVEN ) {
          for (var i = 0 in guildRoles) {
            if (guildRoles[i] != guildRoles.HEAVEN)
            member.roles.remove(guildRoles[i])
          }
          member.roles.add(guildRoles.HEAVEN)   
        }

Upvotes: 2

Views: 3699

Answers (1)

BlueEmperor
BlueEmperor

Reputation: 21

I don't know how to use a part of your code in the answer so I have do my own :

client.on("messageReactionAdd", (reaction, Member) => {
    if(Member.bot) return;
    reaction.message.reactions.cache.map(x=>{
        if(x._emoji.name != reaction._emoji.name&&x.users.cache.has(Member.id)) x.users.remove(Member.id)
    })
});

I get all reaction in the message and map them to check if the user have react. I also check if the reaction is the same as the one currently reacting to remove only the others. Hope it will help you even if it's 10 months too late ^^

Upvotes: 2

Related Questions