diff --git a/community_modules/badges.js b/community_modules/badges.js index 43e2a20..01a62d7 100644 --- a/community_modules/badges.js +++ b/community_modules/badges.js @@ -8,11 +8,12 @@ * - implement todo's by saying something to the user * - point to css code in SetCssImage function */ - +var fs = require('fs'); var ranks = require('../ranks'); var lwip = require('lwip'); var request = require('request'); var badges_target_resolution = 30; +var badge_dir = __dirname + "/badges"; function Badges(module, bot) { var that = this; @@ -20,101 +21,241 @@ function Badges(module, bot) { this.bot = bot; this.module.SetName('badges'); - function SetCssImage(user, filename, custom) { - //user is the user object + fs.mkdir(badge_dir, function (err) { + if (err && err.code !== 'EEXIST') { + console.error(err); //something went wrong creating the required directory and it doesn't exist' + } + }); + + function SetCssImage(from, filename, custom, user) { + //from is the user object from the sender + //user is (if not undefined) the user whos badge has to be set //custom == true set the url to the image on this server //custom == false delete the custom css entry - var username = that.bot.connection.escape(user.username); - if (custom) { - filename = filename.replace(__dirname, ''); - that.bot.query('INSERT INTO users SET username=' + username + ', time=' + new Date().valueOf() + ', id=' + user.id + ', badgeurl=\'' + filename + '\' ON DUPLICATE KEY UPDATE badgeurl=\'' + filename + '\';'); - that.bot.cache.put('badges', user.id, { - url: filename, - extraCss: {} - }); - - that.bot.sendChat('[@' + user.username + '] Your badge has been set.'); - } - else { - that.bot.query('INSERT INTO users SET username=' + username + ', time=' + new Date().valueOf() + ', id=' + user.id + ' ON DUPLICATE KEY UPDATE badgeurl=\'\';'); - that.bot.cache.delete('badges', user.id); - - that.bot.sendChat('[@' + user.username + '] Your badge has been removed.'); - } + var isadmin = (user !== undefined); + var username, userid; + if (isadmin) { + username = that.bot.connection.escape(user.username); + userid = user.id; + } + else { + username = that.bot.connection.escape(from.username); + userid = from.id; + } + if (custom) { + filename = filename.replace(__dirname, ''); + that.bot.query('INSERT INTO users SET username=' + username + ', time=' + new Date().valueOf() + ', id=' + userid + ', badgeurl=\'' + filename + '\' ON DUPLICATE KEY UPDATE badgeurl=\'' + filename + '\';'); + that.bot.cache.put('badges', userid, { + url: filename, + extraCss: {} + }); + + if (isadmin) { + that.bot.sendChat('[@' + from.username + '] The badge of ' + user.username + ' has been set. Type /reload to see the new badge.'); + return; + } + + that.bot.sendChat('[@' + from.username + '] Your badge has been set. Type /reload to see the new badge.'); + } + else { + that.bot.query('INSERT INTO users SET username=' + username + ', time=' + new Date().valueOf() + ', id=' + userid + ' ON DUPLICATE KEY UPDATE badgeurl=\'\';'); + that.bot.cache.delete('badges', userid); + + if (isadmin) { + that.bot.sendChat('[@' + from.username + '] The badge of ' + user.username + ' has been removed. Type /reload to see the new badge.'); + return; + } + + that.bot.sendChat('[@' + from.username + '] Your badge has been removed. Type /reload to see the new badge.'); + } } - // this.module.RegisterCommand(commandName[string], userRole[int], callback[function], isBeta[bool], supportsDiscord[bool]) - this.module.RegisterCommand('setbadge', ranks.RDJ, function (data) { + function AddExtraTask(task, options, batch) { + if (task === "resize") { + if (!options.resize) { + options.resize = [badges_target_resolution, badges_target_resolution]; + } + var width = options.resize[0] || badges_target_resolution; + var height = options.resize[1] || width; + if (width > options.width || height > options.height) { + return { error: "resize can only shrink the image not enlarge it" }; + } + options.width = width; + options.height = height; + return batch.resize(width, height, options.inter[0]); + } + else if (task === "contain") { + if (!options.contain) { + options.contain = [badges_target_resolution, badges_target_resolution]; + } + var width = options.contain[0] || badges_target_resolution; + var height = options.contain[1] || width; + if (width > options.width || height > options.height) { + return { error: "contain can only shrink the image not enlarge it" }; + } + options.width = width; + options.height = height; + return batch.contain(width, height, options.backcolor, options.inter[0]); + } + else if (task === "cover") { + if (!options.cover) { + options.cover = [badges_target_resolution, badges_target_resolution]; + } + var width = options.cover[0] || badges_target_resolution; + var height = options.cover[1] || width; + if (width > options.width || height > options.height) { + return { error: "cover can only shrink the image not enlarge it" }; + } + options.width = width; + options.height = height; + return batch.cover(width, height, options.inter[0]); + } + else if (task === "rotate") { + if (!options.rotate || !options.rotate[0]) { + return { error: "Please specify a rotation when using rotate" }; + } + var deg = Number(options.rotate[0]); + if (deg === NaN || deg < 1 || deg > 359) { + return { error: "Rotation was not a number or between 0 and 360" }; + } + return batch.rotate(deg, options.backcolor); + } + else if (task === "border") { + if (!options.border || options.border.length < 2) { + return { error: "Please specify a width and color when using border" }; + } + var width = Number(options.border[0]); + if (width === NaN || width < 1) { + return { error: "Width was not a number or bigger than 0" }; + } + var color = options.border[1]; + if (options.border.length > 2) { + color = options.border.slice(1); + } + options.width += width * 2; + options.height += width * 2; + return batch.border(width, color); + } + else if (task === "pad") { + if (!options.pad || options.pad.length < 5) { + return { error: "Please specify a width for left, top, right, bottom and a color when using border" }; + } + var left, top, right, bottom; + left = Number(options.pad[0]); + top = Number(options.pad[1]); + right = Number(options.pad[2]); + bottom = Number(options.pad[3]); + if (left === NaN || top === NaN || right === NaN || bottom === NaN) { + return { error: "one of the padding numbers wasn't valid" }; + } + var color = options.pad[4]; + if (options.pad.length > 5) { + color = options.pad.slice(4); + } + return batch.pad(left, top, right, bottom, color); + } + return batch; + } + + function SetBadge(args, data, user) { var userid = data.raw.uid; - var arguments = data.message.substr(9).trim().split(' '); + if (user) { + userid = user.id; + } var options = { - url: arguments[0], - type: "jpg", - inter: "lanczos", + url: args[0], + type: ["jpg"], + inter: ["lanczos"], mode: "square", - backcolor: [0, 0, 0, 0] + backcolor: [0, 0, 0, 0], + extratasks: [] } if (options.url == 'none' || options.url == 'default' || options.url == 'nothing') { - SetCssImage(data.from, null, false); - return; + SetCssImage(data.from, null, false, user); + return; } - for (var i = 1; i < arguments.length; i++) { - if (arguments[i].indexOf(':') > 0) { - var sp = arguments[i].split(':'); - options[sp[0]] = sp[1]; + for (var i = 1; i < args.length; i++) { + if (args[i].indexOf(':') > 0) { + var sp = args[i].split(':'); + options[sp[0]] = sp[1].split(','); + options.extratasks.push(sp[0]); } - else if (arguments[i] == 'crop' || arguments[i] == 'cover' || arguments[i] == 'contain') { - options.mode = arguments[i]; + else if (args[i] == 'crop' || args[i] == 'cover' || args[i] == 'contain') { + options.mode = args[i]; + options.extratasks.push(args[i]); } + else { + options.extratasks.push(args[i]); + } + } + + if (options.backcolor.length === 1) { + options.backcolor = options.backcolor[0]; + } + + if (options.type[0] == "gif" && data.userrank < ranks.Manager) { + that.bot.sendChat(data.issuer + 'Only managers can use gifs in their badges.'); } - if (options.url.endsWith(".jpg") || options.url.endsWith(".png")) { + + if (options.url.endsWith(".jpg") || options.url.endsWith(".png") || options.url.endsWith(".gif")) { request({ url: options.url, encoding: null }, function (error, response, body) { if (!error && response.statusCode == 200) { lwip.open(body, options.url.substr(-3), function (err, image) { - var width = image.width(); - var height = image.height(); - if (options.mode == "square" && width != height) { - that.bot.sendChat(data.issuer + 'Can\'t set badge. Both the height and width must be the same!'); + if (err){ + that.bot.sendChat(data.issuer + 'Can\'t set badge. There is a problem with the image.'); return; } - var targetfilename = __dirname + "/badges/" + userid + '.' + options.type; - if (width > badges_target_resolution || height > badges_target_resolution) { - var callback = function (err, image) { - if (err !== undefined && err != null) { - that.bot.sendChat(data.issuer + 'I\'ve failed to download your badge.'); - return; + options.width = image.width(); + options.height = image.height(); + var targetfilename = badge_dir + "/" + userid + '.' + options.type[0]; + if (options.type[0] == "gif") { + if (options.width != options.height || height > badges_target_resolution) { + that.bot.sendChat(data.issuer + 'Can\'t set badge. Both the height and width of the gif must be the same and under ' + badges_target_resolution + ' pixels!'); + return; + } + fs.writeFile(targetfilename, body, (err) => { + if (err) { + that.bot.sendChat(data.issuer + 'I\'ve failed to download and update your badge.'); } - image.writeFile(targetfilename, function (err) { - if (err !== undefined && err != null) { - that.bot.sendChat(data.issuer + 'I\'ve failed to download and update your badge.'); - console.log(err); - console.log(targetfilename); - return; - } - SetCssImage(data.from, targetfilename, true); - }); - }; + SetCssImage(data.from, targetfilename, true, user); + }); + } + + var batch = image.batch(); + + for (var o = 0; o < options.extratasks.length && !batch.error; o++) { + batch = AddExtraTask(options.extratasks[o], options, batch); + } + + if (batch.error) { + //these are just restrictions represented as an error no need to log or throw + that.bot.sendChat(data.issuer + 'I\'ve failed to download and update your badge: ' + batch.error); + } + + if (options.width > badges_target_resolution || options.height > badges_target_resolution) { if (options.mode == "square" || options.mode == "crop" || options.mode == "cover") { - image.cover(badges_target_resolution, badges_target_resolution, options.inter, callback); + if (options.width != options.height && options.mode == "square") { + that.bot.sendChat(data.issuer + 'Can\'t set badge. Both the height and width must be the same!'); + return; + } + batch = batch.cover(badges_target_resolution, badges_target_resolution, options.inter[0]); } else if (options.mode == "contain") { - image.contain(badges_target_resolution, badges_target_resolution, options.backcolor, options.inter, callback); + batch = batch.contain(badges_target_resolution, badges_target_resolution, options.backcolor, options.inter[0]); } else { that.bot.sendChat(data.issuer + 'The mode you are trying to use does not exist. If you don\'t know what this does please do not mess with it.'); } } - else { - image.writeFile(targetfilename, function (err) { - if (err !== undefined && err != null) { - that.bot.sendChat(data.issuer + 'I\'ve failed to download and update your badge.'); - console.log(err); - console.log(targetfilename); - return; - } - SetCssImage(data.from, targetfilename, true); - }); - } + batch.writeFile(targetfilename, function (err) { + if (err !== undefined && err != null) { + that.bot.sendChat(data.issuer + 'I\'ve failed to download and update your badge.'); + console.log(err); + console.log(targetfilename); + return; + } + SetCssImage(data.from, targetfilename, true, user); + }); }); } else { @@ -125,6 +266,22 @@ function Badges(module, bot) { else { that.bot.sendChat(data.issuer + 'Bad link detected.'); } + } + + // this.module.RegisterCommand(commandName[string], userRole[int], callback[function], isBeta[bool], supportsDiscord[bool]) + this.module.RegisterCommand('setbadge', ranks.RDJ, function (data) { + var args = data.message.substr(9).trim().split(' '); + SetBadge(args, data); + }); + + this.module.RegisterCommand('adminsetbadge', ranks.Manager, function (data) { + var args = data.message.substr(9).trim().split(' '); + var userid = Number(args.shift()); + if (userid === NaN) { + that.bot.sendChat(data.issuer + 'Bad userid.'); + } + var user = that.bot.getUser(userid); + SetBadge(args, data, user); }); };