From fba8edef18a8b5b800002c3c9302750d9ef5f9a9 Mon Sep 17 00:00:00 2001 From: reality Date: Tue, 22 Jan 2013 19:52:26 +0000 Subject: [PATCH] ~ud was surprisingly easy [#192] --- modules/link/link.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/link/link.js b/modules/link/link.js index cbd9c2a..0efbd02 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -10,7 +10,7 @@ var link = function(dbot) { this.urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; this.links = {}; this.fetchTitle = function(event, link) { - request(link, function (error, response, body) { + request(link, function(error, response, body) { if(!error && response.statusCode == 200) { body = body.replace(/(\r\n|\n\r|\n)/gm, " "); var title = body.valMatch(/(.*)<\/title>/, 2); @@ -31,6 +31,18 @@ var link = function(dbot) { } } this.fetchTitle(event, link); + }, + + '~ud': function(event) { + var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' + event.params[1]; + request(reqUrl, function(error, response, body) { + var result = JSON.parse(body); + if(_.has(result, 'result_type') && result.result_type != 'no_results') { + event.reply(event.params[1] + ': ' + result.list[0].definition); + } else { + event.reply(event.user + ': No definition found.'); + } + }); } }; this.commands = commands;