From 3d91b7fa9b4f0d49b9e30b7f7efabe15d25e40d6 Mon Sep 17 00:00:00 2001 From: Slincnik Date: Sat, 7 Dec 2024 14:52:22 +0300 Subject: [PATCH] feat(command): added checking if command editing --- .../functions/registerCommands.js | 20 ++++++++++++------- .../utils/differentcommands.js | 8 ++++++++ 2 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 handlers/command-handler/utils/differentcommands.js diff --git a/handlers/command-handler/functions/registerCommands.js b/handlers/command-handler/functions/registerCommands.js index 2f4e5740..18b32119 100644 --- a/handlers/command-handler/functions/registerCommands.js +++ b/handlers/command-handler/functions/registerCommands.js @@ -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}`)); + } + }), ); }; diff --git a/handlers/command-handler/utils/differentcommands.js b/handlers/command-handler/utils/differentcommands.js new file mode 100644 index 00000000..bf32572c --- /dev/null +++ b/handlers/command-handler/utils/differentcommands.js @@ -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; +}