Files
xiao/commands/edit-image/charcoal.js
T
2020-07-04 12:06:28 -04:00

58 lines
1.5 KiB
JavaScript

const Command = require('../../structures/Command');
const gm = require('gm').subClass({ imageMagick: true });
const request = require('node-superfetch');
module.exports = class CharcoalCommand extends Command {
constructor(client) {
super(client, {
name: 'charcoal',
group: 'edit-image',
memberName: 'charcoal',
description: 'Draws an image or a user\'s avatar but with charcoal.',
throttling: {
usages: 1,
duration: 15
},
clientPermissions: ['ATTACH_FILES'],
credit: [
{
name: 'ImageMagick',
url: 'https://imagemagick.org/index.php',
reason: 'Image Manipulation'
}
],
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 { body } = await request.get(image);
const magik = gm(body);
magik.charcoal(1);
magik.setFormat('png');
const attachment = await this.toBuffer(magik);
if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.');
return msg.say({ files: [{ attachment, name: 'sketch.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
toBuffer(magik) {
return new Promise((res, rej) => {
magik.toBuffer((err, buffer) => {
if (err) return rej(err);
return res(buffer);
});
});
}
};