Hello, I create a small twitch bot and I want to execute the following condition only once every 100 seconds for example:
if (message.indexOf("emoji") !== -1) {
client.say(channel, `emoji party`);
}
but as you can see I have other similar conditions so I don't want my whole bot to be paralized for 100 seconds. I want each condition to be time independent
const tmi = require("tmi.js");
const client = new tmi.Client({
options: { debug: true, messagesLogLevel: "info" },
connection: {
reconnect: true,
secure: true
},
// Lack of the identity tags makes the bot anonymous and able to fetch messages from the channel
// for reading, supervison, spying or viewing purposes only
identity: {
username: `${process.env.TWITCH_BOT_USERNAME}`,
password: `oauth:${process.env.TWITCH_OAUTH_TOKEN}`
},
channels: ["channelName"]
});
client.connect().catch(console.error);
client.on("message", (channel, tags, message, self) => {
if (self) return;
if (message.indexOf("emoji") !== -1) {
client.say(channel, `emoji party`);
}
if (message.indexOf("LUL") !== -1) {
client.say(channel, `LUL LUL LUL LUL`);
}
});
Thanks for you help
I think something like that will work for you. Im not sure how to store persistent variable but global
might be ok.
global.lastEmojiPartyMs = null; // lets store last called time (milliseconds)
client.on("message", (channel, tags, message, self) => {
if (self) return;
if (message.indexOf("emoji") !== -1) {
const nowMs = Date.now();
if (
!global.lastEmojiPartyMs ||
nowMs - global.lastEmojiPartyMs >= 100 * 1000 // 100 seconds for example
) {
global.lastEmojiPartyMs = nowMs;
client.say(channel, `emoji party`);
}
}
if (message.indexOf("LUL") !== -1) {
client.say(channel, `LUL LUL LUL LUL`);
}
});