My Discord bot is sending multiple messages at once using discord.js

Viewed 3373

For some reason the bot is posting in the discord more than once. I am unsure how to fix this at this moment. I've exhausted all options and even restarted the code and the bot itself and I am not to sure where to go on from here.

You can find my code below:

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

const client = new Discord.client();    
const prefix = '-';

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.once('ready', () => {
    console.log('United 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 === 'youtube'){
        client.commands.get('youtube').execute(message, args);
    } else if (command == 'twitter'){
        message.channel.send('https://twitter.com/UnitedPeoplesTV');
    }
});

youtube.js

module.exports = {
    name: 'youtube',
    description: "displays youtube channel!",
    execute(message, args){
        message.channel.send('https://youtube.com/unitedpeoplestv?sub_confirmation=1');
    }
}
4 Answers

I would suggest that you kick the bot from the server, restart your computer (or stop your hosting service - making sure for example if you're using pm2 that it isnt running multiple instances), and try again. Invite the bot back again from the discord applications webpage once you have completed that.

Make sure there aren't two processes running, Look in task manager and see if there are two Node.js processes running, and if there are then it might be double running. Try ending them and then starting the bot again.

To make sure that there aren't multiple instances, you can reset your token in the discord developer portal inside your application and replace the old token with the new one. After that, you need to restart your bot with the new token.

This will stop all other processes because discord is stopping all connections due to the token reset.

Try adding this line of code:

if (message.author.bot) return;

It check is the author of the message is a bot and returns nothing if it's True.

Related