From 67d7abbf84cc0a67365b22e5ad1562de0fcfbddf Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Wed, 30 Jan 2013 16:50:54 +0000 Subject: [PATCH] fixed xkcd random and updated readme for link --- config.json.sample | 8 +++---- modules/link/README.md | 2 +- modules/link/link.js | 53 +++++++++++++++++++++++++----------------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/config.json.sample b/config.json.sample index 05c37a4..a3d95e4 100644 --- a/config.json.sample +++ b/config.json.sample @@ -1,13 +1,13 @@ { - "name": "testressionbot", + "name": "welshtestbot", "servers": { "freenode": { - "server": "irc.freenode.net", + "server": "irc.aberwiki.org", "port": 6667, "nickserv": "nickserv", - "password": "lolturtles", + "password": "", "channels": [ - "#realitest" + "#lolhax" ] } }, 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 f36ce71..7dca608 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -20,7 +20,20 @@ var link = function(dbot) { } }); }; - + + function outputComic(comicId,event){ + 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")); + } + }); + } + + var commands = { '~title': function(event) { var link = this.links[event.channel.name]; @@ -35,28 +48,26 @@ 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"; if(comicId == "*"){ - link = "http://dynamic.xkcd.com/random/comic"; - request(link, function(error, response, body) - link = response.location + "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")); - } - }); - }, + 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); + comicId++; + comicId = comicId + "/"; + outputComic(comicId,event); + } + }); + }else if(comicId){ + comicId = comicId + "/"; + outputComic(comicId,event); + } else { + comicId = ""; + outputComic(comicId,event); + } + }, '~ud': function(event) { var query = event.input[1];