-1

Hi I’ve been working on a bot and wanted to make a cooldown for the user for 10 mins if they use the command again I created this system that works as a cooldown but for all users how would I make it for only the user of the command and have it be individual.

if(message == '!joinqueue') {

    if(!block) {
        
        client.say(channel, `@${user.username}, Joined The Queue!`);
    
        queue.push(`${user.username}`);

        console.log(queue);

        block = true
        setTimeout(() => {
            block = false;
        }, (60 * 10000))
    }
    else {
        client.say(channel, `@${user.username} Please Wait Before Doing This Command Again`)
    }
Zoren Singh
  • 310
  • 2
  • 8
  • It looks like you haven't tried anything. One way to do it would be to change `block` from a boolean to an array of strings, where the strings are user unique identifiers. When you want to block a user, you push that user's unique `id` into the array. When you want to check if a user is blocked, you check if the array has the `id`. And in the timeout which unsets the block, you remove the user's id from the array. – tao Aug 23 '21 at 18:45
  • @tao I am generally new to Javascript I came up with a way I think maybe easier than if they are already in the queue then they cannot rejoin until they have left or have passed the first spot I am still generally confused do you have any recommendations on how I would do this – Zoren Singh Aug 23 '21 at 19:48
  • Regardless of the task at hand, and regardless of your skill in any given programming language, you are faced with two options: a) code an attempt or b) ask others to code it for you, without even trying. When you choose a) you are a coder and your question is welcome here. When you choose b) you are a client and your question is technically *off-topic* here. Most people here will help beginners and seasoned coders alike, as long as the question shows a decent amount of effort at coding or researching the task at hand. – tao Aug 23 '21 at 21:47
  • Please read [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – tao Aug 23 '21 at 21:53

1 Answers1

0

Here's a version which would store the users inside block, (as described in my initial comment above) based on the code you've shown:

let block = [];

if(message === '!joinqueue') {
  if(!block.includes(user.username)) {
    client.say(channel, `@${user.username}, Joined The Queue!`);
    queue.push(user.username);
    block.push(user.username);
    setTimeout(() => {
        block = block.filter(u => u !== user.username);
    }, (60 * 10000))
  } else {
    client.say(channel, `@${user.username} Please Wait Before Doing This Command Again`)
  }
}

Notes:

  • the above will only work as expected if username is a unique user identifier; if it's not, use another field which is a unique identifier; if you don't have one, add one to each user (you could use uuid for this)
  • if the above code doesn't work, you'll need to provide more details about the context in which you're running it, ideally creating a mcve.
  • if your code is placed inside a function which gets run repeatedly (on some hook), it's important to initialize the block variable outside of that function. Otherwise you'll re-init it every time the function runs, effectively losing the current state.
tao
  • 82,996
  • 16
  • 114
  • 150