3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 22:39:26 +01:00

Made a getQuote API function [#98]

This commit is contained in:
reality 2013-01-03 21:27:06 +00:00
parent 078379b5f1
commit 49528bb434

View File

@ -55,6 +55,26 @@ var quotes = function(dbot) {
} }
}; };
var api = {
'getQuote': function(category) {
var key = category.trim().toLowerCase();
var altKey;
if(key.split(' ').length > 0) {
altKey = key.replace(/ /g, '_');
}
if(key.charAt(0) !== '_') { // lol
if(quotes.hasOwnProperty(key)) {
return interpolatedQuote(key);
} else if(quotes.hasOwnProperty(altKey)) {
return interpolatedQuote(altKey);
} else {
return false;
}
}
}
};
var commands = { var commands = {
// Alternative syntax to ~q // Alternative syntax to ~q
'~': function(event) { '~': function(event) {
@ -101,20 +121,12 @@ var quotes = 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 key = event.input[1].trim().toLowerCase(); var key = event.input[1].trim().toLowerCase();
var altKey; var quote = api.getQuote(event.input[1]);
if(key.split(' ').length > 0) { if(quote) {
altKey = key.replace(/ /g, '_'); event.reply(key + ': ' + quote);
}
if(key.charAt(0) !== '_') { // lol
if(quotes.hasOwnProperty(key)) {
event.reply(key + ': ' + interpolatedQuote(key));
} else if(quotes.hasOwnProperty(altKey)) {
event.reply(altKey + ': ' + interpolatedQuote(altKey));
} else { } else {
event.reply(dbot.t('category_not_found', {'category': key})); event.reply(dbot.t('category_not_found', {'category': key}));
} }
}
}, },
// Shows a list of the biggest categories // Shows a list of the biggest categories
@ -263,6 +275,7 @@ var quotes = function(dbot) {
'name': 'quotes', 'name': 'quotes',
'ignorable': true, 'ignorable': true,
'commands': commands, 'commands': commands,
'api': api,
'listener': function(event) { 'listener': function(event) {
if(event.action == 'PRIVMSG') { if(event.action == 'PRIVMSG') {
@ -279,10 +292,10 @@ var quotes = function(dbot) {
dbot.instance.emit(event); dbot.instance.emit(event);
} }
} else if(event.action == 'JOIN') { } else if(event.action == 'JOIN') {
event.message = '~q ' + event.user; var userQuote = api.getQuote(event.user)
event.action = 'PRIVMSG'; if(userQuote) {
event.params = event.message.split(' '); event.reply(event.user + ': ' + api.getQuote(event.user));
dbot.instance.emit(event); }
} }
}, },
'on': ['PRIVMSG', 'JOIN'] 'on': ['PRIVMSG', 'JOIN']