From 73e9b218242281003f7233a2275393ea8936e3bd Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Wed, 30 Jan 2013 14:19:52 +0000 Subject: [PATCH 01/14] added random xkcd function using "*" as the param Will get a random comic from http://dynamic.xkcd.com/random/comic/ Needs testing --- modules/link/link.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index d684cf3..f36ce71 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -35,22 +35,29 @@ var link = function(dbot) { '~xkcd': function(event) { var comicId = event.params[1]; - if(comicId){ + 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 { + } else { event.reply(dbot.t("no-hits")); } }); }, - + '~ud': function(event) { var query = event.input[1]; var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' + encodeURI(query); From a130c3e744fd22231d52227374e984c30d13fd07 Mon Sep 17 00:00:00 2001 From: Douglas Gardner Date: Wed, 30 Jan 2013 14:57:36 +0000 Subject: [PATCH 02/14] Further automate the install script * config.json is now created in the correct location * config.json is only created if it doesn't already exist * vim launches to edit config.json on first install * User is prompted to run depressionbot directly from the script --- install.sh | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/install.sh b/install.sh index 42cf7ec..8a8d85d 100755 --- a/install.sh +++ b/install.sh @@ -1,5 +1,5 @@ #!/bin/bash - +cat LICENCE git submodule init git submodule update @@ -16,8 +16,20 @@ wget http://d3js.org/d3.v3.zip unzip d3.v3.zip rm d3.v3.zip -cd .. +cd ../.. -cp config.json.sample config.json +if [ ! -f config.json ]; +then + echo 'Creating configuration file...' + cp config.json.sample config.json + vim config.json +fi + +read -p "Setup complete. Run depressionbot now? [y/N]" +if [[ ! $REPLY =~ ^[Yy]$ ]] +then + echo 'Okay. To run the bot, use "node run.js"' + exit +fi +node run.js -echo 'Setup complete. Now edit config.json with your preferences and run the bot with "node run.js"' From 67d7abbf84cc0a67365b22e5ad1562de0fcfbddf Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Wed, 30 Jan 2013 16:50:54 +0000 Subject: [PATCH 03/14] 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]; From 5a83e788206f24cd325d4905a5b82e588fb5bae2 Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Wed, 30 Jan 2013 17:04:27 +0000 Subject: [PATCH 04/14] Reverted file --- config.json.sample | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config.json.sample b/config.json.sample index a3d95e4..05c37a4 100644 --- a/config.json.sample +++ b/config.json.sample @@ -1,13 +1,13 @@ { - "name": "welshtestbot", + "name": "testressionbot", "servers": { "freenode": { - "server": "irc.aberwiki.org", + "server": "irc.freenode.net", "port": 6667, "nickserv": "nickserv", - "password": "", + "password": "lolturtles", "channels": [ - "#lolhax" + "#realitest" ] } }, From bcdca5d002fec4d6f031994825e59c0d108df734 Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Wed, 30 Jan 2013 17:18:07 +0000 Subject: [PATCH 05/14] updated xkcd random to use dbot api instead of function call. --- modules/link/link.js | 53 +++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index 7dca608..9c26a76 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -20,20 +20,7 @@ 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]; @@ -52,21 +39,31 @@ var link = function(dbot) { 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); - comicId++; - comicId = comicId + "/"; - outputComic(comicId,event); - } + 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 + "/"; - outputComic(comicId,event); - } else { - comicId = ""; - outputComic(comicId,event); - } + }else { + 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)); + } else { + event.reply(dbot.t("no-hits")); + } + }); + } }, '~ud': function(event) { From 8d3d5d08a5bcd38b5101e58f613e48918091ecf0 Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Wed, 30 Jan 2013 17:23:53 +0000 Subject: [PATCH 06/14] Update modules/link/link.js indent fix test --- modules/link/link.js | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/link/link.js b/modules/link/link.js index 9c26a76..37d4945 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -88,7 +88,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]); } From 5f0bcd771060fa9241b78ec0b0e47817d51caf0c Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Wed, 30 Jan 2013 17:28:34 +0000 Subject: [PATCH 07/14] Update modules/link/link.js Tab fix attempt 2 --- modules/link/link.js | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index 37d4945..5382f56 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -34,11 +34,10 @@ var link = function(dbot) { }, '~xkcd': function(event) { - var comicId = event.params[1]; - - if(comicId == "*"){ - request("http://xkcd.com/info.0.json", function(error, response, body){ - if (response.statusCode == "200") { + 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); @@ -47,21 +46,21 @@ var link = function(dbot) { event.params = event.message.split(' '); dbot.instance.emit(event); } - }); - }else { - 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)); - } else { - event.reply(dbot.t("no-hits")); - } + }); + }else { + 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)); + } else { + event.reply(dbot.t("no-hits")); + } }); } }, From c3e569be41207c816576e27be7a2777c12d11997 Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Wed, 30 Jan 2013 17:31:06 +0000 Subject: [PATCH 08/14] Update modules/link/link.js Tab fix three: Revenge of the tabs --- modules/link/link.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index 5382f56..cf26bc6 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -61,21 +61,21 @@ var link = function(dbot) { } 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) { } }); } From bf2cff6ebac6da64f1d4622929dcb0b1c6f0a213 Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Thu, 31 Jan 2013 15:41:16 +0000 Subject: [PATCH 09/14] Cleaned up code based on feedback. --- modules/link/link.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index cf26bc6..6c987f6 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -41,10 +41,8 @@ var link = function(dbot) { 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); + event.params[1] = comicId; + dbot.commands['~xkcd'](event); } }); }else { From 451b7ea4d0bf65179f7e8ba0f49b60a3ab47ff71 Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Thu, 31 Jan 2013 15:53:45 +0000 Subject: [PATCH 10/14] Cleaned up link slightly. --- modules/link/link.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index 6c987f6..e9e1c7f 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -39,9 +39,7 @@ var link = function(dbot) { 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.params[1] = comicId; + event.params[1] = (Math.floor(Math.random() * data.num) + 1); dbot.commands['~xkcd'](event); } }); From 4b7c2092b0ffc52fc005c2d3d065ebe42b30ba2b Mon Sep 17 00:00:00 2001 From: Daniel Dowling Date: Thu, 31 Jan 2013 17:20:51 +0000 Subject: [PATCH 11/14] Added check for params[1] to ~xkcd --- modules/link/link.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/link/link.js b/modules/link/link.js index e9e1c7f..2cdce4c 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -34,7 +34,10 @@ var link = function(dbot) { }, '~xkcd': function(event) { - var comicId = event.params[1]; + var comicId; + if(!_.isUndefined(event.params[1])) { + comicId = event.params[1]; + } if(comicId == "*"){ request("http://xkcd.com/info.0.json", function(error, response, body){ if (response.statusCode == "200") { @@ -59,6 +62,7 @@ var link = function(dbot) { } }); } + }, '~ud': function(event) { From d7c193cbe97340a3a2d1777c33ab292c4fe97281 Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 31 Jan 2013 17:46:15 +0000 Subject: [PATCH 12/14] couple of formatting fixes + fix indentation [Close #258] --- jsbot | 2 +- modules/link/link.js | 50 ++++++++++++++++++++++++-------------------- modules/stats | 2 +- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/jsbot b/jsbot index 35910d9..9477dc3 160000 --- a/jsbot +++ b/jsbot @@ -1 +1 @@ -Subproject commit 35910d9025fa3af15b24cecc3f6e7ee897aee4dc +Subproject commit 9477dc33ff1b940f8c07225f00d0648de2a41cb4 diff --git a/modules/link/link.js b/modules/link/link.js index 2cdce4c..f30c015 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,49 +34,53 @@ var link = function(dbot) { }, '~xkcd': function(event) { - var comicId; - if(!_.isUndefined(event.params[1])) { - comicId = event.params[1]; - } - if(comicId == "*"){ + 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); - event.params[1] = (Math.floor(Math.random() * data.num) + 1); - dbot.commands['~xkcd'](event); - } - }); - }else { - if(comicId){ + try { + if(response.statusCode == "200") { + data = JSON.parse(body); + event.params[1] = (Math.floor(Math.random() * data.num) + 1); + dbot.commands['~xkcd'](event); + } + } catch(err) { }; + }); + } else { + 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)); - } else { - event.reply(dbot.t("no-hits")); - } + try { + if (response.statusCode == "200") { + data = JSON.parse(body); + event.reply(dbot.t("xkcd",data)); + } else { + event.reply(dbot.t("no-hits")); + } + } catch(err) { }; }); } }, - + '~ud': function(event) { var query = event.input[1]; var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' + encodeURI(query); + request(reqUrl, function(error, response, body) { - try { + 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.'); } - } catch(err) { } + } catch(err) { } }); } }; diff --git a/modules/stats b/modules/stats index ea795e4..a803be3 160000 --- a/modules/stats +++ b/modules/stats @@ -1 +1 @@ -Subproject commit ea795e4d17aa500923468366e73a10f6fbc94ade +Subproject commit a803be39a19a09bed1fcb0586a882b046ae29c99 From a38f807f224f3c4ec04825e89b7470b31dd9b390 Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 31 Jan 2013 17:47:52 +0000 Subject: [PATCH 13/14] rebump jsbot and that --- jsbot | 2 +- modules/stats | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jsbot b/jsbot index 9477dc3..35910d9 160000 --- a/jsbot +++ b/jsbot @@ -1 +1 @@ -Subproject commit 9477dc33ff1b940f8c07225f00d0648de2a41cb4 +Subproject commit 35910d9025fa3af15b24cecc3f6e7ee897aee4dc diff --git a/modules/stats b/modules/stats index a803be3..171d246 160000 --- a/modules/stats +++ b/modules/stats @@ -1 +1 @@ -Subproject commit a803be39a19a09bed1fcb0586a882b046ae29c99 +Subproject commit 171d246b744ad59a66fcab37b8c9ccde793d9213 From 3bfc82c7bc1839a5d4deefc28273f82e7ebf97ee Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Thu, 31 Jan 2013 18:53:58 +0100 Subject: [PATCH 14/14] remove unnecessary else --- modules/link/link.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index f30c015..b72e8e8 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -47,10 +47,8 @@ var link = function(dbot) { } catch(err) { }; }); } else { - if(comicId) { + if(comicId !== "") { comicId = comicId + "/"; - } else { - comicId = ""; } var link = "http://xkcd.com/"+comicId+"info.0.json";