mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-10 19:04:42 +02:00
Make meme generator use pictures users send instead of a database
This commit is contained in:
Binary file not shown.
@@ -1,5 +1,9 @@
|
|||||||
const Command = require('../../structures/Command');
|
const Command = require('../../structures/Command');
|
||||||
|
const { createCanvas, loadImage, registerFont } = require('canvas');
|
||||||
const request = require('node-superfetch');
|
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 {
|
module.exports = class MemeGenCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
@@ -9,38 +13,58 @@ module.exports = class MemeGenCommand extends Command {
|
|||||||
group: 'image-edit',
|
group: 'image-edit',
|
||||||
memberName: 'meme-gen',
|
memberName: 'meme-gen',
|
||||||
description: 'Sends a meme with the text and background of your choice.',
|
description: 'Sends a meme with the text and background of your choice.',
|
||||||
|
throttling: {
|
||||||
|
usages: 1,
|
||||||
|
duration: 10
|
||||||
|
},
|
||||||
clientPermissions: ['ATTACH_FILES'],
|
clientPermissions: ['ATTACH_FILES'],
|
||||||
args: [
|
args: [
|
||||||
{
|
|
||||||
key: 'type',
|
|
||||||
prompt: 'What meme type do you want to use?',
|
|
||||||
type: 'string',
|
|
||||||
parse: type => encodeURIComponent(type)
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
key: 'top',
|
key: 'top',
|
||||||
prompt: 'What should the top row of the meme to be?',
|
prompt: 'What should the something to believe in be?',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
max: 200,
|
max: 50
|
||||||
parse: top => encodeURIComponent(top)
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'bottom',
|
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',
|
type: 'string',
|
||||||
max: 200,
|
max: 50
|
||||||
parse: bottom => encodeURIComponent(bottom)
|
},
|
||||||
|
{
|
||||||
|
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 {
|
try {
|
||||||
const search = await request.get(`https://memegen.link/api/search/${type}`);
|
const { body } = await request.get(image);
|
||||||
if (!search.body.length) return msg.say('Could not find any results.');
|
const base = await loadImage(body);
|
||||||
const { body } = await request.get(search.body[0].template.blank.replace(/\/_/, `/${top}/${bottom}`));
|
const canvas = createCanvas(base.width, base.height);
|
||||||
return msg.say({ files: [{ attachment: body, name: 'meme.jpg' }] });
|
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) {
|
} catch (err) {
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ module.exports = class InfoCommand extends Command {
|
|||||||
run(msg) {
|
run(msg) {
|
||||||
const embed = new MessageEmbed()
|
const embed = new MessageEmbed()
|
||||||
.setColor(0x00AE86)
|
.setColor(0x00AE86)
|
||||||
.setFooter('©2017-2018 dragonfire535#8081')
|
.setFooter('©2017-2019 dragonfire535#8081')
|
||||||
.addField('❯ Servers', formatNumber(this.client.guilds.size), true)
|
.addField('❯ Servers', formatNumber(this.client.guilds.size), true)
|
||||||
.addField('❯ Shards', formatNumber(this.client.options.shardCount), true)
|
.addField('❯ Shards', formatNumber(this.client.options.shardCount), true)
|
||||||
.addField('❯ Commands', formatNumber(this.client.registry.commands.size), true)
|
.addField('❯ Commands', formatNumber(this.client.registry.commands.size), true)
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiao",
|
"name": "xiao",
|
||||||
"version": "101.0.0",
|
"version": "101.0.1",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user