Files
xiao/commands/edit-image/bob-ross.js
T
2021-01-30 12:02:37 -05:00

62 lines
1.8 KiB
JavaScript

const Command = require('../../structures/Command');
const { createCanvas, loadImage } = require('canvas');
const request = require('node-superfetch');
const path = require('path');
const { centerImagePart } = require('../../util/Canvas');
module.exports = class BobRossCommand extends Command {
constructor(client) {
super(client, {
name: 'bob-ross',
aliases: ['ross'],
group: 'edit-image',
memberName: 'bob-ross',
description: 'Draws an image or a user\'s avatar over Bob Ross\' canvas.',
throttling: {
usages: 1,
duration: 10
},
clientPermissions: ['ATTACH_FILES'],
credit: [
{
name: 'Know Your Meme',
url: 'https://knowyourmeme.com/',
reason: 'Image',
reasonURL: 'https://knowyourmeme.com/photos/1160348'
},
{
name: 'Bob Ross',
url: 'https://www.bobross.com/',
reason: 'Himself'
}
],
args: [
{
key: 'image',
prompt: 'What image would you like to edit?',
type: 'image-or-avatar',
default: msg => msg.author.displayAvatarURL({ format: 'png', size: 512 })
}
]
});
}
async run(msg, { image }) {
try {
const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'bob-ross.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 { x, y, width, height } = centerImagePart(data, 440, 440, 15, 20);
ctx.drawImage(data, x, y, width, height);
ctx.drawImage(base, 0, 0);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'bob-ross.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};