feat(command): added checking if command editing

This commit is contained in:
Slincnik 2024-12-07 14:52:22 +03:00
parent ba5bf02086
commit 3d91b7fa9b
No known key found for this signature in database
2 changed files with 21 additions and 7 deletions

View file

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