Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit f5c8c57

Browse files
author
Tom Bell
committed
Merge pull request #810 from drdamour/master
added a random command to xkcd
2 parents 9d6efa5 + 1129402 commit f5c8c57

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/scripts/xkcd.coffee

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,38 @@
88
# None
99
#
1010
# Commands:
11-
# hubot xkcd - The latest XKCD comic
11+
# hubot xkcd [latest]- The latest XKCD comic
1212
# hubot xkcd <num> - XKCD comic <num>
13+
# hubot xkcd random - XKCD comic <num>
1314
#
1415
# Author:
1516
# twe4ked
1617

1718
module.exports = (robot) ->
18-
robot.respond /xkcd\s?(\d+)?/i, (msg) ->
19-
if msg.match[1] == undefined
20-
num = ''
21-
else
22-
num = "#{msg.match[1]}/"
19+
robot.respond /xkcd(\s+latest)?$/i, (msg) ->
20+
msg.http("http://xkcd.com/info.0.json")
21+
.get() (err, res, body) ->
22+
if res.statusCode == 404
23+
msg.send 'Comic not found.'
24+
else
25+
object = JSON.parse(body)
26+
msg.send object.title, object.img, object.alt
27+
28+
robot.respond /xkcd\s+(\d+)/i, (msg) ->
29+
num = "#{msg.match[1]}"
30+
31+
msg.http("http://xkcd.com/#{num}/info.0.json")
32+
.get() (err, res, body) ->
33+
if res.statusCode == 404
34+
msg.send 'Comic #{num} not found.'
35+
else
36+
object = JSON.parse(body)
37+
msg.send object.title, object.img, object.al
2338

24-
msg.http("http://xkcd.com/#{num}info.0.json")
39+
robot.respond /xkcd\s+random/i, (msg) ->
40+
max = 1100 #max as of 3/2013 TODO: add way to get current max
41+
num = Math.floor((Math.random()*max)+1)
42+
msg.http("http://xkcd.com/#{num}/info.0.json")
2543
.get() (err, res, body) ->
2644
if res.statusCode == 404
2745
msg.send 'Comic not found.'

0 commit comments

Comments
 (0)