4

I've tried searching for other examples, to fix this issue by myself but I'm fairly new to coding in general and I'm very new to java script so I apologise in advance for any silly mistakes I make.

Basically, I am learning javascript and I thought that a good interactive way of learning would be to make a discord bot so I can "see" my effort paying off and therefore keep up my motivation. I decided a basic spam bot would be a good starting point to get me familiar with the most basic aspects.

After some research I found the method "setInterval" which seemed perfect for the application I had in mind. This method will execute a line of code every given time interval.

So I can get my bot to spam a discord channel just fine, but this issue im having is if I want to get it to stop.

client.on('message', message => {           //reacting when ever the 'message' EVENT occurs (e.g. a message is sent on a text channel in discord)

console.log('A message was detected and this is my reaction');
console.log(message.author + ' also knows as ' + message.author.username + ' said:\t' + message.content);       //message.author is the value of the person who sent the message, message.content is the content of the message


if(message.author.bot) {
    return null                 //returns nothing if the message author is the bot


} else if (message.content.startsWith(`${prefix}spam`)) {
    
    let timerId = setInterval(() => {                                           //starts to spams the channel
        message.channel.send('spamtest');
    }, 1500);   


} else if (message.content.startsWith(`${prefix}stop`)) {

    clearInterval(timerId);
    message.channel.send('condition met');

}

});

The error I get here is that timerId is not defined. So I thought thats because its a local variable, and now im stumped. I dont know what else to try and ive been getting quite frustrated over something this simple, so I was hoping that someone here could help.

Thanks

Rene Pot
  • 24,681
  • 7
  • 68
  • 92
coxie1102
  • 43
  • 3

1 Answers1

2

As stated by Jaromanda X in the comments, the let keyword declares a variable in the current block scope, making the variable inaccessible to the other block scope (the other else if block).

To fix this, you need to declare the variable timerId in the global scope, so that it is accessible by all other block scopes:

let timerId; // declare timer in global scope

client.on('message', message => {           //reacting when ever the 'message' EVENT occurs (e.g. a message is sent on a text channel in discord)

console.log('A message was detected and this is my reaction');
console.log(message.author + ' also knows as ' + message.author.username + ' said:\t' + message.content);       //message.author is the value of the person who sent the message, message.content is the content of the message


if(message.author.bot) {
    return null                 //returns nothing if the message author is the bot


} else if (message.content.startsWith(`${prefix}spam`)) {
    
    timerId = setInterval(() => {                                           //starts to spams the channel
        message.channel.send('spamtest');
    }, 1500);   


} else if (message.content.startsWith(`${prefix}stop`)) {

    clearInterval(timerId);
    message.channel.send('condition met');

}
Daemon Beast
  • 2,794
  • 3
  • 12
  • 29