mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-27 14:18:36 +02:00
GIF Fire Command
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
const Command = require('../../structures/Command');
|
||||
const { createCanvas, loadImage } = require('canvas');
|
||||
const GIFEncoder = require('gifencoder');
|
||||
const request = require('node-superfetch');
|
||||
const path = require('path');
|
||||
const { streamToArray } = require('../../util/Util');
|
||||
const { drawImageWithTint } = require('../../util/Canvas');
|
||||
const frameCount = 31;
|
||||
|
||||
module.exports = class FireCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'fire',
|
||||
aliases: ['hell', 'burn'],
|
||||
group: 'edit-avatar',
|
||||
memberName: 'fire',
|
||||
description: 'Burns a user\'s avatar.',
|
||||
throttling: {
|
||||
usages: 1,
|
||||
duration: 10
|
||||
},
|
||||
clientPermissions: ['ATTACH_FILES'],
|
||||
credit: [
|
||||
{
|
||||
name: 'LowGif',
|
||||
url: 'http://www.lowgif.com/',
|
||||
reason: 'Images',
|
||||
reasonURL: 'http://www.lowgif.com/43360ebce9150f23.html'
|
||||
}
|
||||
],
|
||||
args: [
|
||||
{
|
||||
key: 'user',
|
||||
prompt: 'Which user would you like to edit the avatar of?',
|
||||
type: 'user',
|
||||
default: msg => msg.author
|
||||
},
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
async run(msg, { user }) {
|
||||
const avatarURL = user.displayAvatarURL({ format: 'png', size: 512 });
|
||||
try {
|
||||
const { body } = await request.get(avatarURL);
|
||||
const avatar = await loadImage(body);
|
||||
const encoder = new GIFEncoder(avatar.width, avatar.height);
|
||||
const canvas = createCanvas(avatar.width, avatar.height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
const stream = encoder.createReadStream();
|
||||
encoder.start();
|
||||
encoder.setRepeat(0);
|
||||
encoder.setDelay(4);
|
||||
encoder.setQuality(200);
|
||||
const ratio = avatar.width / avatar.height;
|
||||
const height = Math.round(avatar.width / ratio);
|
||||
for (let i = 0; i < frameCount; i += 2) {
|
||||
const frameID = `frame-${i.toString.padStart(2, '0')}.gif`;
|
||||
const frame = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'fire', frameID));
|
||||
drawImageWithTint(ctx, avatar, '#fc671e', 0, 0);
|
||||
ctx.drawImage(frame, 0, avatar.height - height, avatar.width, height);
|
||||
encoder.addFrame(ctx);
|
||||
encoder.addFrame(ctx);
|
||||
}
|
||||
encoder.finish();
|
||||
const buffer = await streamToArray(stream);
|
||||
return msg.say({ files: [{ attachment: Buffer.concat(buffer), name: 'fire.gif' }] });
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user