replace canvas with prebuild one

This commit is contained in:
Jonny_Bro (Nikita) 2024-06-15 17:46:43 +05:00
parent 87dd38a4b1
commit 7a280b204c
Signed by: jonny_bro
GPG key ID: 3F1ECC04147E9BD8
8 changed files with 1652 additions and 1279 deletions

View file

@ -1,5 +1,5 @@
const mongoose = require("mongoose"), const mongoose = require("mongoose"),
Canvas = require("canvas"); Canvas = require("@napi-rs/canvas");
const genToken = () => { const genToken = () => {
let token = ""; let token = "";
@ -107,7 +107,7 @@ userSchema.method("getAchievements", async function () {
dim += 200; dim += 200;
} }
return canvas.toBuffer(); return (await canvas.encode("png"));
}); });
module.exports = mongoose.model("User", userSchema); module.exports = mongoose.model("User", userSchema);

View file

@ -83,13 +83,15 @@ class Goodbye extends BaseCommand {
* @param {import("discord.js").ChatInputCommandInteraction} interaction * @param {import("discord.js").ChatInputCommandInteraction} interaction
*/ */
async execute(client, interaction) { async execute(client, interaction) {
await interaction.deferReply({ ephemeral: true });
const guildData = interaction.data.guild, const guildData = interaction.data.guild,
command = interaction.options.getSubcommand(); command = interaction.options.getSubcommand();
if (command === "test") { if (command === "test") {
client.emit("guildMemberRemove", interaction.member); client.emit("guildMemberRemove", client, interaction.member);
interaction.success("administration/goodbye:TEST_SUCCESS", null, { ephemeral: true }); interaction.success("administration/goodbye:TEST_SUCCESS", null, { edit: true });
} else { } else {
const state = interaction.options.getBoolean("state"); const state = interaction.options.getBoolean("state");
@ -103,7 +105,7 @@ class Goodbye extends BaseCommand {
await guildData.save(); await guildData.save();
interaction.success("administration/goodbye:DISABLED", null, { ephemeral: true }); interaction.success("administration/goodbye:DISABLED", null, { edit: true });
} else { } else {
const channel = interaction.options.getChannel("channel") || interaction.channel; const channel = interaction.options.getChannel("channel") || interaction.channel;
const message = interaction.options.getString("message") || interaction.translate("administration/goodbye:DEFAULT_MESSAGE"); const message = interaction.options.getString("message") || interaction.translate("administration/goodbye:DEFAULT_MESSAGE");

View file

@ -83,13 +83,15 @@ class Welcome extends BaseCommand {
* @param {import("discord.js").ChatInputCommandInteraction} interaction * @param {import("discord.js").ChatInputCommandInteraction} interaction
*/ */
async execute(client, interaction) { async execute(client, interaction) {
await interaction.deferReply({ ephemeral: true });
const guildData = interaction.data.guild, const guildData = interaction.data.guild,
command = interaction.options.getSubcommand(); command = interaction.options.getSubcommand();
if (command === "test") { if (command === "test") {
client.emit("guildMemberAdd", interaction.member); client.emit("guildMemberAdd", client, interaction.member);
interaction.success("administration/goodbye:TEST_SUCCESS", null, { ephemeral: true }); interaction.success("administration/goodbye:TEST_SUCCESS", null, { edit: true });
} else { } else {
const state = interaction.options.getBoolean("state"); const state = interaction.options.getBoolean("state");
@ -103,7 +105,7 @@ class Welcome extends BaseCommand {
await guildData.save(); await guildData.save();
interaction.success("administration/welcome:DISABLED", null, { ephemeral: true }); interaction.success("administration/welcome:DISABLED", null);
} else { } else {
const channel = interaction.options.getChannel("channel") || interaction.channel; const channel = interaction.options.getChannel("channel") || interaction.channel;
const message = interaction.options.getString("message") || interaction.translate("administration/welcome:DEFAULT_MESSAGE"); const message = interaction.options.getString("message") || interaction.translate("administration/welcome:DEFAULT_MESSAGE");
@ -120,7 +122,7 @@ class Welcome extends BaseCommand {
interaction.success("administration/welcome:ENABLED", { interaction.success("administration/welcome:ENABLED", {
channel: `${channel.toString()}`, channel: `${channel.toString()}`,
}, { ephemeral: true }); }, { edit: true });
} }
} }
} }

View file

@ -1,18 +1,7 @@
const Canvas = require("canvas"), const Canvas = require("@napi-rs/canvas"),
BaseEvent = require("../../base/BaseEvent"), BaseEvent = require("../../base/BaseEvent"),
{ AttachmentBuilder } = require("discord.js"), { AttachmentBuilder } = require("discord.js"),
{ resolve } = require("path"); { applyText } = require("../../helpers/functions");
Canvas.registerFont(resolve("./assets/fonts/RubikMonoOne-Regular.ttf"), { family: "RubikMonoOne" });
Canvas.registerFont(resolve("./assets/fonts/KeepCalm-Medium.ttf"), { family: "KeepCalm" });
const applyText = (canvas, text, defaultFontSize, width, font) => {
const ctx = canvas.getContext("2d");
do ctx.font = `${(defaultFontSize -= 1)}px ${font}`;
while (ctx.measureText(text).width > width);
return ctx.font;
};
class GuildMemberAdd extends BaseEvent { class GuildMemberAdd extends BaseEvent {
constructor() { constructor() {
@ -132,7 +121,7 @@ class GuildMemberAdd extends BaseEvent {
); );
ctx.drawImage(avatar, 45, 90, 270, 270); ctx.drawImage(avatar, 45, 90, 270, 270);
const attachment = new AttachmentBuilder(canvas.toBuffer(), { name: "welcome-image.png" }); const attachment = new AttachmentBuilder((await canvas.encode("png")), { name: "welcome.png" });
channel.send({ channel.send({
content: message, content: message,

View file

@ -1,18 +1,7 @@
const Canvas = require("canvas"), const Canvas = require("@napi-rs/canvas"),
BaseEvent = require("../../base/BaseEvent"), BaseEvent = require("../../base/BaseEvent"),
{ AttachmentBuilder } = require("discord.js"), { AttachmentBuilder } = require("discord.js"),
{ resolve } = require("path"); { applyText } = require("../../helpers/functions");
Canvas.registerFont(resolve("./assets/fonts/RubikMonoOne-Regular.ttf"), { family: "RubikMonoOne" });
Canvas.registerFont(resolve("./assets/fonts/KeepCalm-Medium.ttf"), { family: "KeepCalm" });
const applyText = (canvas, text, defaultFontSize, width, font) => {
const ctx = canvas.getContext("2d");
do ctx.font = `${(defaultFontSize -= 1)}px ${font}`;
while (ctx.measureText(text).width > width);
return ctx.font;
};
class GuildMemberRemove extends BaseEvent { class GuildMemberRemove extends BaseEvent {
constructor() { constructor() {
@ -135,7 +124,7 @@ class GuildMemberRemove extends BaseEvent {
); );
ctx.drawImage(avatar, 45, 90, 270, 270); ctx.drawImage(avatar, 45, 90, 270, 270);
const attachment = new AttachmentBuilder(canvas.toBuffer(), { name: "goodbye-image.png" }); const attachment = new AttachmentBuilder((await canvas.encode("png")), { name: "goodbye-image.png" });
channel.send({ channel.send({
content: message, content: message,

View file

@ -1,4 +1,9 @@
const moment = require("moment"); const moment = require("moment"),
resolve = require("path"),
Canvas = require("@napi-rs/canvas");
Canvas.registerFont(resolve("./assets/fonts/RubikMonoOne-Regular.ttf"), { family: "RubikMonoOne" });
Canvas.registerFont(resolve("./assets/fonts/KeepCalm-Medium.ttf"), { family: "KeepCalm" });
module.exports = { module.exports = {
/** /**
@ -125,4 +130,23 @@ module.exports = {
return five; return five;
}, },
/**
* Function to apply text on a canvas with dynamic font size based on the width constraint.
*
* @param {import("@napi-rs/canvas").Canvas} canvas - The canvas object where the text will be applied.
* @param {string} text - The string of text that needs to be applied on the canvas.
* @param {number} defaultFontSize - The initial font size for the text. It is expected to decrease with each iteration.
* @param {number} width - The maximum width that the text can occupy before it has to shrink down.
* @param {string} font - The name of the font used for drawing the text on the canvas.
*
* @return {string} - The final calculated font size in a format '<size>px <family>'.
*/
applyText(canvas, text, defaultFontSize, width, font) {
const ctx = canvas.getContext("2d");
do ctx.font = `${(defaultFontSize -= 1)}px ${font}`;
while (ctx.measureText(text).width > width);
return ctx.font;
},
}; };

View file

@ -12,7 +12,7 @@
"@discordjs/opus": "^0.9.0", "@discordjs/opus": "^0.9.0",
"@discordjs/rest": "^2.2.0", "@discordjs/rest": "^2.2.0",
"@discordjs/voice": "^0.16.1", "@discordjs/voice": "^0.16.1",
"canvas": "^2.11.2", "@napi-rs/canvas": "^0.1.53",
"chalk": "^4.1.2", "chalk": "^4.1.2",
"cron": "^2.4.4", "cron": "^2.4.4",
"discord-api-types": "^0.37.71", "discord-api-types": "^0.37.71",

File diff suppressed because it is too large Load diff