From b54e5aa22d88b0da3f90b71de4f1fc7764cbfafa Mon Sep 17 00:00:00 2001 From: amki Date: Mon, 6 Apr 2015 20:46:21 +0200 Subject: [PATCH] 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