mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Shake Command
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
const Command = require('../../structures/Command');
|
||||
const { createCanvas, loadImage } = require('canvas');
|
||||
const GIFEncoder = require('gifencoder');
|
||||
const request = require('node-superfetch');
|
||||
const { streamToArray } = require('../../util/Util');
|
||||
const { centerImage } = require('../../util/Canvas');
|
||||
|
||||
module.exports = class ShakeCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'shake',
|
||||
aliases: ['shook'],
|
||||
group: 'edit-image',
|
||||
memberName: 'shake',
|
||||
description: 'Draws an image or a user\'s avatar shaking.',
|
||||
throttling: {
|
||||
usages: 1,
|
||||
duration: 30
|
||||
},
|
||||
clientPermissions: ['ATTACH_FILES'],
|
||||
args: [
|
||||
{
|
||||
key: 'amount',
|
||||
prompt: 'How much do you want to shake the image? Give a number, like 1.',
|
||||
type: 'integer'
|
||||
},
|
||||
{
|
||||
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, { amount, image }) {
|
||||
try {
|
||||
const { body } = await request.get(image);
|
||||
const base = await loadImage(body);
|
||||
const ratio = base.width / base.height;
|
||||
const height = 512 / ratio;
|
||||
const encoder = new GIFEncoder(512, height);
|
||||
const canvas = createCanvas(512, height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
const stream = encoder.createReadStream();
|
||||
encoder.start();
|
||||
encoder.setRepeat(0);
|
||||
encoder.setDelay(20);
|
||||
encoder.setQuality(200);
|
||||
const frames = this.generateFrames(amount);
|
||||
for (const { x, y } of frames) {
|
||||
ctx.drawImage(base, x, y, 512, height);
|
||||
encoder.addFrame(ctx);
|
||||
}
|
||||
encoder.finish();
|
||||
const buffer = await streamToArray(stream);
|
||||
return msg.say({ files: [{ attachment: Buffer.concat(buffer), name: 'shake.gif' }] });
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
}
|
||||
|
||||
generateFrames(amount) {
|
||||
amount = amount * 5;
|
||||
return [
|
||||
{ x: -amount, y: 0 },
|
||||
{ x: 0, y: -amount },
|
||||
{ x: 0, y: 0 },
|
||||
{ x: amount, y: 0 },
|
||||
{ x: 0, y: amount },
|
||||
{ x: 0, y: 0 }
|
||||
];
|
||||
}
|
||||
};
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiao",
|
||||
"version": "131.8.0",
|
||||
"version": "131.9.0",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Xiao.js",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user