From b54e5aa22d88b0da3f90b71de4f1fc7764cbfafa Mon Sep 17 00:00:00 2001 From: amki Date: Mon, 6 Apr 2015 20:46:21 +0200 Subject: [PATCH 1/4] Started work on persistent timers --- modules/remind/remind.js | 60 +++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/modules/remind/remind.js b/modules/remind/remind.js index 7c1fb62..36990b9 100644 --- a/modules/remind/remind.js +++ b/modules/remind/remind.js @@ -3,6 +3,8 @@ * Description: Reminds you */ +var crypto = require('crypto'); + var remind = function(dbot) { var self = this; @@ -36,26 +38,41 @@ var remind = function(dbot) { if(dbot.config.debugMode) { event.reply("The timer will be at "+then); } - var cb = function() { - if(message) { - if(event.user === user) { - event.reply(user+": This is your reminder. You left a message: "+message); - } else { - event.reply(user+": This is your reminder. "+event.user+" left a message: "+message); - } - } else { - if(event.user === user) { - event.reply(user+": This is your reminder. You did not leave a message."); - } else { - event.reply(user+": This is your reminder. "+event.user+" did not leave a message."); - } - } - }; - dbot.api.timers.addTimeout(then,cb,null); + this.internalAPI.startTimer(event.server,event.channel,then,event.user,user,message); + this.internalAPI.saveTimer(event.server,event.channel,then,event.user,user,message); if(message) event.reply("I've set the timer with message "+message); else event.reply("I've set the timer."); + }.bind(this), + 'startTimer': function(server, channel, time, starter, target, message) { + var cb = function() { + if(message) { + if(starter === target) { + dbot.say(server,channel,target+": This is your reminder. You left a message: "+message); + } else { + dbot.say(server,channel,target+": This is your reminder. "+starter+" left a message: "+message); + } + } else { + if(starter === target) { + dbot.say(server,channel,target+": This is your reminder. You did not leave a message."); + } else { + dbot.say(server,channel,target+": This is your reminder. "+starter+" did not leave a message."); + } + } + }; + dbot.api.timers.addTimeout(time,cb,null); + dbot.say(server,channel,"Timer queued for "+time); + }.bind(this), + 'saveTimer': function(server,channel,time,starter,target,message) { + var hash = this.internalAPI.getHashForTime(time); + dbot.db.remindTimers[hash] = {server:server, channel:channel, time:time.valueOf(), starter:starter, target:target, message:message}; + }.bind(this), + 'getHashForTime': function(time) { + var md5 = crypto.createHash('md5'); + console.log(time.valueOf().toString()); + md5.update(time.valueOf().toString()); + return hash = md5.digest('hex'); }.bind(this) }; @@ -73,6 +90,17 @@ var remind = function(dbot) { return; } this.internalAPI.doReminder(event,event.user,event.params[1],event.params.splice(2, event.params.length-1).join(' ').trim()); + } + }; + + this.onLoad = function() { + if(!dbot.db.remindTimers) { + dbot.say("tripsit","#epow0","dbot.db.remindTimers is "+dbot.db.remindTimers); + dbot.db.remindTimers = {}; + return; + } + for(var i=0;i Date: Mon, 6 Apr 2015 20:59:40 +0200 Subject: [PATCH 2/4] New iteration --- modules/remind/remind.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/remind/remind.js b/modules/remind/remind.js index 36990b9..6fe5e47 100644 --- a/modules/remind/remind.js +++ b/modules/remind/remind.js @@ -60,13 +60,14 @@ var remind = function(dbot) { dbot.say(server,channel,target+": This is your reminder. "+starter+" did not leave a message."); } } + dbot.say(server,channel,"REMOTE THE FUCKING TIMER NOW!"); }; dbot.api.timers.addTimeout(time,cb,null); dbot.say(server,channel,"Timer queued for "+time); }.bind(this), 'saveTimer': function(server,channel,time,starter,target,message) { var hash = this.internalAPI.getHashForTime(time); - dbot.db.remindTimers[hash] = {server:server, channel:channel, time:time.valueOf(), starter:starter, target:target, message:message}; + dbot.db.remindTimers[hash] = {server:server, channel:channel.name, time:time.valueOf(), starter:starter, target:target, message:message}; }.bind(this), 'getHashForTime': function(time) { var md5 = crypto.createHash('md5'); @@ -99,8 +100,9 @@ var remind = function(dbot) { dbot.db.remindTimers = {}; return; } - for(var i=0;i Date: Mon, 6 Apr 2015 21:25:33 +0200 Subject: [PATCH 3/4] still doesn't work --- modules/remind/remind.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/remind/remind.js b/modules/remind/remind.js index 6fe5e47..7ef9a2b 100644 --- a/modules/remind/remind.js +++ b/modules/remind/remind.js @@ -46,6 +46,7 @@ var remind = function(dbot) { event.reply("I've set the timer."); }.bind(this), 'startTimer': function(server, channel, time, starter, target, message) { + dbot.say(server,channel,"startTimer called!"); var cb = function() { if(message) { if(starter === target) { @@ -100,8 +101,16 @@ var remind = function(dbot) { dbot.db.remindTimers = {}; return; } - for(var prop in dbot.db.remindTimers) { - dbot.say("tripsit","#epow0","Found saved timer "+prop); + dbot.say("tripsit","#epow0","dbot.db.remindTimers has length "+Object.keys(dbot.db.remindTimers).length); + for(var i=0;i Date: Mon, 6 Apr 2015 23:38:21 +0200 Subject: [PATCH 4/4] Fixed bugs, removed debug output, timers now persistent --- modules/remind/remind.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/modules/remind/remind.js b/modules/remind/remind.js index 7ef9a2b..29ad279 100644 --- a/modules/remind/remind.js +++ b/modules/remind/remind.js @@ -35,18 +35,16 @@ var remind = function(dbot) { timeinseconds += this.internalAPI.getSeconds(number,interval); } var then = new Date(now + (timeinseconds*1000)); - if(dbot.config.debugMode) { + if(dbot.config.debugMode) event.reply("The timer will be at "+then); - } this.internalAPI.startTimer(event.server,event.channel,then,event.user,user,message); this.internalAPI.saveTimer(event.server,event.channel,then,event.user,user,message); if(message) - event.reply("I've set the timer with message "+message); + event.reply("I have set the timer with your message \""+message+"\""); else - event.reply("I've set the timer."); + event.reply("I have set the timer."); }.bind(this), 'startTimer': function(server, channel, time, starter, target, message) { - dbot.say(server,channel,"startTimer called!"); var cb = function() { if(message) { if(starter === target) { @@ -61,10 +59,14 @@ var remind = function(dbot) { dbot.say(server,channel,target+": This is your reminder. "+starter+" did not leave a message."); } } - dbot.say(server,channel,"REMOTE THE FUCKING TIMER NOW!"); + var hash = self.internalAPI.getHashForTime(time); + if(dbot.config.debugMode) + dbot.say(server,channel,"Removing timer with hash "+hash); + delete dbot.db.remindTimers[hash]; }; dbot.api.timers.addTimeout(time,cb,null); - dbot.say(server,channel,"Timer queued for "+time); + if(dbot.config.debugMode) + dbot.say(server,channel,"Timer queued for "+time); }.bind(this), 'saveTimer': function(server,channel,time,starter,target,message) { var hash = this.internalAPI.getHashForTime(time); @@ -97,21 +99,20 @@ var remind = function(dbot) { this.onLoad = function() { if(!dbot.db.remindTimers) { - dbot.say("tripsit","#epow0","dbot.db.remindTimers is "+dbot.db.remindTimers); dbot.db.remindTimers = {}; return; } - dbot.say("tripsit","#epow0","dbot.db.remindTimers has length "+Object.keys(dbot.db.remindTimers).length); for(var i=0;i