mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Crop to content command
This commit is contained in:
@@ -71,7 +71,7 @@ Just read `LICENSE.md`. Give credit if you use any part of this monster, thanks.
|
||||
22. Start Xiao up!
|
||||
|
||||
## Commands
|
||||
Total: 510
|
||||
Total: 511
|
||||
|
||||
### Utility:
|
||||
|
||||
@@ -392,6 +392,7 @@ Total: 510
|
||||
* **contrast:** Draws an image or a user's avatar but with contrast.
|
||||
* **convert-image:** Converts an image from one format to another.
|
||||
* **create-qr-code:** Converts text to a QR Code.
|
||||
* **crop-to-content:** Crops an image to its content.
|
||||
* **desaturate:** Draws an image or a user's avatar but desaturated.
|
||||
* **dexter:** Draws an image or a user's avatar over the screen of Dexter from Pokémon.
|
||||
* **distort:** Draws an image or a user's avatar but distorted.
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
const Command = require('../../framework/Command');
|
||||
const { PermissionFlagsBits } = require('discord.js');
|
||||
const { createCanvas, loadImage } = require('canvas');
|
||||
const request = require('node-superfetch');
|
||||
const { cropToContent } = require('../../util/Canvas');
|
||||
|
||||
module.exports = class CropToContentCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'crop-to-content',
|
||||
group: 'edit-image',
|
||||
memberName: 'crop-to-content',
|
||||
description: 'Crops an image to its content.',
|
||||
throttling: {
|
||||
usages: 2,
|
||||
duration: 10
|
||||
},
|
||||
clientPermissions: [PermissionFlagsBits.AttachFiles],
|
||||
args: [
|
||||
{
|
||||
key: 'image',
|
||||
type: 'image'
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
async run(msg, { image }) {
|
||||
const { body } = await request.get(image);
|
||||
const data = await loadImage(body);
|
||||
const canvas = createCanvas(data.width, data.height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
ctx.drawImage(data, 0, 0);
|
||||
cropToContent(ctx, canvas, data.width, data.height);
|
||||
if (canvas.width === data.width && canvas.height === data.height) {
|
||||
return msg.say('Looks like this image is already cropped to its content.');
|
||||
}
|
||||
const attachment = canvas.toBuffer();
|
||||
if (Buffer.byteLength(attachment) > 8e+6) return msg.reply('Resulting image was above 8 MB.');
|
||||
return msg.say({ files: [{ attachment, name: 'crop-to-content.png' }] });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user