Bot responding several times to single command

I’m working on my first Discord bot and running into an issue where it responds multiple times to one command. When someone sends !process with a URL in direct messages, the bot keeps executing the same action over and over instead of just 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("example.com")) {
        var exec = require('child_process').execSync;
        var randomNum = Math.floor((Math.random() * 1000000000) + 1);
        var command = 'wkhtmltopdf ' + link + ' ' + randomNum + '.pdf';

        var settings = {
          encoding: 'utf8'
        }

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

How can I prevent this duplicate execution problem?

looks like the bot might be triggering itself somehow. try adding a simple check for message.author.id === client.user.id and return early if true. also make sure you’re not running multiple instances of your script accidentally - that happened to me once and drove me crazy lol

To avoid multiple responses from your bot, you need to ensure that it does not respond to its own messages. Start your message handler with a check to see if the message author is the bot itself. You can add if (message.author.bot) return; as the first line to prevent an infinite loop caused by the bot responding to its own commands. Additionally, it is a good idea to check for empty messages with if (!message.content) return;. This should resolve the issue effectively.

This happened to me when I started with Discord.js too. One thing others haven’t mentioned - check if you’ve got multiple event listeners running. If your bot initialization runs more than once, you’ll get duplicate message handlers without knowing it. Make sure you’re only calling client.on('message') once in your whole app. Your hosting service or dev environment might also be restarting the script multiple times. I’d add a simple console.log at the start of your message handler to see exactly how many times it triggers. That’ll help you figure out if it’s a self-response issue or multiple listeners problem.