From 78fca15044aa296b9338757efdd63d4d16032229 Mon Sep 17 00:00:00 2001 From: Slincnik Date: Sat, 11 Jan 2025 14:54:25 +0300 Subject: [PATCH] feat(handlers): added support load dev commands in dev servers --- config.sample.json | 6 +- .../functions/registerCommands.js | 64 ++++++++++++++++++- src/handlers/command-handler/index.js | 7 -- 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/config.sample.json b/config.sample.json index 40e872aa..dda773d4 100644 --- a/config.sample.json +++ b/config.sample.json @@ -7,11 +7,7 @@ "clientSecret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX" }, "youtubeCookie": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX", - "support": { - "id": "123456789098765432", - "logs": "123456789098765432", - "invite": "https://discord.gg/discord" - }, + "devGuildsIds": [], "embed": { "color": "#00FF00", "footer": { diff --git a/src/handlers/command-handler/functions/registerCommands.js b/src/handlers/command-handler/functions/registerCommands.js index c7fe5ef9..e9d6955c 100644 --- a/src/handlers/command-handler/functions/registerCommands.js +++ b/src/handlers/command-handler/functions/registerCommands.js @@ -2,10 +2,24 @@ import logger from "../../../helpers/logger.js"; import differentCommands from "../utils/differentcommands.js"; export default async function registerCommands(props) { - const globalCommands = props.commands.filter(cmd => !cmd.options?.devOnly); - props.client.once("ready", () => registerGlobalCommands(props.client, globalCommands)); + props.client.once("ready", () => handleRegistration(props.client, props.commands)); } +const handleRegistration = async (client, commands) => { + const devOnlyCommands = commands.filter(cmd => cmd.options?.devOnly); + const globalCommands = commands.filter(cmd => !cmd.options?.devOnly); + + const devGuildsIds = client.configService.get("devGuildsIds"); + + await registerGlobalCommands(client, globalCommands); + await registerDevCommands(client, devOnlyCommands, devGuildsIds); +}; + +/** + * + * @param {import("../../../structures/client.js").ExtendedClient} client + * @param {*} commands + */ const registerGlobalCommands = async (client, commands) => { const appCommandsManager = client.application.commands; await appCommandsManager.fetch(); @@ -26,3 +40,49 @@ const registerGlobalCommands = async (client, commands) => { logger.log("Registered global commands"); }; + +/** + * + * @param {import("../../../structures/client.js").ExtendedClient} client + * @param {*} commands + */ +const registerDevCommands = async (client, commands, guildsIds) => { + const devGuilds = []; + + for (const guildId of guildsIds) { + const guild = client.guilds.cache.get(guildId) || (await client.guilds.fetch(guildId)); + + if (!guild) { + logger.error(`Could not register dev commands, guild ${guildId} not found`); + continue; + } + + devGuilds.push(guild); + } + + const guildCommandsManagers = []; + + for (const guild of devGuilds) { + const guildCommandsManager = guild.commands; + await guildCommandsManager.fetch(); + + guildCommandsManagers.push(guildCommandsManager); + } + + await Promise.all( + commands.map(async ({ data }) => { + guildCommandsManagers.map(async guildCommands => { + const targetCommand = guildCommands.cache.find(cmd => cmd.name === data.name); + if (targetCommand && differentCommands(targetCommand, data)) { + await targetCommand.edit(data).catch(() => logger.error(`Failed to update command: ${data.name} in ${guildCommands.guild.name} server`)); + + logger.log(`Edited command globally: ${data.name}`); + } else if (!targetCommand) { + await guildCommands.create(data).catch(() => logger.error(`Failed to register command: ${data.name} in ${guildCommands.guild.name} server`)); + } + }); + }), + ); + + logger.log(`Registered dev commands in ${devGuilds.length} server(s)`); +}; diff --git a/src/handlers/command-handler/index.js b/src/handlers/command-handler/index.js index 7faf5b28..54078119 100644 --- a/src/handlers/command-handler/index.js +++ b/src/handlers/command-handler/index.js @@ -3,7 +3,6 @@ import logger from "../../helpers/logger.js"; import { getFilePaths } from "../../utils/index.js"; import { toFileURL } from "../../utils/resolve-file.js"; import registerCommands from "./functions/registerCommands.js"; -import { replyError } from "../../helpers/extenders.js"; export class CommandHandler { constructor(client) { @@ -56,12 +55,6 @@ export class CommandHandler { if (!targetCommand) return; - const ownerId = this.client.configService.get("owner.id"); - - if (targetCommand.data.ownerOnly && interaction.user.id !== ownerId) { - return replyError(interaction, "misc:OWNER_ONLY", null, { ephemeral: true }); - } - // Skip if autocomplete handler is not defined if (isAutocomplete && !targetCommand.autocompleteRun) return;