|
8 | 8 | # None
|
9 | 9 | #
|
10 | 10 | # Commands:
|
11 |
| -# hubot xkcd - The latest XKCD comic |
| 11 | +# hubot xkcd [latest]- The latest XKCD comic |
12 | 12 | # hubot xkcd <num> - XKCD comic <num>
|
| 13 | +# hubot xkcd random - XKCD comic <num> |
13 | 14 | #
|
14 | 15 | # Author:
|
15 | 16 | # twe4ked
|
16 | 17 |
|
17 | 18 | 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 |
23 | 38 |
|
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") |
25 | 43 | .get() (err, res, body) ->
|
26 | 44 | if res.statusCode == 404
|
27 | 45 | msg.send 'Comic not found.'
|
|
0 commit comments