3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-27 21:22:38 +01:00
dbot/modules/remind/remind.js

197 lines
6.8 KiB
JavaScript
Raw Normal View History

/**
* Module Name: Remind
* Description: Reminds you
*/
2015-10-08 20:07:50 +02:00
var crypto = require('crypto'),
moment = require('moment'),
_ = require('underscore')._;
2015-04-06 20:46:21 +02:00
var remind = function(dbot) {
var self = this;
this.api = {
'parseTime': function(time) {
var components = time.match(/[0-9]+[ydhms]/g);
if(!components) {
return;
}
var seconds = 0;
for(var i=0; i < components.length; ++i) {
var value = parseInt(components[i].match(/[0-9]+/)[0]),
component = components[i].match(/[ydhms]/)[0];
seconds += this.internalAPI.getSeconds(value,component);
}
return new Date(Date.now() + (seconds * 1000));
}
};
this.internalAPI = {
'parseParams': function(params) {
var i;
for(i=0; i < params.length; ++i) {
if(!params[i].match(/[0-9]+[ydhms]/)) {
break;
}
}
var time = params.slice(0,i).join(''),
message = params.slice(i, params.length).join(' ');
if(dbot.config.debugMode) {
console.log("time: " + time + " [0:" + i.toString() + "]");
console.log("message: " + message + " [" + i.toString() + ":" + params.length.toString() + "]");
}
return [time,message];
}.bind(this),
'getSeconds': function(value,component) {
switch(component) {
2016-04-11 23:49:17 +02:00
case "y":
return value * 365 * 60 * 60;
case "d":
return value * 24 * 60 * 60;
case "h":
return value * 60 * 60;
case "m":
return value * 60;
case "s":
return value;
}
}.bind(this),
'doReminder': function(event, user, time, message) {
var then = this.api.parseTime(time);
if(!then) {
return event.reply("The time parameter was not a valid time mah boy, it was "+time);
}
if(dbot.config.debugMode) {
event.reply("The timer will be at "+then);
}
2015-04-06 20:46:21 +02:00
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 have set the timer with your message \""+message+"\"");
}
else {
event.reply("I have set the timer.");
}
2015-04-06 20:46:21 +02:00
}.bind(this),
2015-04-06 20:46:21 +02:00
'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.");
}
}
var hash = self.internalAPI.getHashForTime(time);
if(dbot.config.debugMode) {
dbot.say(server,channel,"Removing timer with hash "+hash);
}
delete dbot.db.remindTimers[hash];
2015-04-06 20:46:21 +02:00
};
2015-04-06 20:46:21 +02:00
dbot.api.timers.addTimeout(time,cb,null);
if(dbot.config.debugMode) {
dbot.say(server,channel,"Timer queued for "+time);
}
2015-04-06 20:46:21 +02:00
}.bind(this),
2015-04-06 20:46:21 +02:00
'saveTimer': function(server,channel,time,starter,target,message) {
var hash = this.internalAPI.getHashForTime(time);
2015-04-06 20:59:40 +02:00
dbot.db.remindTimers[hash] = {server:server, channel:channel.name, time:time.valueOf(), starter:starter, target:target, message:message};
2015-04-06 20:46:21 +02:00
}.bind(this),
2015-04-06 20:46:21 +02:00
'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)
};
this.commands = {
'~remind': function(event) {
if(event.params.length < 3) {
event.reply("You need to give me a user and time dude.");
return;
}
var r = this.internalAPI.parseParams(event.params.slice(2, event.params.length));
this.internalAPI.doReminder(event, event.params[1], r[0], r[1]);
},
'~remindme': function(event) {
if(event.params.length < 2) {
event.reply("You need to give me a time dude.");
return;
}
var r = this.internalAPI.parseParams(event.params.slice(1, event.params.length));
this.internalAPI.doReminder(event, event.user, r[0], r[1]);
},
'~myreminders': function(event) {
var reminders = _.filter(dbot.db.remindTimers, function(t){ return t.target == event.user; });
if(_.size(reminders) > 0) {
var output = '';
_.each(reminders, function(reminder, i) {
output += (i+1) + ': "' + reminder.message + '" in ' + moment(reminder.time).toNow(true) + '. ';
});
event.reply('You have ' + _.size(reminders) + ' active reminders. ' + output);
}
else {
event.reply('You have no currently active timers.');
}
}
2015-04-06 20:46:21 +02:00
};
this.onLoad = function() {
if(!dbot.db.remindTimers) {
dbot.db.remindTimers = {};
return;
}
2015-04-06 21:25:33 +02:00
for(var i=0;i<Object.keys(dbot.db.remindTimers).length;++i) {
if(dbot.config.debugMode) {
console.log("Found saved timer "+Object.keys(dbot.db.remindTimers)[i]);
}
2015-04-06 21:25:33 +02:00
var prop = dbot.db.remindTimers[Object.keys(dbot.db.remindTimers)[i]];
if(parseInt(prop.time) < Date.now().valueOf()) {
if(dbot.config.debugMode) {
console.log("This timer is old. I shall delete it.");
}
2015-04-06 21:25:33 +02:00
delete dbot.db.remindTimers[Object.keys(dbot.db.remindTimers)[i]];
continue;
}
this.internalAPI.startTimer(prop.server,prop.channel,new Date(prop.time),prop.starter,prop.target,prop.message);
}
};
};
exports.fetch = function(dbot) {
return new remind(dbot);
};