Skip to content

Instantly share code, notes, and snippets.

@inooid
Created April 28, 2017 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inooid/5277c45c6c7670d71bfdd595babc7d8c to your computer and use it in GitHub Desktop.
Save inooid/5277c45c6c7670d71bfdd595babc7d8c to your computer and use it in GitHub Desktop.
Twitch bot command cooldown concept
// Warning: Keep in mind that this example has a lot of boilerplate,
// because I want to make sure it's as easy to grasp as possible.
// In a real application you would probably want to wrap the cooldown
// check in a higher order function that easily creates a command and
// checks the cooldowns for you.
// The cooldown manager example
var CooldownManager = {
cooldownTime: 30000, // 30 seconds
store: {
'!comm1': 1493389555431,
},
canUse: function(commandName) {
// Check if the last time you've used the command + 30 seconds has passed
// (because the value is less then the current time)
return this.store[commandName] + this.cooldownTime < Date.now();
},
touch: function(commandName) {
// Store the current timestamp in the store based on the current commandName
this.store[commandName] = Date.now();
}
}
// Your bot
Bot.on('chat', function(msg) {
if (msg.indexOf('!comm1') > -1) {
if (CooldownManager.canUse('!comm1')) {
bot.send('#channel', 'example');
CooldownManager.touch('!comm1');
}
return;
}
if (msg.indexOf('!comm2') > -1) {
if (CooldownManager.canUse('!comm2')) {
bot.send('#channel', 'example2');
CooldownManager.touch('!comm2');
}
return;
}
});
@marcosrocha85
Copy link

Many thanks. Although, how can we implement Global and User cooldown like Streamlabs does?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment