Search code examples
javascriptdiscorddiscord.jsbots

How do I add a stop process command to my bot, which ends the on-going command? (discord.js)


I want to add a joke command to my Discord bot, something like bt!donotusethis which will then answer with the command itself, causing an infinite spam chain.

Obviously, this needs a command to stop it. I want to add a command such as bt!stop, which will end the spam.

Is there any way to add this?

I tried using this:

   client.on("messageCreate", (message) => {
     if (message.content.startsWith("bt!donotusethis")) {
       message.channel.send("bt!donotusethis");
     }
   });

   client.on("messageCreate", (message) => {
     if (message.content.startsWith("bt!stop")) {
       //this is where the end process command goes
     }
   });

And instead of the //this is where the end process command goes I added client.process.end which did stop the spam, but also crashed the bot itself.


Solution

  • The client.process.end is not a function of Discord.js that's why the bot crashes. If you want to stop the bot, you can add a spamming variable with the data "false". When the command is run, you can set it to true and when the other command is run you can set it to false. And also remove multiple messageCreate event

    Here's a code sample:

    let spamming = false;
    
    client.on("messageCreate", (message) => {
      if (message.content.startsWith("bt!donotusethis")) {
        if (!spamming) {
          spamming = true;
          message.channel.send("bt!donotusethis");
        } else {
          message.channel.send("bt!donotusethis");
        }
      } else if (message.content.startsWith("bt!stop")) {
        spamming = false;
      }
    });