Files
xiao/commands/avatar-edit/demotivational-poster.js
T
Daniel Odendahl Jr a33dbf1b35 canvas succ
2017-11-10 14:15:37 +00:00

70 lines
2.2 KiB
JavaScript

const { Command } = require('discord.js-commando');
const { createCanvas, loadImage, registerFont } = require('canvas');
const snekfetch = require('snekfetch');
const path = require('path');
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto.ttf'), { family: 'Noto' });
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-CJK.otf'), { family: 'Noto' });
registerFont(path.join(__dirname, '..', '..', 'assets', 'fonts', 'Noto-Emoji.ttf'), { family: 'Noto' });
module.exports = class DemotivationalPosterCommand extends Command {
constructor(client) {
super(client, {
name: 'demotivational-poster',
aliases: ['demotivational'],
group: 'avatar-edit',
memberName: 'demotivational-poster',
description: 'Draws a user\'s avatar and the text you specify as a demotivational poster.',
throttling: {
usages: 1,
duration: 15
},
clientPermissions: ['ATTACH_FILES'],
args: [
{
key: 'title',
prompt: 'What should the title of the poster be?',
type: 'string'
},
{
key: 'text',
prompt: 'What should the text of the poster be?',
type: 'string'
},
{
key: 'user',
prompt: 'Which user would you like to edit the avatar of?',
type: 'user',
default: ''
}
]
});
}
async run(msg, { title, text, user }) {
if (!user) user = msg.author;
const avatarURL = user.displayAvatarURL({
format: 'png',
size: 1024
});
try {
const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'demotivational-poster.png'));
const { body } = await snekfetch.get(avatarURL);
const avatar = await loadImage(body);
const canvas = createCanvas(base.width, base.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(avatar, 68, -57, 612, 612);
ctx.drawImage(base, 0, 0);
ctx.textAlign = 'center';
ctx.font = '60px Noto';
ctx.fillStyle = 'aquamarine';
ctx.fillText(title, 375, 503);
ctx.font = '27px Noto';
ctx.fillStyle = 'white';
ctx.fillText(text, 375, 575);
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'demotivational-poster.png' }] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};