Make meme generator use pictures users send instead of a database

This commit is contained in:
Daniel Odendahl Jr
2019-02-08 16:29:23 +00:00
parent 2867fcf35d
commit 60b6cbea71
4 changed files with 43 additions and 19 deletions
Binary file not shown.
+41 -17
View File
@@ -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!`);
}
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "101.0.0",
"version": "101.0.1",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {