From 0c8c377b08f3f6c3d3f5c14c9c15cafe07936d23 Mon Sep 17 00:00:00 2001 From: Scritches Date: Thu, 26 Apr 2018 17:14:18 -0400 Subject: [PATCH] ~xkcd command now finds relevant XKCDs when given text instead of a number --- modules/link/link.js | 54 ++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index 2d077ab..d9550be 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -110,23 +110,47 @@ var link = function(dbot) { } catch(err) { }; }); } else { - if(comicId !== "") { - comicId = comicId + "/"; - } - - var link = "http://xkcd.com/"+comicId+"info.0.json"; - request(link, function(error, response, body) { - try { - if (response.statusCode == "200") { - data = JSON.parse(body); - event.reply(dbot.t("xkcd", data)); - } else { - event.reply(dbot.t("no-hits")); + if (isNaN(parseInt(comicId))) { + var relevantUrl = 'https://relevantxkcd.appspot.com/process'; + request({ + url: relevantUrl, + qs: { + action:'xkcd', + query: comicId } - } catch(err) { }; - }); + }, function(err, res, body) { + comicId = body.split(' ').slice(2)[0].trim(); + + var link = "http://xkcd.com/"+comicId+"/info.0.json"; + request(link, function(error, response, body) { + try { + if (response.statusCode == "200") { + data = JSON.parse(body); + event.reply(dbot.t("xkcd", data)); + } else { + event.reply(dbot.t("no-hits")); + } + } catch(err) { }; + }); + }); + } else { + if(comicId !== "") { + comicId = comicId + "/"; + } + + var link = "http://xkcd.com/"+comicId+"info.0.json"; + request(link, function(error, response, body) { + 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) {