3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-30 16:09:27 +01:00

merge udFallback into database branch [#477]

This commit is contained in:
reality 2013-05-27 17:33:10 +00:00
commit e1ea156a41
3 changed files with 37 additions and 13 deletions

View File

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

View File

@ -38,9 +38,19 @@ var commands = function(dbot) {
if(quote) { if(quote) {
event.reply(key + ': ' + quote); event.reply(key + ': ' + quote);
} else { } else {
event.reply(dbot.t('category_not_found', { 'category': key })); 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 }));
}
} }
}); }.bind(this));
}, },
// Choose a random quote category and a random quote from that // Choose a random quote category and a random quote from that

View File

@ -3,5 +3,6 @@
"dependencies": [ "users", "event" ], "dependencies": [ "users", "event" ],
"rmLimit": 10, "rmLimit": 10,
"ignorable": true, "ignorable": true,
"quotesOnJoin": false "quotesOnJoin": false,
"udFallback": false
} }