Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

61 lines (49 sloc) 1.93 kb
# Description:
# None
# Dependencies:
# None
# Configuration:
# None
# Commands:
# hubot a reddit <subreddit> - A random top (today) post from the specified subreddit. Tries to find a picture if possible
# Author:
# artfuldodger
module.exports = (robot) ->
robot.respond /a reddit( .+)*/i, (msg) ->
reddit msg, msg.match[1]?.trim()
reddit = (msg, subreddit) ->
url = if subreddit? then "{subreddit}/top.json" else ""
.get() (err, res, body) ->
# Sometimes when a subreddit doesn't exist, it wants to redirect you to the search page.
# Oh, and it doesn't send back 302s as JSON
if body?.match(/^302/)?[0] == '302'
msg.send "That subreddit does not seem to exist."
posts = JSON.parse(body)
# If the response has an error attribute, let's get out of here.
if posts.error?
msg.send "That doesn't seem to be a valid subreddit. [http response #{posts.error}]"
unless && > 0
msg.send "While that subreddit exists, there does not seem to be anything there."
post = getPost(posts)
tries_to_find_picture = 0
while post?.domain != "" && tries_to_find_picture < 30
post = getPost(posts)
# Send pictures with the url on one line so Campfire displays it as an image
if post.domain == ''
msg.send "#{post.title} -{post.permalink}"
msg.send post.url
msg.send "#{post.title} - #{post.url} -{post.permalink}"
getPost = (posts) ->
random = Math.round(Math.random() *[random]?.data
Jump to Line
Something went wrong with that request. Please try again.