UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

197 lines (195 loc) • 10.2 kB
"use strict"; const require_lib_types_Enums = require('../../types/Enums.cjs'); const require_lib_types_Events = require('../../types/Events.cjs'); const require_lib_utils_application_commands_ApplicationCommandRegistry = require('./ApplicationCommandRegistry.cjs'); const require_lib_utils_application_commands_getNeededParameters = require('./getNeededParameters.cjs'); const require_lib_utils_application_commands_registriesLog = require('./registriesLog.cjs'); const require_lib_utils_application_commands_registriesErrors = require('./registriesErrors.cjs'); let __sapphire_pieces = require("@sapphire/pieces"); let __sapphire_utilities = require("@sapphire/utilities"); let discord_js = require("discord.js"); //#region src/lib/utils/application-commands/ApplicationCommandRegistries.ts let defaultBehaviorWhenNotIdentical = require_lib_types_Enums.RegisterBehavior.Overwrite; let defaultGuildIds = void 0; let bulkOVerwriteRetries = 1; const registries = /* @__PURE__ */ new Map(); const allGuildIdsToFetchCommandsFor = /* @__PURE__ */ new Set(); /** * Acquires a registry for a command by its name. * @param commandName The name of the command. * @returns The application command registry for the command */ function acquire(commandName) { const existing = registries.get(commandName); if (existing) return existing; const newRegistry = new require_lib_utils_application_commands_ApplicationCommandRegistry.ApplicationCommandRegistry(commandName); registries.set(commandName, newRegistry); return newRegistry; } /** * Sets the default behavior when registered commands aren't identical to provided data. * @param behavior The default behavior to have. Set this to `null` to reset it to the default * of `RegisterBehavior.Overwrite`. */ function setDefaultBehaviorWhenNotIdentical(behavior) { defaultBehaviorWhenNotIdentical = behavior ?? require_lib_types_Enums.RegisterBehavior.Overwrite; } function getDefaultBehaviorWhenNotIdentical() { return defaultBehaviorWhenNotIdentical; } /** * Sets the default guild ids for registering commands. This is used when a command is registered _without_ providing * `guildIds` in that command's own {@link Command.registerApplicationCommands} method. * @param guildIds The default guildIds to set. Set this to `null` to reset it to the default * of `undefined`. */ function setDefaultGuildIds(guildIds) { defaultGuildIds = guildIds ?? void 0; } function getDefaultGuildIds() { return defaultGuildIds; } /** * Sets the amount of retries for when registering commands, only applies when {@link defaultBehaviorWhenNotIdentical} * is set to {@link RegisterBehavior.BulkOverwrite}. This is used if registering the commands times out. * The default value is `1`, which means no retries are performed. * @param newAmountOfRetries The new amount of retries to set. Set this to `null` to reset it to the default */ function setBulkOverwriteRetries(newAmountOfRetries) { newAmountOfRetries ??= 1; if (newAmountOfRetries <= 0) throw new RangeError("The amount of retries must be greater than 0"); bulkOVerwriteRetries = newAmountOfRetries; } function getBulkOverwriteRetries() { return bulkOVerwriteRetries; } async function handleRegistryAPICalls() { __sapphire_pieces.container.client.emit(require_lib_types_Events.Events.ApplicationCommandRegistriesInitialising); const commandStore = __sapphire_pieces.container.stores.get("commands"); for (const command of commandStore.values()) if (command.registerApplicationCommands) try { await command.registerApplicationCommands(command.applicationCommandRegistry); } catch (error) { require_lib_utils_application_commands_registriesErrors.emitPerRegistryError(error, command); } if (getDefaultBehaviorWhenNotIdentical() === require_lib_types_Enums.RegisterBehavior.BulkOverwrite) { await handleBulkOverwrite(commandStore, __sapphire_pieces.container.client.application.commands); return; } await handleAppendOrUpdate(commandStore, await require_lib_utils_application_commands_getNeededParameters.getNeededRegistryParameters(allGuildIdsToFetchCommandsFor)); } async function handleBulkOverwrite(commandStore, applicationCommands) { const now = Date.now(); const foundGlobalCommands = []; const foundGuildCommands = {}; for (const command of commandStore.values()) { const registry = command.applicationCommandRegistry; for (const call of registry["apiCalls"]) { if (call.registerOptions.guildIds?.length) { for (const guildId of call.registerOptions.guildIds) { foundGuildCommands[guildId] ??= []; foundGuildCommands[guildId].push({ piece: command, data: call.builtData }); } continue; } foundGlobalCommands.push({ piece: command, data: call.builtData }); } } await (0, __sapphire_utilities.retry)(() => handleBulkOverwriteGlobalCommands(commandStore, applicationCommands, foundGlobalCommands), bulkOVerwriteRetries); for (const [guildId, guildCommands] of Object.entries(foundGuildCommands)) await (0, __sapphire_utilities.retry)(() => handleBulkOverwriteGuildCommands(commandStore, applicationCommands, guildId, guildCommands), bulkOVerwriteRetries); __sapphire_pieces.container.client.emit(require_lib_types_Events.Events.ApplicationCommandRegistriesRegistered, registries, Date.now() - now); } async function handleBulkOverwriteGlobalCommands(commandStore, applicationCommands, foundGlobalCommands) { try { require_lib_utils_application_commands_registriesLog.bulkOverwriteDebug(`Overwriting global application commands, now at ${foundGlobalCommands.length} commands`); const result = await applicationCommands.set(foundGlobalCommands.map((x) => x.data)); for (const [id, globalCommand] of result.entries()) { const piece = foundGlobalCommands.find((x) => x.data.name === globalCommand.name)?.piece; if (piece) { const registry = piece.applicationCommandRegistry; switch (globalCommand.type) { case discord_js.ApplicationCommandType.ChatInput: registry["handleIdAddition"](require_lib_types_Enums.InternalRegistryAPIType.ChatInput, id); break; case discord_js.ApplicationCommandType.User: case discord_js.ApplicationCommandType.Message: registry["handleIdAddition"](require_lib_types_Enums.InternalRegistryAPIType.ContextMenu, id); break; } commandStore.aliases.set(id, piece); } else require_lib_utils_application_commands_registriesLog.bulkOverwriteWarn(`Registered global command "${globalCommand.name}" (${id}) but failed to find the piece in the command store. This should not happen`); } __sapphire_pieces.container.client.emit(require_lib_types_Events.Events.ApplicationCommandRegistriesBulkOverwrite, result, null); } catch (error) { if (error instanceof Error && error.name === "AbortError") throw error; require_lib_utils_application_commands_registriesErrors.emitBulkOverwriteError(error, null); } } async function handleBulkOverwriteGuildCommands(commandStore, applicationCommands, guildId, guildCommands) { try { require_lib_utils_application_commands_registriesLog.bulkOverwriteDebug(`Overwriting guild application commands for guild ${guildId}, now at ${guildCommands.length} commands`); const result = await applicationCommands.set(guildCommands.map((x) => x.data), guildId); for (const [id, guildCommand] of result.entries()) { const piece = guildCommands.find((x) => x.data.name === guildCommand.name)?.piece; if (piece) { const registry = piece.applicationCommandRegistry; switch (guildCommand.type) { case discord_js.ApplicationCommandType.ChatInput: registry["handleIdAddition"](require_lib_types_Enums.InternalRegistryAPIType.ChatInput, id, guildId); break; case discord_js.ApplicationCommandType.User: case discord_js.ApplicationCommandType.Message: registry["handleIdAddition"](require_lib_types_Enums.InternalRegistryAPIType.ContextMenu, id, guildId); break; } commandStore.aliases.set(id, piece); } else require_lib_utils_application_commands_registriesLog.bulkOverwriteWarn(`Registered guild command "${guildCommand.name}" (${id}) but failed to find the piece in the command store. This should not happen`); } __sapphire_pieces.container.client.emit(require_lib_types_Events.Events.ApplicationCommandRegistriesBulkOverwrite, result, guildId); } catch (error) { if (error instanceof Error && error.name === "AbortError") throw error; require_lib_utils_application_commands_registriesErrors.emitBulkOverwriteError(error, guildId); } } async function handleAppendOrUpdate(commandStore, { applicationCommands, globalCommands, guildCommands }) { const now = Date.now(); for (const registry of registries.values()) { await registry["runAPICalls"](applicationCommands, globalCommands, guildCommands); const piece = registry.command; if (piece) { for (const nameOrId of piece.applicationCommandRegistry.chatInputCommands) commandStore.aliases.set(nameOrId, piece); for (const nameOrId of piece.applicationCommandRegistry.contextMenuCommands) commandStore.aliases.set(nameOrId, piece); } } __sapphire_pieces.container.client.emit(require_lib_types_Events.Events.ApplicationCommandRegistriesRegistered, registries, Date.now() - now); } //#endregion exports.acquire = acquire; exports.allGuildIdsToFetchCommandsFor = allGuildIdsToFetchCommandsFor; Object.defineProperty(exports, 'defaultBehaviorWhenNotIdentical', { enumerable: true, get: function () { return defaultBehaviorWhenNotIdentical; } }); Object.defineProperty(exports, 'defaultGuildIds', { enumerable: true, get: function () { return defaultGuildIds; } }); exports.getBulkOverwriteRetries = getBulkOverwriteRetries; exports.getDefaultBehaviorWhenNotIdentical = getDefaultBehaviorWhenNotIdentical; exports.getDefaultGuildIds = getDefaultGuildIds; exports.handleBulkOverwrite = handleBulkOverwrite; exports.handleRegistryAPICalls = handleRegistryAPICalls; exports.registries = registries; exports.setBulkOverwriteRetries = setBulkOverwriteRetries; exports.setDefaultBehaviorWhenNotIdentical = setDefaultBehaviorWhenNotIdentical; exports.setDefaultGuildIds = setDefaultGuildIds; //# sourceMappingURL=ApplicationCommandRegistries.cjs.map