diff --git a/assets/fonts/Impact.ttf b/assets/fonts/Impact.ttf new file mode 100644 index 00000000..114e6c19 Binary files /dev/null and b/assets/fonts/Impact.ttf differ diff --git a/commands/image-edit/meme-gen.js b/commands/image-edit/meme-gen.js index 1ec6832f..a06842fa 100644 --- a/commands/image-edit/meme-gen.js +++ b/commands/image-edit/meme-gen.js @@ -1,5 +1,9 @@ const Command = require('../../structures/Command'); +const { createCanvas, loadImage, registerFont } = require('canvas'); const request = require('node-superfetch'); +const path = require('path'); +const { wrapText } = require('../../util/Canvas'); +registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Impact.ttf'), { family: 'Impact' }); module.exports = class MemeGenCommand extends Command { constructor(client) { @@ -9,38 +13,58 @@ module.exports = class MemeGenCommand extends Command { group: 'image-edit', memberName: 'meme-gen', description: 'Sends a meme with the text and background of your choice.', + throttling: { + usages: 1, + duration: 10 + }, clientPermissions: ['ATTACH_FILES'], args: [ - { - key: 'type', - prompt: 'What meme type do you want to use?', - type: 'string', - parse: type => encodeURIComponent(type) - }, { key: 'top', - prompt: 'What should the top row of the meme to be?', + prompt: 'What should the something to believe in be?', type: 'string', - max: 200, - parse: top => encodeURIComponent(top) + max: 50 }, { key: 'bottom', - prompt: 'What should the bottom row of the meme to be?', + prompt: 'What should believing result in (e.g. sacrificing everything)?', type: 'string', - max: 200, - parse: bottom => encodeURIComponent(bottom) + max: 50 + }, + { + key: 'image', + prompt: 'What image would you like to edit?', + type: 'image', + default: msg => msg.author.displayAvatarURL({ format: 'png', size: 2048 }) } ] }); } - async run(msg, { type, top, bottom }) { + async run(msg, { top, bottom, image }) { try { - const search = await request.get(`https://memegen.link/api/search/${type}`); - if (!search.body.length) return msg.say('Could not find any results.'); - const { body } = await request.get(search.body[0].template.blank.replace(/\/_/, `/${top}/${bottom}`)); - return msg.say({ files: [{ attachment: body, name: 'meme.jpg' }] }); + const { body } = await request.get(image); + const base = await loadImage(body); + const canvas = createCanvas(base.width, base.height); + const ctx = canvas.getContext('2d'); + ctx.drawImage(base, 0, 0); + const fontSize = Math.round(base.height / 10); + ctx.font = `${fontSize}px Impact`; + ctx.fillStyle = 'white'; + ctx.textAlign = 'center'; + const topLines = await wrapText(ctx, top, base.width - 10); + for (let i = 0; i < topLines.length; i++) { + const textHeight = (i * fontSize) + (i * 10); + ctx.fillText(topLines[i], base.width / 2, textHeight); + } + const bottomLines = await wrapText(ctx, bottom, base.width - 10); + for (let i = 0; i < bottomLines.length; i++) { + const textHeight = base.height - (i * fontSize) - (i * 10); + ctx.fillText(bottomLines[i], base.width / 2, textHeight); + } + const attachment = canvas.toBuffer(); + if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.'); + return msg.say({ files: [{ attachment, name: 'meme-gen.png' }] }); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } diff --git a/commands/util/info.js b/commands/util/info.js index 0acc2b7a..b0dc7a9a 100644 --- a/commands/util/info.js +++ b/commands/util/info.js @@ -23,7 +23,7 @@ module.exports = class InfoCommand extends Command { run(msg) { const embed = new MessageEmbed() .setColor(0x00AE86) - .setFooter('©2017-2018 dragonfire535#8081') + .setFooter('©2017-2019 dragonfire535#8081') .addField('❯ Servers', formatNumber(this.client.guilds.size), true) .addField('❯ Shards', formatNumber(this.client.options.shardCount), true) .addField('❯ Commands', formatNumber(this.client.registry.commands.size), true) diff --git a/package.json b/package.json index 54bdd57a..343c2497 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "101.0.0", + "version": "101.0.1", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {