title: My discord.js bot seems to be running multiple instances at the same time
body: <p>So what I'm trying to do is a simple bot that can detect commands. I made a '!test' command that does a few things (replies to the message, deletes it and then deletes the answer later, but also logs the command in a channel).</p>
  
  <p>It seems to work fairly well, however it spams the answer and executes the series of actions multiple times : <a href="http://prntscr.com/nkgj8m" rel="nofollow noreferrer">http://prntscr.com/nkgj8m</a> (more and more every time when I restart the bot).</p>
  
  <p>I tried to delete the app and recreate it, and it worked well : the message showed up only once, until I restarted the bot.</p>
  
  <p>I made a '!stop' command that destroys the client, but it doesn't work as intended : the bot is disconnected (says "stop" in my console), but almost instantly reconnects on my server (and I don't see the logs in my local console anymore).</p>
  
  <p>It seems to be a little "random" though about the count of messages. Some bot messages are also sometimes not deleted at all, and are not logged as well.</p>
  
  <p>Here's my code (I never really did anything in js before so I might misuse some things, or some things might be not optimal, sorry about that - I did some research and most things I think are good, or decent at least).</p>
  
  <pre class="lang-js prettyprint-override"><code>// Require libs
  require('dotenv').config()
  const Discord = require('discord.js');
  
  // Get discord client
  var client = new Discord.Client();
  initialize();
  
  // --------------------------------------------------
  //      INITIALIZE
  // --------------------------------------------------
  
  function initialize() {
      // On ready
      client.on("ready", function() {
          console.log(`Logged in as ${client.user.tag}! Yup, this is the default message.`);
      });
  
      // On message
      client.on("message", function(input) {
          // server message
          if (input.guild.available) {
              // get the message content
              var command = input.content.toLowerCase();
              // stop command
              if (command.startsWith("!stop")) {
                  client.destroy();
                  console.log("Stopped");
              }
              // test command
              else if (command.startsWith("!test")) {
                  input.reply(`This is my answer to your test !`)
                      .then(function(output) {
                          consumeCommand(input, output, 5000);
                      })
                      .catch(console.error);
              }
          }
      });
  
      // login bot client
      client.login(process.env.BOT_TOKEN);
  }
  
  // --------------------------------------------------
  //      CONSULE AND LOG COMMANDS
  // --------------------------------------------------
  
  // Log the output of a command, delete the input message and delete the output soon
  // input, message, the user message
  // output, string, is the bot output
  // outputTimeout, int, is the time we should wait until deleting the bot's output
  function consumeCommand(input, output, outputTimeout) {
      // delete input
      input.delete(0)
          .then(function() {
              console.log(`Deleted message ${input.content}`)
          })
          .catch(console.error);
      // log
      var logChannel = input.guild.channels.find(channel =&gt; channel.name === 'guiguibot-commands');
      if (logChannel != null) {
          logCommand(input, output, logChannel);
      } else {
          console.log("Trying to log bot command but there's no guiguibot-commands channel");
      }
      // delete output later if not null
      if (output != null &amp;&amp; outputTimeout != null) {
      }
  }
  
  // Log the output of a command
  // input, message, the user message
  // msg, message, the user message
  // output, string, is the bot output
  function logCommand(input, output, logChannel) {
      // has output
      if (output != null) {
          logChannel.send(`@${input.author.username} sent a command`, {
            embed: {
              fields: [
                {
                  name: ":keyboard: Input :",
                  value: `\`${input.content}\``
                },
                {
                  name: ":robot: Output :",
                  value: `\`${output.content}\``
                }
              ]
            }
          })
              .then(() =&gt; console.log('Logged user action'))
              .catch(console.error);
      }
      // no ouput
      else {
          logChannel.send(`@${input.author.id} sent a command (no output was found)`, {
            embed: {
              fields: [
                {
                  name: ":keyboard: Input :",
                  value: `\`${input.content}\``
                }
              ]
            }
          })
              .then(function() {
                  console.log('Logged user action')
              })
              .catch(console.error);
      }
  }
  </code></pre>
  
  <p>So, my question would be : how do I make sure that there's only one instance of my code running ? (if I deducted the problem correctly). Any help is appreciated. Thanks!</p>