0

I want to set a command that returns 3 or 4 replies over about 5 minutes. This is something I found that is I tried to use but it just hangs up in terminal and won't return the "Ready" indicator.

let botLastSent = false;
let timeBetweenEachCmd = 60000 * 3; //Bot will only respond once every 3 minutes.

client.on('message', message => {
    if (message.author.bot) return undefined;
    if (message.content === '!french')

    channel = client.channels.cache.get('766344174379204622');
            
    channel.send('(。··)_且 would you like a fresh french press sir?(。··)_且'); 
    (botLastSent !== false ? message.createdTimestamp - botLastSent < timeBetweenEachCmd : false);

    channel = client.channels.cache.get('766344174379204622');
    channel.send('(。··)_且 would you like some milk cubes sir?(。··)_且');

    (botLastSent !== false ? message.createdTimestamp - botLastSent < timeBetweenEachCmd : false);

    channel = client.channels.cache.get('766344174379204622');
    channel.send('(。··)_且 how about some local honey with that sir?(。··)_且');

    (botLastSent !== false ? message.createdTimestamp - botLastSent < timeBetweenEachCmd : false);

    channel = client.channels.cache.get('766344174379204622');

    channel.send('(。··)_且 now for the perfect cup of coffee sir...(。··)_且'); 

    botLastSent = message.createdTimestamp;
});
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
TOKI
  • 33
  • 11
  • Does this answer your question? https://stackoverflow.com/questions/14249506/how-can-i-wait-in-node-js-javascript-l-need-to-pause-for-a-period-of-time?rq=1 – Xeoth Jan 08 '21 at 18:42

1 Answers1

0
client.on('message', message => {
    if (message.author.bot) return;
    if (message.content.startsWith('!french')) {
       let channel = client.channels.cache.get('766344174379204622');
       let responses = ['(。··)_且 would you like a fresh french press sir?(。··)_且', '(。··)_且 would you like some milk cubes sir?(。··)_且', '(。··)_且 how about some local honey with that sir?(。··)_且', '(。··)_且 now for the perfect cup of coffee sir...(。··)_且'];
       let i = 0;
       setInterval(function() {
         if(i === responses.length-1) clearInterval(this);
         channel.send(responses[i]);
         i++;
       }, 60000);
    }
});

This will send each response with a 60 second delay. You can change the time to whatever you prefer. Ok, so what have I done? At first I defined the channel, where I would like to have the responses in (it is the channel ID from your question right now). Then I defined an array with your responses inside it. Then I created i with the value 0, because an array always starts at index 0. Then I used setInterval, which takes in a function and a duration in ms. Inside the function it checks if i is equal to the last index of responses. responses.length is the length of the array, in this case it has a length of 4. But as we want to check if i is equal to the last index we have to use responses.lenght-1, this will be 3. So if i is equal to 3, the interval gets cleared and the responses don't get send anymore. Otherwise if i is not equal to 3, it will send the responses at the index i to your channel and count up i by one (i++).

For example:
The first run would be: i == 0, so it sends the first response (responses[0]) to your channel. i gets count up and is now 1. It will wait 60 seconds and will repeat the same procedure.

Jannik Schmidtke
  • 1,257
  • 2
  • 5
  • 15
  • Thank you stranger. It works; but it sends 3 at once and then waits a minute to send the final one. I will see if I can problem solve how to get it to spread out evenly over time. I've always struggled with dreaming up the functions with i where i counts and helps the function progress. Thank you very much! – TOKI Jan 08 '21 at 22:52
  • For me it works without any problems and it should work in general. It waits 60s then sends the first one, waits again 60s sends the second one and so on. – Jannik Schmidtke Jan 08 '21 at 23:11
  • Got it! It was because I repeated client.on('message', message => { twice. Now it works like a charm. Thanks again for being the brains behind the operation. – TOKI Jan 08 '21 at 23:20