mirror of
				https://github.com/reality/dbot.git
				synced 2025-11-04 15:17:36 +01:00 
			
		
		
		
	Merge github.com:reality/depressionbot into database
Conflicts: modules/poll/commands.js modules/quotes/commands.js
This commit is contained in:
		
						commit
						3a567a35da
					
				@ -7,34 +7,85 @@ var _ = require('underscore')._,
 | 
			
		||||
    request = require('request');
 | 
			
		||||
 | 
			
		||||
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 = { 
 | 
			
		||||
        '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://i.imgur.com/' + 
 | 
			
		||||
                random(5) +
 | 
			
		||||
                '.png';
 | 
			
		||||
                testSlug +
 | 
			
		||||
                '.' + ext[_.random(0, ext.length - 1)];
 | 
			
		||||
            var image = request(testUrl, function(error, response, body) {
 | 
			
		||||
                // 492 is body.length of a removed image
 | 
			
		||||
                if(!error && response.statusCode == 200 && body.length != 492) {
 | 
			
		||||
                    callback(testUrl);
 | 
			
		||||
                    callback(testUrl, testSlug);
 | 
			
		||||
                } else {
 | 
			
		||||
                    this.api.getRandomImage(callback);
 | 
			
		||||
                }
 | 
			
		||||
            }.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 = {
 | 
			
		||||
        '~ri': function(event) {
 | 
			
		||||
            this.api.getRandomImage(function(link) {
 | 
			
		||||
                event.reply(event.user + ': (' + dbot.t('nsfw') + ') ' + link);
 | 
			
		||||
            });
 | 
			
		||||
            this.api.getRandomImage(function(link, slug) {
 | 
			
		||||
                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) {
 | 
			
		||||
 | 
			
		||||
@ -14,7 +14,7 @@ var link = function(dbot) {
 | 
			
		||||
    this.fetchTitle = function(event, link) {
 | 
			
		||||
        var limit = 1000000,
 | 
			
		||||
        size = 0,
 | 
			
		||||
        page = request(link, function(error, response, body) {
 | 
			
		||||
        page = request(link.replace('https', 'http'), function(error, response, body) {
 | 
			
		||||
            if(!error && response.statusCode == 200) {
 | 
			
		||||
                body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
 | 
			
		||||
                var title = body.valMatch(/<title>(.*)<\/title>/, 2);
 | 
			
		||||
 | 
			
		||||
@ -27,11 +27,7 @@ var commands = function(dbot) {
 | 
			
		||||
                    event.reply(dbot.t('poll_created', {
 | 
			
		||||
                        'name': name, 
 | 
			
		||||
                        'description': description, 
 | 
			
		||||
                        'url': dbot.t('url', {
 | 
			
		||||
                            'host': 'test.com',
 | 
			
		||||
                            'port': 80, 
 | 
			
		||||
                            'path': 'polls/' + name
 | 
			
		||||
                        })
 | 
			
		||||
                        'url': dbot.api.web.getUrl('/poll/' + name)     
 | 
			
		||||
                    })); 
 | 
			
		||||
                } else if(err instanceof AlreadyExistsError) {
 | 
			
		||||
                    event.reply(dbot.t('poll_exists', { 'name': name }));
 | 
			
		||||
 | 
			
		||||
@ -33,7 +33,7 @@ var commands = function(dbot){
 | 
			
		||||
            if(event.params[1]){
 | 
			
		||||
                var primary = dbot.api.users.resolveUser(event.server, event.params[1]);
 | 
			
		||||
                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{
 | 
			
		||||
                    event.reply("No profile found for "+event.params[1]);
 | 
			
		||||
 | 
			
		||||
@ -91,7 +91,17 @@ var commands = function(dbot) {
 | 
			
		||||
                { '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) {
 | 
			
		||||
            if(this.rmAllowed === true || _.include(dbot.config.admins, event.user)) {
 | 
			
		||||
                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')) {
 | 
			
		||||
                        event.reply(dbot.t('quote_link', {
 | 
			
		||||
                            'category': key, 
 | 
			
		||||
                            'url': dbot.t('url', {
 | 
			
		||||
                                'host': dbot.config.web.webHost, 
 | 
			
		||||
                                'port': dbot.config.web.webPort, 
 | 
			
		||||
                                'path': 'quotes/' + encodeURIComponent(key)
 | 
			
		||||
                            })
 | 
			
		||||
                            'url': dbot.api.web.getUrl('quotes/' + encodeURIComponent(key))
 | 
			
		||||
                        }));
 | 
			
		||||
                    } else {
 | 
			
		||||
                        event.reply(dbot.t('web_not_configured'));
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
{
 | 
			
		||||
    "webHost": "localhost",
 | 
			
		||||
    "webPort": 8080
 | 
			
		||||
    "webPort": 8080,
 | 
			
		||||
    "externalPath": false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -37,7 +37,17 @@ var webInterface = function(dbot) {
 | 
			
		||||
 | 
			
		||||
    this.onDestroy = function() {
 | 
			
		||||
        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) {
 | 
			
		||||
 | 
			
		||||
@ -10,8 +10,5 @@
 | 
			
		||||
        "es": "No se pudó cargar el módulo: {moduleName}",
 | 
			
		||||
        "na'vi": "Oeru Oel {moduleName}it sung.",
 | 
			
		||||
        "cy": "Wedi methu a llwytho modiwl: {moduleName}"
 | 
			
		||||
    },
 | 
			
		||||
    "url": {
 | 
			
		||||
        "en": "http://{host}:{port}/{path}"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user