From a27f789a3731df63173528b6e26530eb46ce61a8 Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 28 Mar 2015 12:55:06 +0000 Subject: [PATCH] get a random post from a given subreddit --- modules/reddit/reddit.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/modules/reddit/reddit.js b/modules/reddit/reddit.js index 699d4ac..977a1c7 100644 --- a/modules/reddit/reddit.js +++ b/modules/reddit/reddit.js @@ -177,6 +177,27 @@ var reddit = function(dbot) { }); }, + '~r': function(event) { + var sr = event.params[1].trim(); + this.api.getNewPosts(sr, 0, function(err, posts) { + if(!err) { + var qPost = posts[_.random(0, posts.length -1)]; + event.reply(dbot.t('about_post', { + 'title': _.unescape(qPost.title.trim()), + 'poster': qPost.author, + 'subreddit': qPost.subreddit, + 'comments': qPost.num_comments, + 'score': qPost.score, + 'up': qPost.ups, + 'down': qPost.downs, + 'url': qPost.url + })); + } else { + event.reply('Could not find that subreddit'); + } + }); + }, + '~addredditfeed': function(event) { var channel = event.input[1], subreddit = event.input[2].replace('r/', ''),