From 4fa870e34edc1136858b23d89643045ea640f6e6 Mon Sep 17 00:00:00 2001 From: reality Date: Wed, 10 Apr 2013 19:43:11 +0000 Subject: [PATCH] Initial imgur module with ~randomimgur command --- modules/imgur/imgur.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 modules/imgur/imgur.js diff --git a/modules/imgur/imgur.js b/modules/imgur/imgur.js new file mode 100644 index 0000000..2dad0b6 --- /dev/null +++ b/modules/imgur/imgur.js @@ -0,0 +1,36 @@ +/** + * Module Name: imgur + * Description: Various imgur functionality + */ + +var _ = require('underscore')._, + request = require('request'); + +var imgur = function(dbot) { + this.api = { + 'getRandomImage': function(callback) { + var testUrl = 'http://i.imgur.com/' + + Math.random().toString(36).substr(2,6) + + '.jpg'; + var image = request(testUrl, function(error, response, body) { + if(!error && response.statusCode == 200) { + callback(testUrl); + } else { + this.api.getRandomImage(callback); + } + }.bind(this)); + } + }; + + this.commands = { + '~randomimgur': function(event) { + this.api.getRandomImage(function(link) { + event.reply(event.user + ': ' + link); + }); + } + } +}; + +exports.fetch = function(dbot) { + return new imgur(dbot); +}