Vignette Command

This commit is contained in:
Dragon Fire
2021-03-28 13:13:35 -04:00
parent 165f0afb32
commit 818cfbcfe4
4 changed files with 57 additions and 1 deletions
+1
View File
@@ -21,3 +21,4 @@ tf_models/
*.traineddata
# In-Development Commands
commands/edit-image/vintage.js
+44
View File
@@ -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
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "134.3.2",
"version": "134.4.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+11
View File
@@ -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');