3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-24 19:52:36 +01:00

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,20 +57,22 @@ 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)) {
if(isBanned(event.user, commandName)) { commandName = '~';
event.reply(dbot.t('command_ban', {'user': event.user})); }
} else {
if(!isIgnoring(event.user, commandName)) { if(isBanned(event.user, commandName)) {
if(applyRegex(commandName, event)) { event.reply(dbot.t('command_ban', {'user': event.user}));
dbot.commands[commandName](event); } else {
dbot.save(); if(!isIgnoring(event.user, commandName)) {
} else { if(applyRegex(commandName, event)) {
event.reply(dbot.t('syntax_error')); dbot.commands[commandName](event);
} dbot.save();
} else {
event.reply(dbot.t('syntax_error'));
} }
} }
} }
}, },
'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();
if(quotes.hasOwnProperty(key)) { // Retrieve quote from a category in the database.
dbot.say(data.channel, q[1] + ': ' + interpolatedQuote(key)); '~q': function(event) {
} else { var key = event.input[1].trim().toLowerCase();
dbot.say(data.channel, dbot.t('category_not_found', {'category': q[1]})); if(quotes.hasOwnProperty(key)) {
} event.reply(key + ': ' + interpolatedQuote(key));
} else {
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(); var matches = [];
if(!quotes.hasOwnProperty(key)) { quotes[haystack].each(function(quote) {
dbot.say(data.channel, dbot.t('empty_category')); if(quote.indexOf(needle) != -1) {
} else { matches.push(quote);
var matches = [];
quotes[key].each(function(quote) {
if(quote.indexOf(params[2]) != -1) {
matches.push(quote);
}
}.bind(this));
if(matches.length == 0) {
dbot.say(data.channel, dbot.t('no_results'));
} else {
dbot.say(data.channel, params[1] + ' (' + params[2] + '): ' + matches.random() + ' [' + matches.length + ' results]');
} }
}.bind(this));
if(matches.length == 0) {
event.reply(dbot.t('no_results'));
} else {
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(); var quote = quotes[key].pop();
if(quotes.hasOwnProperty(q[1])) { if(quotes[key].length === 0) {
if(!dbot.db.locks.include(q[1]) || dbot.admin.include(data.user)) { delete quotes[key];
var quote = quotes[key].pop();
if(quotes[key].length === 0) {
delete quotes[key];
}
rmAllowed = false;
dbot.say(data.channel, '\'' + quote + '\'' +
dbot.t('removed_from') + q[1]);
} else {
dbot.say(data.channel, dbot.t('locked_category', {'category': q[1]}));
} }
rmAllowed = false;
event.reply(dbot.t('removed_from', {'quote': quote, 'category': key}));
} else { } else {
dbot.say(data.channel, dbot.t('no_quotes', {'category': q[1]})); event.reply(dbot.t('locked_category', {'category': q[1]}));
} }
} else { } else {
var last = addStack.pop(); event.reply(dbot.t('no_quotes', {'category': q[1]}));
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 { } else {
dbot.say(data.channel, dbot.t('rmlast_spam')); event.reply(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] = []; }
} else {
if (quotes[key].include(q[2])) { if(quotes[key].include(text)) {
dbot.say(data.channel, dbot.t('quote_exists')); event.reply(dbot.t('quote_exists'));
return;
}
}
quotes[key].push(q[2]);
addStack.push(q[1]);
rmAllowed = true;
dbot.say(data.channel, dbot.t('quote_saved', {'category': q[1], 'count': quotes[key].length}));
} else { } else {
dbot.say(data.channel, dbot.t('syntax_error')); quotes[key].push(text);
rmAllowed = true;
event.reply(dbot.t('quote_saved', {'category': key, 'count': quotes[key].length}));
} }
}, },
'~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) {
dbot.say(data.channel, data.user + ': ' + interpolatedQuote(dbot.name));
}, },
'~link': function(data, params) { '~link': function(event) {
if(params[1] === undefined || !quotes.hasOwnProperty(params[1].toLowerCase())) { var key = event.params[1].trim().toLowerCase();
dbot.say(data.channel, dbot.t('syntax_error')); if(quotes.hasOwnProperty(key)) {
event.reply(dbot.t('quote_link', {'category': key}) + ' - http://nc.no.de:443/quotes/' + key);
} 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,