mirror of
https://github.com/JonnyBro/JaBa.git
synced 2024-12-29 14:53:03 +05:00
feat(command): added checking if command editing
This commit is contained in:
parent
ba5bf02086
commit
3d91b7fa9b
2 changed files with 21 additions and 7 deletions
|
@ -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}`));
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
),
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
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;
|
||||||
|
}
|
Loading…
Reference in a new issue