Files
xiao/commands/edit-image/yu-gi-oh-token.js
T
Dragon Fire 36d4cd7f89 Fix
2020-06-14 23:55:15 -04:00

56 lines
1.7 KiB
JavaScript

const Command = require('../../structures/Command');
const { createCanvas, loadImage } = require('canvas');
const request = require('node-superfetch');
const path = require('path');
module.exports = class YuGiOhTokenCommand extends Command {
constructor(client) {
super(client, {
name: 'yu-gi-oh-token',
aliases: ['ygo-token'],
group: 'edit-image',
memberName: 'yu-gi-oh-token',
description: 'Draws an image or a user\'s avatar over a blank Yu-Gi-Oh! Token card.',
throttling: {
usages: 1,
duration: 10
},
clientPermissions: ['ATTACH_FILES'],
credit: [
{
name: 'Konami',
url: 'https://www.konami.com/en/',
reason: 'Image, Original "Yu-Gi-Oh!" Game',
reasonURL: 'https://www.yugioh-card.com/en/'
}
],
args: [
{
key: 'image',
prompt: 'What image would you like to edit?',
type: 'image',
default: msg => msg.author.displayAvatarURL({ format: 'png', size: 512 })
}
]
});
}
async run(msg, { image }) {
try {
const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'yu-gi-oh-token.png'));
const { body } = await request.get(image);
const data = await loadImage(body);
const canvas = createCanvas(base.width, base.height);
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, base.width, base.height);
const height = 294 / data.width;
ctx.drawImage(data, 45, 102, 294, data.height * height);
ctx.drawImage(base, 0, 0);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'yu-gi-oh-token.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};