Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

node.js - Make a bot return several responses over a period of time

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;
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...