mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Vignette Command
This commit is contained in:
@@ -21,3 +21,4 @@ tf_models/
|
||||
*.traineddata
|
||||
|
||||
# In-Development Commands
|
||||
commands/edit-image/vintage.js
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
const Command = require('../../structures/Command');
|
||||
const { createCanvas, loadImage } = require('canvas');
|
||||
const request = require('node-superfetch');
|
||||
const { vignette } = require('../../util/Canvas');
|
||||
|
||||
module.exports = class VignetteCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'vignette',
|
||||
group: 'edit-image',
|
||||
memberName: 'vignette',
|
||||
description: 'Draws an image or a user\'s avatar with a vignette.',
|
||||
throttling: {
|
||||
usages: 2,
|
||||
duration: 10
|
||||
},
|
||||
clientPermissions: ['ATTACH_FILES'],
|
||||
args: [
|
||||
{
|
||||
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, { image }) {
|
||||
try {
|
||||
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);
|
||||
vignette(ctx, data.width, data.height);
|
||||
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: 'vignette.png' }] });
|
||||
} catch (err) {
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
}
|
||||
};
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiao",
|
||||
"version": "134.3.2",
|
||||
"version": "134.4.0",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Xiao.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -133,6 +133,17 @@ module.exports = class CanvasUtil {
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static vignette(ctx, width, height) {
|
||||
const outerRadius = width * 0.5;
|
||||
const innerRadius = width * 0.2;
|
||||
const grd = ctx.createRadialGradient(width / 2, height / 2, innerRadius, width / 2, height / 2, outerRadius);
|
||||
grd.addColorStop(0, 'rgba(0, 0, 0, 0.4)');
|
||||
grd.addColorStop(1, 'rgba(0, 0, 0, 0.9)');
|
||||
ctx.fillStyle = grd;
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static hasAlpha(image) {
|
||||
const canvas = createCanvas(image.width, image.height);
|
||||
const ctx = canvas.getContext('2d');
|
||||
|
||||
Reference in New Issue
Block a user