user7509866
user7509866

Reputation:

How to add Sleep in Node.js after executing command?

Basically this is a chat bot for Twitch.tv. Currently, it replies to someone in the chat when they say "Hello". I'd like to add a Sleep for 2 seconds after the bot replies with hello, so it doesn't flood the chat. I have tried setTimeout/setInterval but those put the 2 second delay BEFORE it replies to the person. Cheers.

var tmi = require('tmi.js');

process.setMaxListeners(0);

var options = {
options: {
    debug: true
},
connection: {
    cluster: "aws",
    reconnect: true
},
identity: {
    username: "",
    password: ""
},
channels: [""]
};

var client = new tmi.client(options);
client.connect();

client.on('chat', function(channel, user, message, self) {
if(message === "Hello") {
    client.action("", "@" + user['display-name'] + ", Welcome!");
}});

Upvotes: 0

Views: 446

Answers (1)

Pascal
Pascal

Reputation: 122

var canSendMessage = true;
client.on('chat', function(channel, user, message, self) {
if(message === "Hello" && canSendMessage ) {
canSendMessage = false;
client.action("", "@" + user['display-name'] + ", Welcome!");
setTimeout(function(){ canSendMessage = true }, 2000);
}});

You can try this (Not tested). The variable canSendMessage will block the bot from writing if it is not reseted after 2000ms (2s).

Upvotes: 1

Related Questions