3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-01 00:19:29 +01:00
dbot/modules/quotes/quotes.js

131 lines
4.6 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(server, channel, key, quoteTree) {
2013-01-15 15:47:46 +01:00
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 = dbot.api.users.getRandomChannelUser(server, channel);
2013-01-15 15:47:46 +01:00
quoteString = quoteString.replace("~~" + cleanRef + "~~", randomNick);
quoteTree.pop();
} else if(_.has(this.quotes, cleanRef)) {
quoteTree.push(key);
quoteString = quoteString.replace("~~" + cleanRef + "~~",
this.internalAPI.interpolatedQuote(server, channel, cleanRef, quoteTree.slice()));
2013-01-15 15:47:46 +01:00
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;
2013-01-27 21:52:11 +01:00
setTimeout(function() {
2013-01-15 15:47:46 +01:00
this.rmAllowed = true;
2013-01-27 21:52:11 +01:00
}.bind(this), 5000);
2013-01-15 15:47:46 +01:00
this.rmCache.push({
'key': key,
'quote': quote
});
2013-01-15 15:47:46 +01:00
clearTimeout(this.rmTimer);
2013-01-15 15:47:46 +01:00
if(this.rmCache.length < dbot.config.quotes.rmLimit) {
2013-01-27 21:52:11 +01:00
this.rmTimer = setTimeout(function() {
2013-01-15 15:47:46 +01:00
this.rmCache.length = 0; // lol what
2013-01-27 21:52:11 +01:00
}.bind(this), 600000);
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.server, event.channel.name, key);
2013-01-15 15:47:46 +01:00
} else if(_.has(this.quotes, altKey)) {
return this.internalAPI.interpolatedQuote(event.server, event.channel.name, altKey);
2013-01-03 22:27:06 +01:00
} else {
return false;
}
}
},
'getQuoteCategory': function(name) {
console.log(name);
var key = name.trim().toLowerCase();
if(_.has(this.quotes, key)) {
return this.quotes[key];
} else {
return false;
}
2013-01-03 22:27:06 +01:00
}
};
this.api['getQuoteCategory'].external = true;
this.api['getQuoteCategory'].extMap = [ 'name' ];
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);
2013-05-17 09:04:57 +02:00
}
2013-01-15 15:47:46 +01:00
} else if(event.action == 'JOIN') {
2013-05-17 09:04:57 +02:00
if(this.config.quotesOnJoin == true) {
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);
};