forked from GitHub/dbot
Merge github.com:reality/depressionbot into database
Conflicts: modules/poll/commands.js modules/quotes/commands.js
This commit is contained in:
commit
3a567a35da
@ -7,6 +7,27 @@ var _ = require('underscore')._,
|
|||||||
request = require('request');
|
request = require('request');
|
||||||
|
|
||||||
var imgur = function(dbot) {
|
var imgur = function(dbot) {
|
||||||
|
this.internalAPI = {
|
||||||
|
'infoString': function(imgData) {
|
||||||
|
info = null;
|
||||||
|
if(imgData && _.has(imgData, 'data')) {
|
||||||
|
imgData = imgData.data;
|
||||||
|
info = '[';
|
||||||
|
if(imgData.title) {
|
||||||
|
info += imgData.title + ' is ';
|
||||||
|
}
|
||||||
|
if(imgData.animated) {
|
||||||
|
info += 'an animated ' + imgData.type.split('/')[1] + ' with ';
|
||||||
|
} else {
|
||||||
|
info += 'a non-animated ' + imgData.type.split('/')[1] + ' with ';
|
||||||
|
}
|
||||||
|
info += imgData.views + ' views].';
|
||||||
|
}
|
||||||
|
|
||||||
|
return info;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
this.api = {
|
this.api = {
|
||||||
'getRandomImage': function(callback) {
|
'getRandomImage': function(callback) {
|
||||||
var random = function(len) {
|
var random = function(len) {
|
||||||
@ -14,27 +35,57 @@ var imgur = function(dbot) {
|
|||||||
return len ? chars.charAt(~~(Math.random()*chars.length)) + random(len-1) : "";
|
return len ? chars.charAt(~~(Math.random()*chars.length)) + random(len-1) : "";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var ext = [ 'gif', 'png', 'jpg' ];
|
||||||
|
var testSlug = random(5);
|
||||||
var testUrl = 'http://i.imgur.com/' +
|
var testUrl = 'http://i.imgur.com/' +
|
||||||
random(5) +
|
testSlug +
|
||||||
'.png';
|
'.' + ext[_.random(0, ext.length - 1)];
|
||||||
var image = request(testUrl, function(error, response, body) {
|
var image = request(testUrl, function(error, response, body) {
|
||||||
// 492 is body.length of a removed image
|
// 492 is body.length of a removed image
|
||||||
if(!error && response.statusCode == 200 && body.length != 492) {
|
if(!error && response.statusCode == 200 && body.length != 492) {
|
||||||
callback(testUrl);
|
callback(testUrl, testSlug);
|
||||||
} else {
|
} else {
|
||||||
this.api.getRandomImage(callback);
|
this.api.getRandomImage(callback);
|
||||||
}
|
}
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
},
|
||||||
|
|
||||||
|
'getImageInfo': function(slug, callback) {
|
||||||
|
request.get({
|
||||||
|
'url': 'https://api.imgur.com/3/image/' + slug + '.json',
|
||||||
|
'json': true,
|
||||||
|
'headers': {
|
||||||
|
'Authorization': 'Client-ID 86fd3a8da348b65'
|
||||||
|
}
|
||||||
|
}, function(err, response, body) {
|
||||||
|
callback(body);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.commands = {
|
this.commands = {
|
||||||
'~ri': function(event) {
|
'~ri': function(event) {
|
||||||
this.api.getRandomImage(function(link) {
|
this.api.getRandomImage(function(link, slug) {
|
||||||
event.reply(event.user + ': (' + dbot.t('nsfw') + ') ' + link);
|
this.api.getImageInfo(slug, function(imgData) {
|
||||||
});
|
var info = this.internalAPI.infoString(imgData);
|
||||||
|
event.reply(event.user + ': ' + link + ' ' + info);
|
||||||
|
}.bind(this));
|
||||||
|
}.bind(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.onLoad = function() {
|
||||||
|
var imgurHandler = function(event, matches, name) {
|
||||||
|
if(matches[2]) { // TODO: handle this in the regex
|
||||||
|
this.api.getImageInfo(matches[1], function(imgData) {
|
||||||
|
var info = this.internalAPI.infoString(imgData);
|
||||||
|
if(info) event.reply(info);
|
||||||
|
}.bind(this));
|
||||||
|
}
|
||||||
|
}.bind(this);
|
||||||
|
dbot.api.link.addHandler(this.name, /http:\/\/i\.imgur\.com\/([a-zA-Z0-9]+)\.([jpg|png|gif])/, imgurHandler);
|
||||||
|
dbot.api.link.addHandler(this.name, /\bhttps?:\/\/imgur\.com\/([a-zA-Z0-9]+)\b/i, imgurHandler);
|
||||||
|
}.bind(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.fetch = function(dbot) {
|
exports.fetch = function(dbot) {
|
||||||
|
@ -14,7 +14,7 @@ var link = function(dbot) {
|
|||||||
this.fetchTitle = function(event, link) {
|
this.fetchTitle = function(event, link) {
|
||||||
var limit = 1000000,
|
var limit = 1000000,
|
||||||
size = 0,
|
size = 0,
|
||||||
page = request(link, function(error, response, body) {
|
page = request(link.replace('https', 'http'), function(error, response, body) {
|
||||||
if(!error && response.statusCode == 200) {
|
if(!error && response.statusCode == 200) {
|
||||||
body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
|
body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
|
||||||
var title = body.valMatch(/<title>(.*)<\/title>/, 2);
|
var title = body.valMatch(/<title>(.*)<\/title>/, 2);
|
||||||
|
@ -27,11 +27,7 @@ var commands = function(dbot) {
|
|||||||
event.reply(dbot.t('poll_created', {
|
event.reply(dbot.t('poll_created', {
|
||||||
'name': name,
|
'name': name,
|
||||||
'description': description,
|
'description': description,
|
||||||
'url': dbot.t('url', {
|
'url': dbot.api.web.getUrl('/poll/' + name)
|
||||||
'host': 'test.com',
|
|
||||||
'port': 80,
|
|
||||||
'path': 'polls/' + name
|
|
||||||
})
|
|
||||||
}));
|
}));
|
||||||
} else if(err instanceof AlreadyExistsError) {
|
} else if(err instanceof AlreadyExistsError) {
|
||||||
event.reply(dbot.t('poll_exists', { 'name': name }));
|
event.reply(dbot.t('poll_exists', { 'name': name }));
|
||||||
|
@ -33,7 +33,7 @@ var commands = function(dbot){
|
|||||||
if(event.params[1]){
|
if(event.params[1]){
|
||||||
var primary = dbot.api.users.resolveUser(event.server, event.params[1]);
|
var primary = dbot.api.users.resolveUser(event.server, event.params[1]);
|
||||||
if(_.has(dbot.db.profiles[event.server], primary.toLowerCase())){
|
if(_.has(dbot.db.profiles[event.server], primary.toLowerCase())){
|
||||||
event.reply("http://"+dbot.config.web.webHost+":"+dbot.config.web.webPort+"/profile/"+event.server+"/"+primary.toLowerCase());
|
event.reply(dbot.api.web.getUrl("/profile/"+event.server+"/"+primary.toLowerCase());
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
event.reply("No profile found for "+event.params[1]);
|
event.reply("No profile found for "+event.params[1]);
|
||||||
|
@ -91,7 +91,17 @@ var commands = function(dbot) {
|
|||||||
{ 'count': rmCacheCount }));
|
{ 'count': rmCacheCount }));
|
||||||
},
|
},
|
||||||
|
|
||||||
// Remove last quote from category
|
// Retrieve quote from a category in the database.
|
||||||
|
'~q': function(event) {
|
||||||
|
var key = event.input[1].trim().toLowerCase();
|
||||||
|
var quote = this.api.getQuote(event, event.input[1]);
|
||||||
|
if(quote) {
|
||||||
|
event.reply(key + ': ' + quote);
|
||||||
|
} else {
|
||||||
|
event.reply(dbot.t('category_not_found', {'category': key}));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
'~rmlast': function(event) {
|
'~rmlast': function(event) {
|
||||||
if(this.rmAllowed === true || _.include(dbot.config.admins, event.user)) {
|
if(this.rmAllowed === true || _.include(dbot.config.admins, event.user)) {
|
||||||
var key = event.input[1].trim().toLowerCase(),
|
var key = event.input[1].trim().toLowerCase(),
|
||||||
@ -352,11 +362,7 @@ var commands = function(dbot) {
|
|||||||
if(_.has(dbot.config, 'web') && _.has(dbot.config.web, 'webHost')) {
|
if(_.has(dbot.config, 'web') && _.has(dbot.config.web, 'webHost')) {
|
||||||
event.reply(dbot.t('quote_link', {
|
event.reply(dbot.t('quote_link', {
|
||||||
'category': key,
|
'category': key,
|
||||||
'url': dbot.t('url', {
|
'url': dbot.api.web.getUrl('quotes/' + encodeURIComponent(key))
|
||||||
'host': dbot.config.web.webHost,
|
|
||||||
'port': dbot.config.web.webPort,
|
|
||||||
'path': 'quotes/' + encodeURIComponent(key)
|
|
||||||
})
|
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
event.reply(dbot.t('web_not_configured'));
|
event.reply(dbot.t('web_not_configured'));
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"webHost": "localhost",
|
"webHost": "localhost",
|
||||||
"webPort": 8080
|
"webPort": 8080,
|
||||||
|
"externalPath": false
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,17 @@ var webInterface = function(dbot) {
|
|||||||
|
|
||||||
this.onDestroy = function() {
|
this.onDestroy = function() {
|
||||||
server.close();
|
server.close();
|
||||||
}
|
};
|
||||||
|
|
||||||
|
this.api = {
|
||||||
|
'getUrl': function(path) {
|
||||||
|
if(this.config.externalPath) {
|
||||||
|
return this.config.externalPath + '/' + path;
|
||||||
|
} else {
|
||||||
|
return 'http://' + this.config.webHost + ':' + port + '/' + path;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.fetch = function(dbot) {
|
exports.fetch = function(dbot) {
|
||||||
|
@ -10,8 +10,5 @@
|
|||||||
"es": "No se pudó cargar el módulo: {moduleName}",
|
"es": "No se pudó cargar el módulo: {moduleName}",
|
||||||
"na'vi": "Oeru Oel {moduleName}it sung.",
|
"na'vi": "Oeru Oel {moduleName}it sung.",
|
||||||
"cy": "Wedi methu a llwytho modiwl: {moduleName}"
|
"cy": "Wedi methu a llwytho modiwl: {moduleName}"
|
||||||
},
|
|
||||||
"url": {
|
|
||||||
"en": "http://{host}:{port}/{path}"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user