Discord bot duplicating responses when using discord.js library

I’m having trouble with my Discord bot because it keeps posting the same response multiple times when users run commands. This started happening recently and I can’t figure out what’s causing it. I’ve tried restarting both my code and the bot but the problem persists.

Here’s my main bot file:

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

const bot = new Discord.Client();
const commandPrefix = '!';

bot.commands = new Discord.Collection();

const cmdFiles = fs.readdirSync('./handlers/').filter(file => file.endsWith('.js'));

for (const file of cmdFiles){
    const cmd = require(`./handlers/${file}`);
    bot.commands.set(cmd.name, cmd);
}

bot.once('ready', () => {
    console.log('Bot is running');
});

bot.on('message', msg =>{
    if(!msg.content.startsWith(commandPrefix) || msg.author.bot) return;
    
    const arguments = msg.content.slice(commandPrefix.length).split(/ +/);
    const cmd = arguments.shift().toLowerCase();
    
    if(cmd === 'channel'){
        bot.commands.get('channel').execute(msg, arguments);
    } else if (cmd == 'social'){
        msg.channel.send('https://twitter.com/example');
    }
});

And here’s my command handler file (channel.js):

module.exports = {
    name: 'channel',
    description: "shows our main channel",
    execute(msg, arguments){
        msg.channel.send('https://youtube.com/example?sub_confirmation=1');
    }
}

Any ideas what might be causing the duplicate messages?

I had this issue too once. Make sure you don’t have multiple processes running. Sometimes just restarting everything helps. Also, check if you’re sending messages in a loop by accident.

The issue is probably how you’re loading command files. You’re using fs.readdirSync('./handlers/') and setting up commands in a collection, but then hardcoding command execution in your message event - that’s likely causing conflicts. I’ve seen this when bots get registered multiple times or have leftover event listeners. Try adding bot.removeAllListeners() before your event handlers and make sure you’re only calling bot.login() once at the end of your main file. Also check for any setInterval or recurring functions that might trigger message sends. Since this started recently, something probably changed in your hosting environment or you’ve got another instance still running.

This happens when you’ve got multiple event listeners for the same event. Check if your bot script is running more than once or if there’s another instance still going in the background. I’ve hit this before - turned out I’d started the bot twice without killing the first process. Check your task manager or run ps aux | grep node on Linux to see if you’ve got multiple node processes running your bot. Also, don’t call bot.login() more than once in your code - that creates duplicate event handlers too.