Created
April 28, 2017 14:36
-
-
Save inooid/5277c45c6c7670d71bfdd595babc7d8c to your computer and use it in GitHub Desktop.
Twitch bot command cooldown concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Many thanks. Although, how can we implement Global and User cooldown like Streamlabs does?