mirror of
https://github.com/reality/dbot.git
synced 2024-11-27 06:19:24 +01:00
add puush module
This commit is contained in:
parent
f1442edf34
commit
ecec80ac58
69
modules/puush/puush.js
Normal file
69
modules/puush/puush.js
Normal file
@ -0,0 +1,69 @@
|
||||
/**
|
||||
* Module Name: puush
|
||||
* Description: You like the random puush?
|
||||
*/
|
||||
|
||||
var _ = require('underscore')._,
|
||||
request = require('request'),
|
||||
async = require('async');
|
||||
|
||||
var puush = function(dbot) {
|
||||
this.ApiRoot = 'http://puu.sh/';
|
||||
this.rpCache = [];
|
||||
|
||||
this.api = {
|
||||
'getRandomImage': function(callback) {
|
||||
var random = function(len) {
|
||||
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
|
||||
return len ? chars.charAt(~~(Math.random()*chars.length)) + random(len-1) : "";
|
||||
};
|
||||
|
||||
var ext = [ 'gif', 'png', 'jpg' ];
|
||||
var testSlug = random(5);
|
||||
var testUrl = 'http://puu.sh/' +
|
||||
testSlug +
|
||||
'.' + ext[_.random(0, ext.length - 1)];
|
||||
|
||||
request(testUrl, function(error, response, body) {
|
||||
if(!error && response.statusCode === 200 && body.length > 500) {
|
||||
callback(testUrl);
|
||||
} else {
|
||||
this.api.getRandomImage(callback);
|
||||
}
|
||||
}.bind(this));
|
||||
}
|
||||
};
|
||||
|
||||
this.commands = {
|
||||
'~rp': function(event) {
|
||||
var local = event.user;
|
||||
if(event.params[1]) {
|
||||
local = event.params.splice(1, event.params.length - 1).join(' ').trim();
|
||||
}
|
||||
|
||||
if(this.rpCache.length > 0) {
|
||||
event.reply(local + ': ' + this.rpCache.pop());
|
||||
this.api.getRandomImage(function(url) {
|
||||
this.rpCache.push(url);
|
||||
}.bind(this));
|
||||
} else {
|
||||
this.api.getRandomImage(function(url) {
|
||||
event.reply(local + ': ' + url);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.onLoad = function() {
|
||||
async.times(10, function(n, next) {
|
||||
this.api.getRandomImage(function(url) {
|
||||
this.rpCache.push(url);
|
||||
next();
|
||||
}.bind(this));
|
||||
}.bind(this), function() {});
|
||||
}.bind(this);
|
||||
};
|
||||
|
||||
exports.fetch = function(dbot) {
|
||||
return new puush(dbot);
|
||||
};
|
Loading…
Reference in New Issue
Block a user