Basic Reddit module which posts info about Reddit links posted in the channel [#293]

This commit is contained in:
reality 2013-04-13 16:54:48 +00:00
parent 8151122b80
commit a881e60c81
2 changed files with 51 additions and 0 deletions

46
modules/reddit/reddit.js Normal file
View File

@ -0,0 +1,46 @@
/**
* Module Name: reddit
* Description: Various reddit functionality
*/
var _ = require('underscore')._,
request = require('request');
var reddit = function(dbot) {
this.ApiRoot = 'http://reddit.com/';
this.api = {
'getSubredditInfo': function(name, callback) {
request.get({
'url': this.ApiRoot + 'r/' + name + '/about.json',
'json': true,
'headers': {
'User-Agent': 'dbot by u/realitone'
}
}, function(err, response, body) {
callback(body);
});
}
};
this.onLoad = function() {
var srHandler = function(event, matches, name) {
this.api.getSubredditInfo(matches[1], function(info) {
if(info.data) {
info = info.data;
var infoString = dbot.t('about_subreddit', {
'display_name': info.display_name,
'subscribers': info.subscribers,
'active': info.accounts_active
});
if(info.over18) infoString += ' [NSFW]';
event.reply(infoString);
}
});
}.bind(this);
dbot.api.link.addHandler(this.name, /https?:\/\/reddit\.com\/r\/([a-zA-Z0-9]+)/, srHandler);
}.bind(this);
};
exports.fetch = function(dbot) {
return new reddit(dbot);
}

View File

@ -0,0 +1,5 @@
{
"about_subreddit": {
"en": "[{display_name} has {subscribers} subscribers ({active} active)]"
}
}