Converted quotes module to new format. Everything seems to be working

This commit is contained in:
Luke Slater 2012-05-23 18:01:28 +01:00
parent ad9ddbce06
commit c3832c209f
2 changed files with 102 additions and 164 deletions

View File

@ -36,7 +36,7 @@ var command = function(dbot) {
*/ */
var applyRegex = function(commandName, event) { var applyRegex = function(commandName, event) {
var applies = false; var applies = false;
if(dbot.commands[commandName].hasOwnProperty(regex)) { if(dbot.commands[commandName].hasOwnProperty('regex')) {
var cRegex = dbot.commands[commandName].regex; var cRegex = dbot.commands[commandName].regex;
var q = event.message.valMatch(cRegex[0], cRegex[1]); var q = event.message.valMatch(cRegex[0], cRegex[1]);
if(q) { if(q) {
@ -57,7 +57,10 @@ var command = function(dbot) {
*/ */
'listener': function(event) { 'listener': function(event) {
var commandName = event.params[0]; var commandName = event.params[0];
if(dbot.commands.hasOwnProperty(commandName)) { if(!dbot.commands.hasOwnProperty(commandName)) {
commandName = '~';
}
if(isBanned(event.user, commandName)) { if(isBanned(event.user, commandName)) {
event.reply(dbot.t('command_ban', {'user': event.user})); event.reply(dbot.t('command_ban', {'user': event.user}));
} else { } else {
@ -70,7 +73,6 @@ var command = function(dbot) {
} }
} }
} }
}
}, },
'on': 'PRIVMSG', 'on': 'PRIVMSG',

View File

@ -4,7 +4,8 @@ var quotes = function(dbot) {
var addStack = []; var addStack = [];
var rmAllowed = true; var rmAllowed = true;
// Retrieve a random quote from a given category, interpolating any quote references (~~QUOTE CATEGORY~~) within it // Retrieve a random quote from a given category, interpolating any quote
// references (~~QUOTE CATEGORY~~) within it
var interpolatedQuote = function(key, quoteTree) { var interpolatedQuote = function(key, quoteTree) {
if(quoteTree !== undefined && quoteTree.indexOf(key) != -1) { if(quoteTree !== undefined && quoteTree.indexOf(key) != -1) {
return ''; return '';
@ -28,52 +29,27 @@ var quotes = function(dbot) {
} }
} }
// Parse quote parameters
/*
var paramRefs = quoteString.match(/~~\$([1-9])~~/g);
var thisParam;
while(paramRefs && (thisParam = paramRefs.shift()) !== undefined) {
thisParam = thisParam[1];
console.log(thisParam);
if(thisParam < params.length) {
quoteString = quoteString.replace("~~$" + thisParam + "~~", params[thisParam]);
}
}
*/
return quoteString; return quoteString;
}; };
var commands = { var commands = {
'~': function(data, params) { // Alternative syntax to ~q
var q = data.message.valMatch(/^~([\d\w\s-]*)/, 2); '~': function(event) {
if(q) { commands['~q'](event);
q[1] = q[1].trim(); },
key = q[1].toLowerCase();
// Retrieve quote from a category in the database.
'~q': function(event) {
var key = event.input[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) { if(quotes.hasOwnProperty(key)) {
dbot.say(data.channel, q[1] + ': ' + interpolatedQuote(key)); event.reply(key + ': ' + interpolatedQuote(key));
} else { } else {
dbot.say(data.channel, dbot.t('category_not_found', {'category': q[1]})); event.reply(dbot.t('category_not_found', {'category': key}));
}
} }
}, },
'~q': function(data, params) { // Shows a list of the biggest categories
var q = data.message.valMatch(/^~q ([\d\w\s-]*)/, 2); '~qstats': function(event) {
if(q) {
q[1] = q[1].trim();
key = q[1].toLowerCase();
if(quotes.hasOwnProperty(key)) {
dbot.say(data.channel, q[1] + ': ' + interpolatedQuote(key));
} else {
dbot.say(data.channel, dbot.t('category_not_found', {'category': q[1]}));
}
}
},
// shows the biggest categories
'~qstats': function(data, params) {
var qSizes = []; var qSizes = [];
for(var cat in quotes) { for(var cat in quotes) {
if(quotes[cat].length != 0) { if(quotes[cat].length != 0) {
@ -90,76 +66,55 @@ var quotes = function(dbot) {
qString += qSizes[i][0] + " (" + qSizes[i][1] + "), "; qString += qSizes[i][0] + " (" + qSizes[i][1] + "), ";
} }
dbot.say(data.channel, qString.slice(0, -2)); event.reply(qString.slice(0, -2));
}, },
'~qsearch': function(data, params) { // Search a given category for some text.
if(params[2] === undefined) { '~qsearch': function(event) {
dbot.say(data.channel, dbot.t('syntax_error')); var haystack = event.input[1].trim().toLowerCase();
} else { var needle = event.input[2];
params[1].trim(); if(quotes.hasOwnProperty(haystack)) {
key = params[1].toLowerCase();
if(!quotes.hasOwnProperty(key)) {
dbot.say(data.channel, dbot.t('empty_category'));
} else {
var matches = []; var matches = [];
quotes[haystack].each(function(quote) {
quotes[key].each(function(quote) { if(quote.indexOf(needle) != -1) {
if(quote.indexOf(params[2]) != -1) {
matches.push(quote); matches.push(quote);
} }
}.bind(this)); }.bind(this));
if(matches.length == 0) { if(matches.length == 0) {
dbot.say(data.channel, dbot.t('no_results')); event.reply(dbot.t('no_results'));
} else { } else {
dbot.say(data.channel, params[1] + ' (' + params[2] + '): ' + matches.random() + ' [' + matches.length + ' results]'); event.reply(dbot.t('search_results', {'category': haystack, 'needle': needle,
} 'quote': matches.random(), 'matches': matches.length}));
} }
} else {
event.reply(dbot.t('empty_category'));
} }
}, },
'~rmlast': function(data, params) { '~rmlast': function(event) {
if(rmAllowed == true || dbot.admin.include(data.user)) { if(rmAllowed == true || dbot.admin.include(event.user)) {
var q = data.message.valMatch(/^~rmlast ([\d\w\s-]*)/, 2); var key = event.input[1].trim().toLowerCase();
if(q) { if(quotes.hasOwnProperty(key)) {
q[1] = q[1].trim() if(!dbot.db.locks.include(key) || dbot.admin.include(event.user)) {
key = q[1].toLowerCase();
if(quotes.hasOwnProperty(q[1])) {
if(!dbot.db.locks.include(q[1]) || dbot.admin.include(data.user)) {
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];
} }
rmAllowed = false; rmAllowed = false;
dbot.say(data.channel, '\'' + quote + '\'' + event.reply(dbot.t('removed_from', {'quote': quote, 'category': key}));
dbot.t('removed_from') + q[1]);
} else { } else {
dbot.say(data.channel, dbot.t('locked_category', {'category': q[1]})); event.reply(dbot.t('locked_category', {'category': q[1]}));
} }
} else { } else {
dbot.say(data.channel, dbot.t('no_quotes', {'category': q[1]})); event.reply(dbot.t('no_quotes', {'category': q[1]}));
} }
} else { } else {
var last = addStack.pop(); event.reply(dbot.t('rmlast_spam'));
if(last) {
if(!dbot.db.locks.include(last)) {
quotes[last].pop();
rmAllowed = false;
dbot.say(data.channel, dbot.t('last_removed', {'category': last}));
} else {
dbot.say(data.channel, dbot.t('locked_category', {'category': last}));
}
} else {
dbot.say(data.channel, dbot.t('no_recent_adds'));
}
}
} else {
dbot.say(data.channel, dbot.t('rmlast_spam'));
} }
}, },
'~rm': function(data, params) { /*'~rm': function(data, params) {
if(rmAllowed == true || dbot.admin.include(data.user)) { if(rmAllowed == true || dbot.admin.include(data.user)) {
var q = data.message.valMatch(/^~rm ([\d\w\s-]*) (.+)$/, 3); var q = data.message.valMatch(/^~rm ([\d\w\s-]*) (.+)$/, 3);
if(q) { if(q) {
@ -188,82 +143,57 @@ var quotes = function(dbot) {
} else { } else {
dbot.say(data.channel, dbot.t('rmlast_spam')); dbot.say(data.channel, dbot.t('rmlast_spam'));
} }
}, },*/
'~qcount': function(data, params) { '~qcount': function(event) {
var q = data.message.valMatch(/^~qcount ([\d\w\s-]*)/, 2); var input = event.message.valMatch(/^~qcount ([\d\w\s-]*)/, 2);
if(q) { if(input) { // Give quote count for named category
q[1] = q[1].trim(); var key = input[1].trim().toLowerCase();
key = q[1].toLowerCase();
if(quotes.hasOwnProperty(key)) { if(quotes.hasOwnProperty(key)) {
dbot.say(data.channel, dbot.t('quote_count', {'category': q[1], 'count': quotes[key].length})); event.reply(dbot.t('quote_count', {'category': key, 'count': quotes[key].length}));
} else { } else {
dbot.say(data.channel, dbot.t('no_quotes', {'category': q[1]})); event.reply(dbot.t('no_quotes', {'category': key}));
} }
} else { // Give total quote count } else { // Give total quote count
var totalQuoteCount = 0; var totalQuoteCount = 0;
for(var category in quotes) { for(var category in quotes) {
totalQuoteCount += category.length; totalQuoteCount += category.length;
} }
dbot.say(data.channel, dbot.t('total_quotes', {'count': totalQuoteCount})); event.reply(dbot.t('total_quotes', {'count': totalQuoteCount}));
} }
}, },
'~qadd': function(data, params) { '~qadd': function(event) {
var q = data.message.valMatch(/^~qadd ([\d\w\s-]+?)[ ]?=[ ]?(.+)$/, 3); var key = event.input[1].toLowerCase();
if(q) { var text = event.input[2];
key = q[1].toLowerCase();
if(!Object.isArray(quotes[key])) { if(!Object.isArray(quotes[key])) {
quotes[key] = []; quotes[key] = [];
}
if(quotes[key].include(text)) {
event.reply(dbot.t('quote_exists'));
} else { } else {
if (quotes[key].include(q[2])) { quotes[key].push(text);
dbot.say(data.channel, dbot.t('quote_exists'));
return;
}
}
quotes[key].push(q[2]);
addStack.push(q[1]);
rmAllowed = true; rmAllowed = true;
dbot.say(data.channel, dbot.t('quote_saved', {'category': q[1], 'count': quotes[key].length})); event.reply(dbot.t('quote_saved', {'category': key, 'count': quotes[key].length}));
} else {
dbot.say(data.channel, dbot.t('syntax_error'));
} }
}, },
'~qset': function(data, params) { '~rq': function(event) {
var q = data.message.valMatch(/^~qset ([\d\w\s-]*)=(.+)$/, 3);
if(q) {
q[1] = q[1].trim();
key = q[1].toLowerCase();
if(!quotes.hasOwnProperty(key) || (quotes.hasOwnProperty(key) &&
quotes[key].length == 1)) {
quotes[key] = [q[2]];
dbot.say(data.channel, dbot.t('quote_saved', {'category': q[1], 'count': 1}));
} else {
dbot.say(data.channel, dbot.t('quote_replace'));
}
}
},
'~rq': function(data, params) {
var rQuote = Object.keys(quotes).random(); var rQuote = Object.keys(quotes).random();
dbot.say(data.channel, rQuote + ': ' + interpolatedQuote(rQuote)); event.reply(rQuote + ': ' + interpolatedQuote(rQuote));
}, },
'~d': function(data, params) { '~link': function(event) {
dbot.say(data.channel, data.user + ': ' + interpolatedQuote(dbot.name)); var key = event.params[1].trim().toLowerCase();
}, if(quotes.hasOwnProperty(key)) {
event.reply(dbot.t('quote_link', {'category': key}) + ' - http://nc.no.de:443/quotes/' + key);
'~link': function(data, params) {
if(params[1] === undefined || !quotes.hasOwnProperty(params[1].toLowerCase())) {
dbot.say(data.channel, dbot.t('syntax_error'));
} else { } else {
dbot.say(data.channel, dbot.t('quote_link', {'category': params[1]}) + event.reply(dbot.t('category_not_found'));
' - http://nc.no.de:443/quotes/' + params[1]);
} }
}, },
'~qprune': function(data) { '~qprune': function(event) {
var pruned = [] var pruned = []
for(key in quotes) { for(key in quotes) {
if(quotes.hasOwnProperty(key)) { if(quotes.hasOwnProperty(key)) {
@ -274,13 +204,19 @@ var quotes = function(dbot) {
} }
} }
if(pruned.length > 0) { if(pruned.length > 0) {
dbot.say(data.channel, dbot.t('prune', {'categories': pruned.join(", ")})); event.reply(dbot.t('prune', {'categories': pruned.join(", ")}));
} else { } else {
dbot.say(data.channel, dbot.t('no_prune')); event.reply(dbot.t('no_prune'));
} }
} }
}; };
commands['~'].regex = [/^~([\d\w\s-]*)/, 2];
commands['~q'].regex = [/^~q ([\d\w\s-]*)/, 2];
commands['~qsearch'].regex = [/^~qsearch ([\d\w\s-]+?)[ ]?=[ ]?(.+)$/, 3];
commands['~rmlast'].regex = [/^~rmlast ([\d\w\s-]*)/, 2];
commands['~qadd'].regex = [/^~qadd ([\d\w\s-]+?)[ ]?=[ ]?(.+)$/, 3];
return { return {
'onLoad': function() { 'onLoad': function() {
dbot.timers.addTimer(1000 * 60 * 3, function() { dbot.timers.addTimer(1000 * 60 * 3, function() {
@ -289,7 +225,7 @@ var quotes = function(dbot) {
return commands; return commands;
}, },
// For automatic quote retrieval /* For automatic quote retrieval
'listener': function(data, params) { 'listener': function(data, params) {
if((dbot.db.ignores.hasOwnProperty(data.user) && if((dbot.db.ignores.hasOwnProperty(data.user) &&
dbot.db.ignores[data.user].include(name)) == false) { dbot.db.ignores[data.user].include(name)) == false) {
@ -317,7 +253,7 @@ var quotes = function(dbot) {
} }
}, },
'on': 'PRIVMSG', 'on': 'PRIVMSG',*/
'name': name, 'name': name,