Compare commits

...

3 commits

Author SHA1 Message Date
Slincnik
dc7bef73ee
fix(event): fixed not loading events 2024-12-07 14:53:11 +03:00
Slincnik
3d91b7fa9b
feat(command): added checking if command editing 2024-12-07 14:52:22 +03:00
ba5bf02086 remove unused folder and function 2024-12-07 14:47:05 +05:00
7 changed files with 47 additions and 44 deletions

View file

@ -12,6 +12,8 @@ export class ExtendedClient extends Client {
}
async init() {
this.login(config.token).then(async () => await Promise.all([initCommands(), initEvents()]).catch(console.error));
return this.login(config.token)
.then(async () => await Promise.all([initCommands(), initEvents()]))
.catch(console.error);
}
}

View file

View file

@ -1,3 +1,5 @@
import differentCommands from "../utils/differentcommands.js";
export default async function registerCommands(props) {
const globalCommands = props.commands.filter(cmd => !cmd.options?.devOnly);
await registerGlobalCommands(props.client, globalCommands);
@ -7,13 +9,17 @@ const registerGlobalCommands = async (client, commands) => {
const appCommandsManager = client.application.commands;
await appCommandsManager.fetch();
const newCommands = commands.filter(cmd => !appCommandsManager.cache.some(existingCmd => existingCmd.name === cmd.data.name));
await Promise.all(
newCommands.map(data =>
appCommandsManager.create(data).catch(() => {
throw new Error(`Failed to register command: ${data.name}`);
}),
),
commands.map(async ({ data }) => {
const targetCommand = appCommandsManager.cache.find(cmd => cmd.name === data.name);
if (targetCommand && differentCommands(targetCommand, data)) {
await targetCommand.edit(data).catch(() => console.log(`Failed to update command: ${data.name} globally`));
console.log(`Edited command globally: ${data.name}`);
} else if (!targetCommand) {
await appCommandsManager.create(data).catch(() => console.log(`Failed to register command: ${data.name}`));
}
}),
);
};

View file

@ -0,0 +1,8 @@
export default function differentCommands(appCommand, localCommand) {
const appOptions = appCommand.options || [];
const localOptions = localCommand.options || [];
const appDescription = appCommand.description || "";
const localDescription = localCommand.description || "";
return localDescription !== appDescription || localOptions.length !== appOptions.length;
}

View file

@ -11,31 +11,32 @@ export const init = async () => {
};
const buildEvents = async () => {
const eventFilePaths = (await getFilePaths("./newEvents", true)).filter(path => path.endsWith(".js"));
try {
const eventFilePaths = (await getFilePaths("./newEvents", true)).filter(path => path.endsWith(".js"));
for (const eventFilePath of eventFilePaths) {
const { data, run } = await import(toFileURL(eventFilePath));
for (const eventFilePath of eventFilePaths) {
const { data, run } = await import(toFileURL(eventFilePath));
if (!data || !data.name) {
console.warn(`Event ${eventFilePath} does not have a data object or name`);
continue;
if (!data || !data.name) {
console.warn(`Event ${eventFilePath} does not have a data object or name`);
continue;
}
if (typeof run !== "function") {
console.warn(`Event ${eventFilePath} does not have a run function or it is not a function`);
continue;
}
events.push({ data, run });
}
if (typeof run !== "function") {
console.warn(`Event ${eventFilePath} does not have a run function or it is not a function`);
continue;
}
events.push({ data, run });
} catch (error) {
console.error("Error build events: ", error);
}
};
const registerEvents = () => {
for (const { data, run } of events) {
if (data.once) {
client.once(data.name, run);
} else {
client.on(data.name, run);
}
if (data.once) client.once(data.name, run);
else client.on(data.name, run);
}
};

View file

@ -117,21 +117,3 @@ export function getNoun(number, one, two, five) {
if (n >= 2 && n <= 4) return two;
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>'.
*/
export function 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

@ -3,6 +3,10 @@ export const data = {
once: true,
};
/**
*
* @param {import("../base/Client.JaBaClient")} client
*/
export async function run(client) {
console.log(client.user.tag + " is online!");
}