Files
xiao/commands/edit-image/ifunny.js
T
2024-09-13 15:14:27 -04:00

51 lines
1.6 KiB
JavaScript

const Command = require('../../framework/Command');
const { PermissionFlagsBits } = require('discord.js');
const { createCanvas, loadImage } = require('@napi-rs/canvas');
const request = require('node-superfetch');
const path = require('path');
module.exports = class IfunnyCommand extends Command {
constructor(client) {
super(client, {
name: 'ifunny',
group: 'edit-image',
description: 'Draws an image with the iFunny logo.',
throttling: {
usages: 2,
duration: 10
},
clientPermissions: [PermissionFlagsBits.AttachFiles],
credit: [
{
name: 'iFunny',
url: 'https://ifunny.co/',
reason: 'Logo'
}
],
args: [
{
key: 'image',
type: 'image-or-avatar',
avatarSize: 512,
default: msg => msg.author.displayAvatarURL({ extension: 'png', size: 512, forceStatic: true })
}
]
});
}
async run(msg, { image }) {
const base = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'ifunny.png'));
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);
ctx.fillStyle = '#181619';
ctx.fillRect(0, canvas.height - base.height, canvas.width, base.height);
ctx.drawImage(base, canvas.width - base.width, canvas.height - base.height);
const attachment = canvas.toBuffer('image/png');
if (Buffer.byteLength(attachment) > 2.5e+7) return msg.reply('Resulting image was above 25 MB.');
return msg.say({ files: [{ attachment, name: 'ifunny.png' }] });
}
};