Recommand · May 25, 2021 0

Duplication issue in results node.js

I am new to Node.Js and Javascript and I am trying to create a discord bot that assigns roles in a certain discord channel. The main issue right now is that it doesn’t properly add the reaction to the embedded message and then the main weird issue is that with every test (i.e inputting node . ) in terminal and trying the command (-reactionrole) it begins to duplicate it’s response (shown in the screenshot below) sometimes 2 times, then 3 then 4 and so on in a weird loop. I figure it’s something to do with if statements I made waiting to fetch the data

Here is my main.js

const Discord = require('discord.js');

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});

const prefix = '-';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
 
    client.commands.set(command.name, command);
}
 
 
client.on('ready', () => {
    console.log('bot is online!');
});
 
 
client.on('message', message => {
 
    if (!message.content.startsWith(prefix) || message.author.bot) return;
 
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
    if (command === 'reactionrole') {
        client.commands.get('reactionrole').execute(message, args, Discord, client);
    } 
  
});
 



client.login('TOKEN');

and here in the commands folder I have the reactionrole.js code

module.exports = {
    name: 'reactionrole',
    description: "Sets up a reaction role message!",
    

    
    async execute(message, args, Discord, client) {
        const channel = '845773342551048283';

        const aguRole = message.guild.roles.cache.find(role => role.name === "Agu");

        const aguEmoji = ':sunshine:';
        


        let embed = new Discord.MessageEmbed()
        .setColor('#e42643')
        .setTitle('Choose the role!')
        .setDescription('How does your commandant look!\n\n'
            + `${aguEmoji} for agu team\n`);
            


            let messageEmbed = await message.channel.send(embed);

            messageEmbed.react(aguEmoji);


            client.on('messageReactionAdd', async (reaction, user) => {
                
                if (reaction.message.partial) await reaction.message.fetch();
                if (reaction.partial) await reaction.fetch();
                if (user.bot) return;
                if (!reaction.message.guild) return;

 
                if (reaction.message.channel.id == channel) {
                    if (reaction.emoji.name === aguEmoji) {
                        await reaction.message.guild.members.cache.get(user.id).roles.add(aguRole);
                        message.react(aguRole);
                    }
                } else {
                    return;
                }
     
            });
     
            client.on('messageReactionRemove', async (reaction, user) => {
     
                if (reaction.message.partial) await reaction.message.fetch();
                if (reaction.partial) await reaction.fetch();
                if (user.bot) return;
                if (!reaction.message.guild) return;
     
     
                if (reaction.message.channel.id == channel) {
                    if (reaction.emoji.name === aguEmoji) {
                        await reaction.message.guild.members.cache.get(user.id).roles.remove(aguRole);
                    }
                } else {
                    return;
                }
            });







            
    }

}

here is the duplicated responses (no roles)
H As you can see there is no reaction being added. Not sure why

Appreciate any help.