diff --git a/modules/link/README.md b/modules/link/README.md index 21de8ef..fed0a33 100644 --- a/modules/link/README.md +++ b/modules/link/README.md @@ -22,4 +22,4 @@ which was posted in the current channel. #### ~ud [headword] Returns the first [Urban Dictionary](http://www.urbandictionary.com) definition for the headword provided. #### ~xkcd -Returns a link to the [xkcd](http://xkcd.com) comic specified, or the latest one if a comic is not given. +Returns a link to the [xkcd](http://xkcd.com) comic specified, or the latest one if a comic is not given. Use '*' to return a link to a random comic. diff --git a/modules/link/link.js b/modules/link/link.js index d684cf3..cf26bc6 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -20,7 +20,7 @@ var link = function(dbot) { } }); }; - + var commands = { '~title': function(event) { var link = this.links[event.channel.name]; @@ -34,34 +34,48 @@ var link = function(dbot) { }, '~xkcd': function(event) { - var comicId = event.params[1]; - if(comicId){ - comicId = comicId + "/"; - } else { - comicId = ""; - } - var link = "http://xkcd.com/"+comicId+"info.0.json"; - request(link, function(error, response, body) { - if (response.statusCode == "200") { - data = JSON.parse(body); - event.reply(dbot.t("xkcd",data)); + var comicId = event.params[1]; + if(comicId == "*"){ + request("http://xkcd.com/info.0.json", function(error, response, body){ + if (response.statusCode == "200") { + data = JSON.parse(body); + comicId = data.num; + comicId = (Math.floor(Math.random() * comicId) + 1); + event.message = '~xkcd ' + comicId; + event.action = 'PRIVMSG'; + event.params = event.message.split(' '); + dbot.instance.emit(event); + } + }); + }else { + if(comicId){ + comicId = comicId + "/"; } else { - event.reply(dbot.t("no-hits")); + comicId = ""; } - }); + var link = "http://xkcd.com/"+comicId+"info.0.json"; + request(link, function(error, response, body) { + if (response.statusCode == "200") { + data = JSON.parse(body); + event.reply(dbot.t("xkcd",data)); + } else { + event.reply(dbot.t("no-hits")); + } + }); + } }, - + '~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) { try { - var result = JSON.parse(body); - if(_.has(result, 'result_type') && result.result_type != 'no_results') { - event.reply(query + ': ' + result.list[0].definition.split('\n')[0]); - } else { - event.reply(event.user + ': No definition found.'); - } + var result = JSON.parse(body); + if(_.has(result, 'result_type') && result.result_type != 'no_results') { + event.reply(query + ': ' + result.list[0].definition.split('\n')[0]); + } else { + event.reply(event.user + ': No definition found.'); + } } catch(err) { } }); } @@ -73,7 +87,6 @@ var link = function(dbot) { var urlMatches = event.message.match(this.urlRegex); if(urlMatches !== null) { this.links[event.channel.name] = urlMatches[0]; - if(dbot.config.link.autoTitle == true) { this.fetchTitle(event, urlMatches[0]); }