Files
xiao/commands/avatar-edit/hat.js
T
Daniel Odendahl Jr 4e057cf9b5 Pilgrim hat
2017-11-03 16:33:42 +00:00

61 lines
1.8 KiB
JavaScript

const { Command } = require('discord.js-commando');
const { createCanvas, loadImage } = require('canvas');
const snekfetch = require('snekfetch');
const path = require('path');
const { list } = require('../../util/Util');
const hats = ['christmas', 'anime', 'tophat', 'pilgrim'];
module.exports = class HatCommand extends Command {
constructor(client) {
super(client, {
name: 'hat',
group: 'avatar-edit',
memberName: 'hat',
description: 'Draws a hat over a user\'s avatar.',
throttling: {
usages: 1,
duration: 15
},
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'type',
prompt: `What type of hat would you like to use? Either ${list(hats, 'or')}.`,
type: 'string',
validate: type => {
if (hats.includes(type.toLowerCase())) return true;
return `Invalid type, please enter either ${list(hats, 'or')}.`;
},
parse: type => type.toLowerCase()
},
{
key: 'user',
prompt: 'Which user would you like to edit the avatar of?',
type: 'user',
default: ''
}
]
});
}
async run(msg, { type, user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 512
});
try {
const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'hat', `${type}.png`));
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const canvas = createCanvas(avatar.width, avatar.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(avatar, 0, 0);
ctx.drawImage(base, 0, 0, avatar.width, avatar.height);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: `${type}-hat.png` }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};