UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

1 lines 4.74 kB
{"version":3,"file":"CommandStore.mjs","names":[],"sources":["../../../../src/lib/structures/CommandStore.ts"],"sourcesContent":["import { AliasStore } from '@sapphire/pieces';\nimport type { Args } from '../parsers/Args';\nimport { RegisterBehavior } from '../types/Enums';\nimport {\n\tallGuildIdsToFetchCommandsFor,\n\tgetDefaultBehaviorWhenNotIdentical,\n\thandleBulkOverwrite,\n\tregistries\n} from '../utils/application-commands/ApplicationCommandRegistries';\nimport { getNeededRegistryParameters } from '../utils/application-commands/getNeededParameters';\nimport { emitPerRegistryError } from '../utils/application-commands/registriesErrors';\nimport { Command } from './Command';\n\n/**\n * Stores all Command pieces\n * @since 1.0.0\n */\nexport class CommandStore extends AliasStore<Command, 'commands'> {\n\tpublic constructor() {\n\t\tsuper(Command, { name: 'commands' });\n\t}\n\n\t/**\n\t * Get all the command categories.\n\t */\n\tpublic get categories(): string[] {\n\t\tconst categories = new Set(this.map((command) => command.category));\n\t\tcategories.delete(null);\n\t\treturn [...categories] as string[];\n\t}\n\n\tpublic override unload(name: string | Command): Promise<Command<Args, Command.Options>> {\n\t\tconst piece = this.resolve(name);\n\n\t\t// Remove the aliases from the store\n\t\tfor (const nameOrId of piece.applicationCommandRegistry.chatInputCommands) {\n\t\t\tconst aliasedPiece = this.aliases.get(nameOrId);\n\t\t\tif (aliasedPiece === piece) {\n\t\t\t\tthis.aliases.delete(nameOrId);\n\t\t\t}\n\t\t}\n\n\t\tfor (const nameOrId of piece.applicationCommandRegistry.contextMenuCommands) {\n\t\t\tconst aliasedPiece = this.aliases.get(nameOrId);\n\t\t\tif (aliasedPiece === piece) {\n\t\t\t\tthis.aliases.delete(nameOrId);\n\t\t\t}\n\t\t}\n\n\t\t// Remove the registry from the application command registries\n\t\tregistries.delete(piece.name);\n\n\t\treturn super.unload(name);\n\t}\n\n\tpublic override async loadAll() {\n\t\tawait super.loadAll();\n\n\t\t// If we don't have an application, that means this was called on login...\n\t\tif (!this.container.client.application) return;\n\n\t\t// super.loadAll() currently deletes all application command registries while unloading old pieces,\n\t\t// re-register application commands to ensure allGuildIdsToFetchCommandsFor has new guild ids for getNeededRegistryParameters\n\t\tfor (const command of this.values()) {\n\t\t\tif (command.registerApplicationCommands) {\n\t\t\t\ttry {\n\t\t\t\t\tawait command.registerApplicationCommands(command.applicationCommandRegistry);\n\t\t\t\t} catch (error) {\n\t\t\t\t\temitPerRegistryError(error, command);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// If the default behavior is set to bulk overwrite, handle it as such and return.\n\t\tif (getDefaultBehaviorWhenNotIdentical() === RegisterBehavior.BulkOverwrite) {\n\t\t\tawait handleBulkOverwrite(this, this.container.client.application.commands);\n\t\t\treturn;\n\t\t}\n\n\t\tconst { applicationCommands, globalCommands, guildCommands } = await getNeededRegistryParameters(allGuildIdsToFetchCommandsFor);\n\n\t\tfor (const command of this.values()) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/dot-notation\n\t\t\tawait command.applicationCommandRegistry['runAPICalls'](applicationCommands, globalCommands, guildCommands);\n\n\t\t\t// Reinitialize the aliases\n\t\t\tfor (const nameOrId of command.applicationCommandRegistry.chatInputCommands) {\n\t\t\t\tthis.aliases.set(nameOrId, command);\n\t\t\t}\n\n\t\t\tfor (const nameOrId of command.applicationCommandRegistry.contextMenuCommands) {\n\t\t\t\tthis.aliases.set(nameOrId, command);\n\t\t\t}\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;;;AAiBA,IAAa,eAAb,cAAkC,WAAgC;CACjE,AAAO,cAAc;AACpB,QAAM,SAAS,EAAE,MAAM,YAAY,CAAC;;;;;CAMrC,IAAW,aAAuB;EACjC,MAAM,aAAa,IAAI,IAAI,KAAK,KAAK,YAAY,QAAQ,SAAS,CAAC;AACnE,aAAW,OAAO,KAAK;AACvB,SAAO,CAAC,GAAG,WAAW;;CAGvB,AAAgB,OAAO,MAAiE;EACvF,MAAM,QAAQ,KA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