mirror of
https://github.com/JonnyBro/JaBa.git
synced 2024-12-29 06:43:02 +05:00
Compare commits
3 commits
cbf0879112
...
dc7bef73ee
Author | SHA1 | Date | |
---|---|---|---|
|
dc7bef73ee | ||
|
3d91b7fa9b | ||
ba5bf02086 |
7 changed files with 47 additions and 44 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}`));
|
||||
}
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
|
8
handlers/command-handler/utils/differentcommands.js
Normal file
8
handlers/command-handler/utils/differentcommands.js
Normal 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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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!");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue