From 4bed1f7176e769c821459763e2ecfad4f0c311b1 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 7 Mar 2021 16:18:14 -0500 Subject: [PATCH] Shake Command --- commands/edit-image/shake.js | 75 ++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 commands/edit-image/shake.js diff --git a/commands/edit-image/shake.js b/commands/edit-image/shake.js new file mode 100644 index 00000000..591d25d4 --- /dev/null +++ b/commands/edit-image/shake.js @@ -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 } + ]; + } +}; diff --git a/package.json b/package.json index a82faa6a..0bf6e544 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "131.8.0", + "version": "131.9.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {