Nico Leon W.
Nico Leon W.

Reputation: 41

How to role check with Role Reaction & Auto remove user Reaction

Hey I've gotten my role reaction so far but would like to make it so that after pressing the emoji the code asks if someone already has the group or not and accordingly adds or removes the group but I would also like to make it so that the reaction with the emoji is removed automatically so that only the bot reacts to the message that emoji is displayed.

I will add the code I have written so far in the attachment and maybe there are some people who can help me.

MessageReactionAddEvent:

const BaseEvent = require('../utils/structures/BaseEvent');
require('dotenv').config();
module.exports = class MessageReactionAddEvent extends BaseEvent {
  constructor() {
    super('messageReactionAdd');
  }
  
  async run(client, reaction, user) {
    const message = reaction.message;
    const member = message.guild.members.cache.get(user.id);

    if(user.bot) return;
 
    const Valorant = message.guild.roles.cache.get(process.env.DISCORD_ROLE_VALORANT);
    const League = message.guild.roles.cache.get(process.env.DISCORD_ROLE_LEAGUE);

    if(
      ["🔫", "💥"].includes(reaction.emoji.name)
  ) {
      switch(reaction.emoji.name) {

          case"🔫":
          if (message.member.roles.cache.has(process.env.DISCORD_ROLE_VALORANT)) {
            console.log('Has Valorant')
          } else {
            member.roles.add(Valorant)
            member.createDM().then( channel => {
                channel.send("Added Valorant")
            })
          }
          
          break;

          case"💥":
          if (message.member.roles.cache.has(process.env.DISCORD_ROLE_LEAGUE)) {
            console.log('Has League of Legends')
          } else {
            member.roles.add(League)
            member.createDM().then( channel => {
                channel.send("Added League of Legends")
            })
          }
      }
    }
  }
}

MessageReactionRemoveEvent:

const BaseEvent = require('../utils/structures/BaseEvent');
require('dotenv').config();
module.exports = class MessageReactionRemoveEvent extends BaseEvent {
  constructor() {
    super('messageReactionRemove');
  }
  
  async run(client, reaction, user) {
    const message = reaction.message;
    const member = message.guild.members.cache.get(user.id);

    if(user.bot) return;
 
    const Valorant = message.guild.roles.cache.get(process.env.DISCORD_ROLE_VALORANT);
    const League = message.guild.roles.cache.get(process.env.DISCORD_ROLE_LEAGUE);
 
    if(
        ["🔫", "💥"].includes(reaction.emoji.name)
    ) {
        switch(reaction.emoji.name) {
 
          case"🔫":
          if (message.member.roles.cache.has(process.env.DISCORD_ROLE_VALORANT)) {
            member.roles.remove(Valorant)
            member.createDM().then( channel => {
                channel.send("Added Valorant")
            })
          } else {
            console.log('Hasn´t Valorant')
          }
          
          break;

          case"💥":
          if (message.member.roles.cache.has(process.env.DISCORD_ROLE_LEAGUE)) {
            member.roles.remove(League)
            member.createDM().then( channel => {
                channel.send("Added League of Legends")
            })
          } else {
            console.log('Hasn´t League of Legends')
          }
      }
    }
  }
}

RolesCommand:

const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require("discord.js")

module.exports = class RolesCommand extends BaseCommand {
  constructor() {
    super('roles', 'Team', []);
  }

  run(client, message, args) {
    try {
      message.delete()

      const embed = new Discord.MessageEmbed()
      .setTitle("Test1")
      .setDescription("Test2")
      .setColor("#FCCF00")
      .setFooter("Test3")

      message.channel.send(embed).then(async msg => {
          await msg.react("🔫")
          await msg.react("💥")
      })

  } catch(e) {
    console.log(e)
    } 
  }
}

Thanks for all the help I get so far the community has already helped me very well - thanks for that.

Upvotes: 1

Views: 750

Answers (2)

Nico Leon W.
Nico Leon W.

Reputation: 41

With a lot of research and a friend who helped me I have now got it right and hope I can help everyone else who has the same problem.

Role Check with Role Reaction:

- message.member.roles.cache.has(process.env.DISCORD_ROLE_VALORANT)
+ member.roles.cache.has(process.env.DISCORD_ROLE_VALORANT)

Auto remove user Reaction:

const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
try {
    for (const reaction of userReactions.values()) {
        await reaction.users.remove(user.id);
    }
} catch (error) {
    console.error('Failed to remove reactions.');
}

Upvotes: 2

Tenclea
Tenclea

Reputation: 1464

I suppose that running reaction.remove(); before your switch statements should do the trick :)

Upvotes: 0

Related Questions