Interpolated quotes revived and plugged into ~q. Also a event inspecific getQuote API function implemented. [#331]

This commit is contained in:
reality 2013-04-12 21:08:11 +00:00
parent 36254c9e03
commit b8a25062b5
2 changed files with 44 additions and 52 deletions

View File

@ -51,18 +51,12 @@ var commands = function(dbot) {
// Retrieve quote from a category in the database. // Retrieve quote from a category in the database.
'~q': function(event) { '~q': function(event) {
var name = event.input[1].trim().toLowerCase(), var key = event.input[1];
category = false; this.api.getInterpolatedQuote(event.server, event.channel, key, function(quote) {
if(quote) {
this.db.search('quote_category', { 'name': name }, function(result) { event.reply(key + ': ' + quote);
category = result;
}, function(err) {
if(category) {
var quotes = category.quotes;
var index = _.random(0, quotes.length - 1);
event.reply(name + ': ' + quotes[index]);
} else { } else {
event.reply(dbot.t('category_not_found', { 'category': name })); event.reply(dbot.t('category_not_found', { 'category': key }));
} }
}); });
}, },

View File

@ -9,37 +9,28 @@ var quotes = function(dbot) {
this.rmTimer; this.rmTimer;
this.internalAPI = { this.internalAPI = {
// Retrieve a random quote from a given category, interpolating any quote 'interpolatedQuote': function(server, channel, key, quote, callback) {
// references (~~QUOTE CATEGORY~~) within it
'interpolatedQuote': function(server, channel, key, quoteTree) {
if(!_.isUndefined(quoteTree) && quoteTree.indexOf(key) != -1) {
return '';
} else if(_.isUndefined(quoteTree)) {
quoteTree = [];
}
var index = _.random(0, this.quotes[key].length - 1);
var quoteString = this.quotes[key][index];
// Parse quote interpolations // Parse quote interpolations
var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g); var quoteRefs = quote.match(/~~([\d\w\s-]*)~~/g);
var thisRef; if(quoteRefs) {
var ref = dbot.cleanNick(quoteRefs[0].replace(/^~~/,'').replace(/~~$/,'').trim());
while(quoteRefs && (thisRef = quoteRefs.shift()) !== undefined) { if(ref === '-nicks-') {
var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim()); dbot.api.users.getRandomChannelUser(server, channel, function(user) {
if(cleanRef === '-nicks-') { quote = quote.replace('~~' + ref + '~~', randomNick);
var randomNick = dbot.api.users.getRandomChannelUser(server, channel); this.internalAPI.interpolatedQuote(server, channel, key, quote, callback);
quoteString = quoteString.replace("~~" + cleanRef + "~~", randomNick); }.bind(this));
quoteTree.pop(); } else {
} else if(_.has(this.quotes, cleanRef)) { this.api.getQuote(ref, function(interQuote) {
quoteTree.push(key); if(!interQuote || ref == key) {
quoteString = quoteString.replace("~~" + cleanRef + "~~", interQuote = '';
this.internalAPI.interpolatedQuote(server, channel, cleanRef, quoteTree.slice()));
quoteTree.pop();
} }
quote = quote.replace('~~' + ref + '~~', interQuote);
this.internalAPI.interpolatedQuote(server, channel, key, quote, callback);
}.bind(this));
}
} else {
callback(quote);
} }
return quoteString;
}.bind(this), }.bind(this),
'resetRemoveTimer': function(event, key, quote) { 'resetRemoveTimer': function(event, key, quote) {
@ -67,22 +58,29 @@ var quotes = function(dbot) {
}; };
this.api = { this.api = {
'getQuote': function(event, category) { 'getQuote': function(key, callback) {
var key = category.trim().toLowerCase(); var category = false;
var altKey; key = key.trim().toLowerCase(),
if(key.split(' ').length > 0) {
altKey = key.replace(/ /g, '_');
}
if(key.charAt(0) !== '_') { // lol this.db.search('quote_category', { 'name': key }, function(result) {
if(_.has(this.quotes, key)) { category = result;
return this.internalAPI.interpolatedQuote(event.server, event.channel.name, key); }, function(err) {
} else if(_.has(this.quotes, altKey)) { if(category) {
return this.internalAPI.interpolatedQuote(event.server, event.channel.name, altKey); var quotes = category.quotes;
var index = _.random(0, quotes.length - 1);
callback(quotes[index]);
} else { } else {
return false; callback(false);
}
} }
});
},
'getInterpolatedQuote': function(server, channel, key, callback) {
key = key.trim().toLowerCase(),
this.api.getQuote(key, function(quote) {
this.internalAPI.interpolatedQuote(server, channel, key, quote, callback);
}.bind(this));
} }
}; };