incomplete underscorisation in quotes [#81]

This commit is contained in:
reality 2013-01-12 21:40:16 +00:00
parent abaa1e7fa2
commit 08bd67f4f0
2 changed files with 87 additions and 69 deletions

View File

@ -1,22 +1,24 @@
var _ = require('underscore')._;
var quotes = function(dbot) { var quotes = function(dbot) {
var name = 'quotes'; var quotes = dbot.db.quoteArrs,
var quotes = dbot.db.quoteArrs; addStack = [],
var addStack = []; rmAllowed = true,
var rmAllowed = true; rmCache = dbot.sessionData.rmCache,
rmTimer;
dbot.sessionData.rmCache = []; dbot.sessionData.rmCache = [];
var rmCache = dbot.sessionData.rmCache;
var rmTimer;
// Retrieve a random quote from a given category, interpolating any quote // Retrieve a random quote from a given category, interpolating any quote
// references (~~QUOTE CATEGORY~~) within it // references (~~QUOTE CATEGORY~~) within it
var interpolatedQuote = function(event, key, quoteTree) { var interpolatedQuote = function(event, key, quoteTree) {
if(quoteTree !== undefined && quoteTree.indexOf(key) != -1) { if(!_.isUndefined(quoteTree) && quoteTree.indexOf(key) != -1) {
return ''; return '';
} else if(quoteTree === undefined) { } else if(_.isUndefined(quoteTree)) {
quoteTree = []; quoteTree = [];
} }
var quoteString = quotes[key].random(); var index = _.random(0, quotes[key].length - 1);
var quoteString = quotes[key][index];
// Parse quote interpolations // Parse quote interpolations
var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g); var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g);
@ -25,10 +27,10 @@ var quotes = function(dbot) {
while(quoteRefs && (thisRef = quoteRefs.shift()) !== undefined) { while(quoteRefs && (thisRef = quoteRefs.shift()) !== undefined) {
var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim()); var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim());
if(cleanRef === '-nicks-') { if(cleanRef === '-nicks-') {
var randomNick = Object.keys(event.channel.nicks).random(); var randomNick = _.keys(event.channel.nicks)[_.random(0, _.size(event.channel.nicks) -1)];
quoteString = quoteString.replace("~~" + cleanRef + "~~", randomNick); quoteString = quoteString.replace("~~" + cleanRef + "~~", randomNick);
quoteTree.pop(); quoteTree.pop();
} else if(quotes.hasOwnProperty(cleanRef)) { } else if(_.has(quotes, cleanRef)) {
quoteTree.push(key); quoteTree.push(key);
quoteString = quoteString.replace("~~" + cleanRef + "~~", quoteString = quoteString.replace("~~" + cleanRef + "~~",
interpolatedQuote(event, cleanRef, quoteTree.slice())); interpolatedQuote(event, cleanRef, quoteTree.slice()));
@ -45,17 +47,20 @@ var quotes = function(dbot) {
rmAllowed = true; rmAllowed = true;
}); });
rmCache.push({'key': key, 'quote': quote}); rmCache.push({
'key': key,
'quote': quote
});
dbot.timers.clearTimeout(rmTimer); dbot.timers.clearTimeout(rmTimer);
if(rmCache.length < dbot.config.quotes.rmLimit) { if(rmCache.length < dbot.config.quotes.rmLimit) {
rmTimer = dbot.timers.addOnceTimer(600000, function() { rmTimer = dbot.timers.addOnceTimer(600000, function() {
rmCache.length = 0; // lol what rmCache.length = 0; // lol what
}); });
} else { } else {
for(var i=0;i<dbot.config.admins.length;i++) { _.each(dbot.config.admins, function(admin) {
dbot.say(event.server, dbot.config.admins[i], dbot.say(event.server, admin, dbot.t('rm_cache_limit'));
dbot.t('rm_cache_limit')); });
}
} }
}; };
@ -68,9 +73,9 @@ var quotes = function(dbot) {
} }
if(key.charAt(0) !== '_') { // lol if(key.charAt(0) !== '_') { // lol
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
return interpolatedQuote(event, key); return interpolatedQuote(event, key);
} else if(quotes.hasOwnProperty(altKey)) { } else if(_.has(quotes, altKey)) {
return interpolatedQuote(event, altKey); return interpolatedQuote(event, altKey);
} else { } else {
return false; return false;
@ -106,7 +111,7 @@ var quotes = function(dbot) {
'~rmdeny': function(event) { '~rmdeny': function(event) {
var rmCacheCount = rmCache.length; var rmCacheCount = rmCache.length;
for(var i=0;i<rmCacheCount;i++) { for(var i=0;i<rmCacheCount;i++) {
if(!quotes.hasOwnProperty(rmCache[i].key)) { if(!_.has(quotes, rmCache[i].key)) {
quotes[rmCache[i].key] = []; quotes[rmCache[i].key] = [];
} }
quotes[rmCache[i].key].push(rmCache[i].quote); quotes[rmCache[i].key].push(rmCache[i].quote);
@ -131,12 +136,16 @@ var quotes = function(dbot) {
// Shows a list of the biggest categories // Shows a list of the biggest categories
'~qstats': function(event) { '~qstats': function(event) {
var qSizes = Object.prototype.sort(quotes, function(key, obj) { return obj[key].length }); var qSizes = _.chain(quotes)
qSizes = qSizes.slice(qSizes.length - 10).reverse(); .pairs()
.sortBy(function(category) { return category[1].length })
.reverse()
.first(10)
.value();
var qString = dbot.t('large_categories'); var qString = dbot.t('large_categories');
for(var i=0;i<qSizes.length;i++) { for(var i=0;i<qSizes.length;i++) {
qString += qSizes[i][0] + " (" + qSizes[i][1] + "), "; qString += qSizes[i][0] + " (" + qSizes[i][1].length + "), ";
} }
event.reply(qString.slice(0, -2)); event.reply(qString.slice(0, -2));
@ -146,19 +155,20 @@ var quotes = function(dbot) {
'~qsearch': function(event) { '~qsearch': function(event) {
var haystack = event.input[1].trim().toLowerCase(); var haystack = event.input[1].trim().toLowerCase();
var needle = event.input[2]; var needle = event.input[2];
if(quotes.hasOwnProperty(haystack)) { if(_.has(quotes, haystack)) {
var matches = []; var matches = _.filter(quotes[haystack], function(quote) {
quotes[haystack].each(function(quote) { return _.indexOf(quote, needle) != -1;
if(quote.indexOf(needle) != -1) { }, this);
matches.push(quote);
}
}.bind(this));
if(matches.length == 0) { if(matches.length == 0) {
event.reply(dbot.t('no_results')); event.reply(dbot.t('no_results'));
} else { } else {
event.reply(dbot.t('search_results', {'category': haystack, 'needle': needle, event.reply(dbot.t('search_results', {
'quote': matches.random(), 'matches': matches.length})); 'category': haystack,
'needle': needle,
'quote': matches.random(),
'matches': matches.length
}));
} }
} else { } else {
event.reply(dbot.t('empty_category')); event.reply(dbot.t('empty_category'));
@ -166,16 +176,19 @@ var quotes = function(dbot) {
}, },
'~rmlast': function(event) { '~rmlast': function(event) {
if(rmAllowed == true || dbot.config.admins.include(event.user)) { if(rmAllowed == true || _.include(dbot.config.admins, event.user)) {
var key = event.input[1].trim().toLowerCase(); var key = event.input[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
var quote = quotes[key].pop(); var quote = quotes[key].pop();
if(quotes[key].length === 0) { if(quotes[key].length === 0) {
delete quotes[key]; delete quotes[key];
} }
resetRemoveTimer(event, key, quote); resetRemoveTimer(event, key, quote);
event.reply(dbot.t('removed_from', {'quote': quote, 'category': key})); event.reply(dbot.t('removed_from', {
'quote': quote,
'category': key
}));
} else { } else {
event.reply(dbot.t('no_quotes', {'category': q[1]})); event.reply(dbot.t('no_quotes', {'category': q[1]}));
} }
@ -185,11 +198,11 @@ var quotes = function(dbot) {
}, },
'~rm': function(event) { '~rm': function(event) {
if(rmAllowed == true || dbot.config.admins.include(event.user)) { if(rmAllowed == true || _.include(dbot.config.admins, event.user)) {
var key = event.input[1].trim().toLowerCase(); var key = event.input[1].trim().toLowerCase();
var quote = event.input[2]; var quote = event.input[2];
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
var category = quotes[key]; var category = quotes[key];
var index = category.indexOf(quote); var index = category.indexOf(quote);
if(index !== -1) { if(index !== -1) {
@ -215,51 +228,59 @@ var quotes = function(dbot) {
var input = event.message.valMatch(/^~qcount ([\d\w\s-]*)/, 2); var input = event.message.valMatch(/^~qcount ([\d\w\s-]*)/, 2);
if(input) { // Give quote count for named category if(input) { // Give quote count for named category
var key = input[1].trim().toLowerCase(); var key = input[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
event.reply(dbot.t('quote_count', {'category': key, 'count': quotes[key].length})); event.reply(dbot.t('quote_count', {
'category': key,
'count': quotes[key].length
}));
} else { } else {
event.reply(dbot.t('no_quotes', {'category': key})); event.reply(dbot.t('no_quotes', { 'category': key }));
} }
} else { // Give total quote count } else { // Give total quote count
var totalQuoteCount = 0; var totalQuoteCount = _.reduce(quotes, function(memo, category) {
for(var category in quotes) { return memo + category.length;
if(quotes.hasOwnProperty(category)) { }, 0);
totalQuoteCount += quotes[category].length; event.reply(dbot.t('total_quotes', { 'count': totalQuoteCount }));
}
}
event.reply(dbot.t('total_quotes', {'count': totalQuoteCount}));
} }
}, },
'~qadd': function(event) { '~qadd': function(event) {
var key = event.input[1].toLowerCase(); var key = event.input[1].toLowerCase();
var text = event.input[2]; var text = event.input[2];
if(!Object.isArray(quotes[key])) { if(!_.isArray(quotes[key])) {
quotes[key] = []; quotes[key] = [];
} }
if(quotes[key].include(text)) { if(_.include(quotes[key], text)) {
event.reply(dbot.t('quote_exists')); event.reply(dbot.t('quote_exists'));
} else { } else {
quotes[key].push(text); quotes[key].push(text);
rmAllowed = true; rmAllowed = true;
event.reply(dbot.t('quote_saved', {'category': key, 'count': quotes[key].length})); event.reply(dbot.t('quote_saved', {
'category': key,
'count': quotes[key].length
}));
} }
}, },
'~rq': function(event) { '~rq': function(event) {
var rQuote = Object.keys(quotes).random(); var category = _.keys(quotes)[_.random(0, _.size(quotes) -1)];
event.reply(rQuote + ': ' + interpolatedQuote(event, rQuote)); event.reply(category + ': ' + interpolatedQuote(event, category));
}, },
'~link': function(event) { '~link': function(event) {
var key = event.params[1].trim().toLowerCase(); var key = event.params[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
event.reply(dbot.t('quote_link', {'category': key, event.reply(dbot.t('quote_link', {
'url': dbot.t('url', {'host': dbot.config.web.webHost, 'category': key,
'port': dbot.config.web.webPort, 'path': 'quotes/' + key})})); 'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'path': 'quotes/' + key
})
}));
} else { } else {
event.reply(dbot.t('category_not_found', {'category': key})); event.reply(dbot.t('category_not_found', { 'category': key }));
} }
}, },
}; };
@ -278,7 +299,7 @@ var quotes = function(dbot) {
// Lists quotes in a category // Lists quotes in a category
'/quotes/:key': function(req, res) { '/quotes/:key': function(req, res) {
var key = req.params.key.toLowerCase(); var key = req.params.key.toLowerCase();
if(dbot.db.quoteArrs.hasOwnProperty(key)) { if(_.has(dbot.db.quoteArrs, key)) {
res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } }); res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } });
} else { } else {
res.render('error', { 'name': dbot.config.name, 'message': 'No quotes under that key.' }); res.render('error', { 'name': dbot.config.name, 'message': 'No quotes under that key.' });

7
run.js
View File

@ -55,9 +55,7 @@ var DBot = function(timers) {
// Populate bot properties with config data // Populate bot properties with config data
// Create JSBot and connect to each server // Create JSBot and connect to each server
this.instance = jsbot.createJSBot(this.config.name); this.instance = jsbot.createJSBot(this.config.name);
for(var name in this.config.servers) { _.each(this.config.servers, function(server, name) {
if(_.has(this.config.servers, name)) {
var server = this.config.servers[name];
this.instance.addConnection(name, server.server, server.port, this.instance.addConnection(name, server.server, server.port,
this.config.admin, function(event) { this.config.admin, function(event) {
var server = this.config.servers[event.server]; var server = this.config.servers[event.server];
@ -65,8 +63,7 @@ var DBot = function(timers) {
this.instance.join(event, server.channels[i]); this.instance.join(event, server.channels[i]);
} }
}.bind(this), server.nickserv, server.password); }.bind(this), server.nickserv, server.password);
} }, this);
}
// Load the modules and connect to the server // Load the modules and connect to the server
this.reloadModules(); this.reloadModules();