dbot/modules/quotes/quotes.js

116 lines
4.1 KiB
JavaScript
Raw Normal View History

var _ = require('underscore')._;
var quotes = function(dbot) {
2013-01-15 15:51:07 +01:00
dbot.sessionData.rmCache = [];
2013-01-15 15:47:46 +01:00
this.quotes = dbot.db.quoteArrs,
this.addStack = [],
this.rmAllowed = true,
this.rmCache = dbot.sessionData.rmCache,
this.rmTimer;
2013-01-15 15:47:46 +01:00
this.internalAPI = {
// Retrieve a random quote from a given category, interpolating any quote
// references (~~QUOTE CATEGORY~~) within it
'interpolatedQuote': function(event, key, quoteTree) {
if(!_.isUndefined(quoteTree) && quoteTree.indexOf(key) != -1) {
return '';
} else if(_.isUndefined(quoteTree)) {
quoteTree = [];
}
2013-01-15 15:47:46 +01:00
var index = _.random(0, this.quotes[key].length - 1);
var quoteString = this.quotes[key][index];
// Parse quote interpolations
var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g);
var thisRef;
while(quoteRefs && (thisRef = quoteRefs.shift()) !== undefined) {
var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim());
if(cleanRef === '-nicks-') {
var randomNick = _.keys(event.channel.nicks)[_.random(0, _.size(event.channel.nicks) -1)];
quoteString = quoteString.replace("~~" + cleanRef + "~~", randomNick);
quoteTree.pop();
} else if(_.has(this.quotes, cleanRef)) {
quoteTree.push(key);
quoteString = quoteString.replace("~~" + cleanRef + "~~",
interpolatedQuote(event, cleanRef, quoteTree.slice()));
quoteTree.pop();
}
}
2013-01-15 15:47:46 +01:00
return quoteString;
}.bind(this),
2013-01-15 15:47:46 +01:00
'resetRemoveTimer': function(event, key, quote) {
this.rmAllowed = false;
dbot.timers.addOnceTimer(5000, function() {
this.rmAllowed = true;
});
2013-01-15 15:47:46 +01:00
this.rmCache.push({
'key': key,
'quote': quote
});
2013-01-15 15:47:46 +01:00
dbot.timers.clearTimeout(this.rmTimer);
if(this.rmCache.length < dbot.config.quotes.rmLimit) {
this.rmTimer = dbot.timers.addOnceTimer(600000, function() {
this.rmCache.length = 0; // lol what
2013-01-15 16:32:44 +01:00
}.bind(this));
2013-01-15 15:47:46 +01:00
} else {
_.each(dbot.config.admins, function(admin) {
dbot.say(event.server, admin, dbot.t('rm_cache_limit'));
});
}
}.bind(this)
};
2013-01-15 15:47:46 +01:00
this.api = {
'getQuote': function(event, category) {
2013-01-03 22:27:06 +01:00
var key = category.trim().toLowerCase();
var altKey;
if(key.split(' ').length > 0) {
altKey = key.replace(/ /g, '_');
}
if(key.charAt(0) !== '_') { // lol
2013-01-15 15:47:46 +01:00
if(_.has(this.quotes, key)) {
return this.internalAPI.interpolatedQuote(event, key);
} else if(_.has(this.quotes, altKey)) {
return this.internalAPI.interpolatedQuote(event, altKey);
2013-01-03 22:27:06 +01:00
} else {
return false;
}
}
}
};
2013-01-15 15:47:46 +01:00
this.listener = function(event) {
if(event.action == 'PRIVMSG') {
if(event.user == 'reality') {
var once = event.message.valMatch(/^I ([\d\w\s,'-]* once)/, 2);
} else {
2013-01-15 15:47:46 +01:00
var once = event.message.valMatch(/^reality ([\d\w\s,'-]* once)/, 2);
}
2013-01-15 15:47:46 +01:00
if(once) {
event.message = '~qadd realityonce=reality ' + once[1];
event.action = 'PRIVMSG';
event.params = event.message.split(' ');
dbot.instance.emit(event);
}
} else if(event.action == 'JOIN') {
var userQuote = this.api.getQuote(event, event.user)
if(userQuote) {
event.reply(event.user + ': ' + this.api.getQuote(event, event.user));
}
2013-01-15 15:47:46 +01:00
}
}.bind(this);
this.on = ['PRIVMSG', 'JOIN'];
};
exports.fetch = function(dbot) {
2013-01-15 15:47:46 +01:00
return new quotes(dbot);
};