Bot responds several times to single command

I’m working on my first Discord bot and running into an issue. When someone sends a direct message with !process followed by a URL, my bot keeps responding multiple times instead of just once. The command seems to trigger repeatedly even though the user only sent it once.

client.on('message', message => {
  if (message.content.startsWith("!process")) {
    var parts = message.content.split(" ");
    if (parts[1] != undefined) {
      var link = parts[1];
      if (link.includes("youtube.com")) {
        var execSync = require('child_process').execSync;
        var randomNum = Math.floor((Math.random() * 999999999) + 1);
        var command = 'youtube-dl ' + link + ' -o ' + randomNum + '.mp4';

        var settings = {
          encoding: 'utf8'
        }

        var outputFile = randomNum + '.mp4';
        console.log(execSync(command, settings));
        message.reply({ files: [outputFile] });
      }
    }
  }
});

Any ideas why this duplicate response issue happens?

Another thing - your message event listener might be getting registered multiple times if you’re reloading code without cleaning up properly. This happened to me when I was developing my bot with hot reload. Each time the handler gets registered again, you end up with multiple listeners responding to the same message. Add some debug logging right at the start of your message handler to see how many times it fires. You might be surprised it’s executing more than expected for a single message.

yep, looks like your bot is replying to its own msgs. just add if (message.author.bot) return; at the top of the handler to ignore bot messages. that should do the trick!

Check if you’ve got multiple bot instances running at once. I ran into this exact issue when testing - accidentally started my bot twice from different terminals. Both instances responded to the same command, creating what looked like duplicate responses. Kill all your node processes and make sure only one’s running. Also check if you’re using nodemon or similar tools that might be auto-restarting your bot while you’re coding.