-
Notifications
You must be signed in to change notification settings - Fork 97
Add an Urban Dictionary Look up Command
DubbyTT edited this page Nov 3, 2012
·
2 revisions
Just something fun to spam up your chat ;-)
bot.on('speak', function(data) {
// Log chat to the console
if(config.consolelog) {
Log(data.name + ': ' + data.text);
}
data.text = data.text.trim(); // Get rid of any surrounding whitespace
// Respond to "/define" command (uses UrbanDictionary.com)
if(data.text.match(/^\/define/i)) {
var queryResponse = '';
var sSplit = data.text.split("/define");
var searchTerms = sSplit.pop().replace(/\s/g, "%20").replace("%20", "").trim();
Log("SEARCH: " + searchTerms);
// http://api.urbandictionary.com/v0/define?term=one%20two%20three
// Build the API call object
var options = {
host: 'api.urbandictionary.com',
port: 80,
path: '/v0/define?term=' + searchTerms
};
// Call the API
http.get(options, function(response) {
Log("Got response:" + response.statusCode);
response.on('data', function(chunk) {
try {
queryResponse += chunk;
} catch(err) {
bot.speak(err);
}
});
response.on('end', function() {
var ret = JSON.parse(queryResponse);
try {
if(typeof ret.list[0].definition === "undefined") {
Log("DEFINITION: Come on, you're failing at a simple task here.");
bot.speak("DEFINITION: Come on, you're failing at a simple task here.");
} else {
Log("DEFINITION: " + ret.list[0].definition);
bot.speak("DEFINITION: " + ret.list[0].definition);
}
} catch(err) {
Log("Sorry, some kind of error" + err);
}
});
}).on('error', function(e) {
bot.speak("Got error: " + e.message);
});
}
});
by B^Dub (aka DubbyTT) 11-2-2012 "DubbyTT"+"@"+"gmail"+"."+"com"
Thanks to :Pig: for the API URL!