udFallback option for quote retrieval [#477]

This commit is contained in:
reality 2013-05-27 17:16:23 +00:00
parent 0cb8147abb
commit d5a4b288f7
3 changed files with 36 additions and 12 deletions

View File

@ -40,6 +40,23 @@ var link = function(dbot) {
page.abort();
}
});
},
'udLookup': function(query, callback) {
var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' +
encodeURI(query);
request(reqUrl, function(error, response, body) {
try {
var result = JSON.parse(body);
if(_.has(result, 'result_type') && result.result_type != 'no_results') {
callback(result.list[0].word, result.list[0].definition.split('\n')[0]);
} else {
callback(false);
}
} catch(err) { callback(false); }
});
}
};
@ -92,17 +109,13 @@ var link = function(dbot) {
'~ud': function(event) {
var query = event.input[1];
var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' + encodeURI(query);
request(reqUrl, function(error, response, body) {
try {
var result = JSON.parse(body);
if(_.has(result, 'result_type') && result.result_type != 'no_results') {
event.reply(result.list[0].word + ': ' + result.list[0].definition.split('\n')[0]);
this.api.udLookup(query, function(word, definition) {
if(word) {
event.reply(word + ': ' + definition);
} else {
event.reply(event.user + ': No definition found.');
}
} catch(err) { }
});
}
};

View File

@ -48,9 +48,19 @@ var commands = function(dbot) {
var quote = this.api.getQuote(event, event.input[1]);
if(quote) {
event.reply(key + ': ' + quote);
} else {
if(this.config.udFallback === true && _.has(dbot.modules, 'link')) {
dbot.api.link.udLookup(key, function(word, definition) {
if(word) {
event.reply(key + '[UD]: ' + definition);
} else {
event.reply(dbot.t('category_not_found', {'category': key}));
}
});
} else {
event.reply(dbot.t('category_not_found', {'category': key}));
}
}
},
// Shows a list of the biggest categories

View File

@ -1,8 +1,9 @@
{
"dbKeys": [ "quoteArrs" ],
"dependencies": [ "command", "users" ],
"dependencies": [ "command", "users", "event" ],
"rmLimit": 10,
"ignorable": true,
"quotesOnJoin": false,
"udFallback": false,
"help": "http://github.com/reality/depressionbot/blob/master/modules/quotes/README.md"
}