quote interpolation recursion

This commit is contained in:
reality 2015-08-13 14:36:22 +00:00
parent 11a0a76a9d
commit 25d26b5586

View File

@ -11,10 +11,9 @@ var quotes = function(dbot) {
this.internalAPI = { this.internalAPI = {
// Parse quote interpolations // Parse quote interpolations
'interpolatedQuote': function(server, channel, username, key, quote, callback) { 'interpolatedQuote': function(server, channel, username, key, quote, callback) {
console.log(quote);
var quoteRefs = quote.match(/~~([\d\w\s-]*)~~/g); var quoteRefs = quote.match(/~~([\d\w\s-]*)~~/g);
if(quoteRefs) { if(quoteRefs) {
var ref = this.internalAPI.cleanRef(quoteRefs[0].replace(/^~~/,'').replace(/~~$/,'').trim()); var ref = quoteRefs[0].replace(/^~~/,'').replace(/~~$/,'').trim();
if(ref === '-nicks-') { if(ref === '-nicks-') {
if(_.has(dbot.instance.connections[server].channels, channel)) { if(_.has(dbot.instance.connections[server].channels, channel)) {
var nicks = var nicks =
@ -30,27 +29,19 @@ var quotes = function(dbot) {
username, key, quote, callback); username, key, quote, callback);
} else { } else {
this.api.getQuote(ref, function(interQuote) { this.api.getQuote(ref, function(interQuote) {
if(!interQuote || ref == key) { if(!interQuote) {
interQuote = ''; interQuote = '';
} }
quote = quote.replace('~~' + ref + '~~', interQuote); quote = quote.replace('~~' + ref + '~~', interQuote);
this.internalAPI.interpolatedQuote(server, channel, this.internalAPI.interpolatedQuote(server, channel,
username, key, quote, callback); username, key, quote, callback);
}.bind(this)); }.bind(this), true);
} }
} else { } else {
callback(quote); callback(quote);
} }
}.bind(this), }.bind(this),
'cleanRef': function(key) {
key = key.toLowerCase();
while(key.slice(-1) == '_') {
key = key.substring(0, key.length-1);
}
return key;
},
'resetRemoveTimer': function(event, key, quote) { 'resetRemoveTimer': function(event, key, quote) {
this.rmAllowed = false; this.rmAllowed = false;
setTimeout(function() { setTimeout(function() {
@ -107,10 +98,17 @@ var quotes = function(dbot) {
}, },
'getQuote': function(key, callback) { 'getQuote': function(key, callback, removeRefs) {
this.api.getQuoteCategory(key, function(category) { this.api.getQuoteCategory(key, function(category) {
if(category) { if(category) {
var quotes = category.quotes; var quotes = category.quotes;
if(removeRefs) {
quotes = _.filter(quotes, function(q) {
return !q.match('~~'+key+'~~');
});
}
var index = _.random(0, quotes.length - 1); var index = _.random(0, quotes.length - 1);
callback(quotes[index]); callback(quotes[index]);
} else { } else {
@ -120,7 +118,6 @@ var quotes = function(dbot) {
}, },
'getInterpolatedQuote': function(server, channel, username, key, callback) { 'getInterpolatedQuote': function(server, channel, username, key, callback) {
console.log(key);
key = key.trim().toLowerCase(), key = key.trim().toLowerCase(),
this.api.getQuote(key, function(quote) { this.api.getQuote(key, function(quote) {