From 54746625fd0ab5e346aea445469bd254fcb94b98 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Sun, 4 Sep 2011 15:39:03 +0100 Subject: [PATCH] Timers and an ~rmlast command to remove the last quote. --- modules/quotes.js | 22 ++++++++++++++++++++++ run.js | 14 +++++++------- timer.js | 19 +++++++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 timer.js diff --git a/modules/quotes.js b/modules/quotes.js index 0f36325..2050ec3 100644 --- a/modules/quotes.js +++ b/modules/quotes.js @@ -1,5 +1,7 @@ var quotes = function(dbot) { var quotes = dbot.db.quoteArrs; + var addStack = []; + var rmAllowed = true; var commands = { '~q': function(data, params) { @@ -14,6 +16,21 @@ var quotes = function(dbot) { } }, + '~rmlast': function(data, params) { + if(rmAllowed == true) { + var last = addStack.pop(); + if(last) { + quotes[last].pop(); + rmAllowed = false; + dbot.say(data.channel, 'Last quote removed from ' + last + '.'); + } else { + dbot.say(data.channel, 'No quotes were added recently.'); + } + } else { + dbot.say(data.channel, 'No spamming that shit. Try again in a few minutes...'); + } + }, + '~qcount': function(data, params) { var q = data.message.valMatch(/^~qcount ([\d\w\s]*)/, 2); if(q) { @@ -34,6 +51,8 @@ var quotes = function(dbot) { quotes[q[1]] = []; } quotes[q[1]].push(q[2]); + addStack.push(q[1]); + rmAllowed = true; dbot.say(data.channel, 'Quote saved in \'' + q[1] + '\' (' + quotes[q[1]].length + ')'); } }, @@ -69,6 +88,9 @@ var quotes = function(dbot) { return { 'onLoad': function() { + dbot.timers.addTimer(1000 * 60 * 3, function() { + rmAllowed = true; + }); return commands; } }; diff --git a/run.js b/run.js index b01abd9..ec713ae 100644 --- a/run.js +++ b/run.js @@ -1,20 +1,19 @@ -require('./snippets'); var fs = require('fs'); +var timers = require('./timer'); var jsbot = require('./jsbot'); +require('./snippets'); var modules = ['user', 'admin', 'puns', 'kick', 'reality', 'karma', 'youare', 'quotes']; -var DBot = function(dModules, quotes) { +var DBot = function(dModules, timers) { this.admin = 'reality'; this.waitingForKarma = false; - this.name = 'depressionbot'; + this.name = 'testressionbot'; this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); + this.timers = timers.create(); this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() { this.instance.join('#realitest'); - this.instance.join('#itonlygetsworse'); - this.instance.join('#42'); - this.instance.join('#fail'); }.bind(this)); this.moduleNames = dModules; @@ -34,6 +33,7 @@ DBot.prototype.reloadModules = function() { this.rawModules = []; this.modules = []; this.commands = {}; + this.timers.clearTimers(); var path = require.resolve('./snippets'); require.cache[path] = undefined; @@ -91,4 +91,4 @@ DBot.prototype.reloadModules = function() { }.bind(this)); }; -new DBot(modules); +new DBot(modules, timers); diff --git a/timer.js b/timer.js new file mode 100644 index 0000000..de260dd --- /dev/null +++ b/timer.js @@ -0,0 +1,19 @@ +var timers = function() { + var timers = []; + + return { + 'addTimer': function(interval, callback) { // Because who puts the callback first. Really. + timers.push(setInterval(callback, interval)); + }, + + 'clearTimers': function() { + for(var i;i