diff --git a/packages/discord.js/src/managers/ApplicationCommandManager.js b/packages/discord.js/src/managers/ApplicationCommandManager.js index 29e770aa553b..2498eca7219a 100644 --- a/packages/discord.js/src/managers/ApplicationCommandManager.js +++ b/packages/discord.js/src/managers/ApplicationCommandManager.js @@ -81,6 +81,7 @@ class ApplicationCommandManager extends CachedManager { /** * Options used to fetch Application Commands from Discord * @typedef {BaseFetchOptions} FetchApplicationCommandOptions + * @property {Snowflake} [id] The command's id to fetch * @property {Snowflake} [guildId] The guild's id to fetch commands for, for when the guild is not cached * @property {Locale} [locale] The locale to use when fetching this command * @property {boolean} [withLocalizations] Whether to fetch all localization data @@ -88,8 +89,7 @@ class ApplicationCommandManager extends CachedManager { /** * Obtains one or multiple application commands from Discord, or the cache if it's already available. - * @param {Snowflake|FetchApplicationCommandOptions} [id] Options for fetching application command(s) - * @param {FetchApplicationCommandOptions} [options] Additional options for this fetch + * @param {Snowflake|FetchApplicationCommandOptions} [options] Options for fetching application command(s) * @returns {Promise>} * @example * // Fetch a single command @@ -98,28 +98,50 @@ class ApplicationCommandManager extends CachedManager { * .catch(console.error); * @example * // Fetch all commands + * client.application.commands.fetch() + * .then(commands => console.log(`Fetched ${commands.size} commands`)) + * .catch(console.error); + * @example + * // Fetch all commands in a guild * guild.commands.fetch() * .then(commands => console.log(`Fetched ${commands.size} commands`)) * .catch(console.error); + * @example + * // Fetch a single command without checking cache + * guild.commands.fetch({ id: '123456789012345678', force: true }) + * .then(command => console.log(`Fetched command ${command.name}`)) + * .catch(console.error) */ - async fetch(id, { guildId, cache = true, force = false, locale, withLocalizations } = {}) { - if (typeof id === 'object') { - ({ guildId, cache = true, locale, withLocalizations } = id); - } else if (id) { - if (!force) { - const existing = this.cache.get(id); - if (existing) return existing; - } - const command = await this.client.rest.get(this.commandPath({ id, guildId })); - return this._add(command, cache); + async fetch(options) { + if (!options) return this._fetchMany(); + + if (typeof options === 'string') return this._fetchSingle({ id: options }); + + const { cache, force, guildId, id, locale, withLocalizations } = options; + + if (id) return this._fetchSingle({ cache, force, guildId, id }); + + return this._fetchMany({ cache, guildId, locale, withLocalizations }); + } + + async _fetchSingle({ cache, force = false, guildId, id }) { + if (!force) { + const existing = this.cache.get(id); + if (existing) return existing; } + const command = await this.client.rest.get(this.commandPath({ id, guildId })); + return this._add(command, cache); + } + + async _fetchMany({ cache, guildId, locale, withLocalizations } = {}) { const data = await this.client.rest.get(this.commandPath({ guildId }), { headers: { 'X-Discord-Locale': locale, }, query: makeURLSearchParams({ with_localizations: withLocalizations }), }); + return data.reduce((coll, command) => coll.set(command.id, this._add(command, cache, guildId)), new Collection()); } diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 0ed841fe6bba..ffb3722b2b6d 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -3913,14 +3913,13 @@ export class ApplicationCommandManager< guildId: Snowflake, ): Promise; public fetch( - id: Snowflake, - options: FetchApplicationCommandOptions & { guildId: Snowflake }, + options: Snowflake | (Omit & { id: Snowflake }), + ): Promise; + public fetch( + options: FetchApplicationCommandOptions & { id: Snowflake; guildId: Snowflake }, ): Promise; - public fetch(options: FetchApplicationCommandOptions): Promise>; - public fetch(id: Snowflake, options?: FetchApplicationCommandOptions): Promise; public fetch( - id?: Snowflake, - options?: FetchApplicationCommandOptions, + options?: Omit, ): Promise>; public set( commands: readonly ApplicationCommandDataResolvable[], @@ -4089,11 +4088,11 @@ export class GuildApplicationCommandManager extends ApplicationCommandManager, ): Promise; - public fetch(id: Snowflake, options?: FetchGuildApplicationCommandFetchOptions): Promise; - public fetch(options: FetchGuildApplicationCommandFetchOptions): Promise>; public fetch( - id?: undefined, - options?: FetchGuildApplicationCommandFetchOptions, + options: Snowflake | (FetchGuildApplicationCommandFetchOptions & { id: Snowflake }), + ): Promise; + public fetch( + options?: Omit, ): Promise>; public set(commands: readonly ApplicationCommandDataResolvable[]): Promise>; } @@ -5461,6 +5460,7 @@ export type EmojiIdentifierResolvable = export type EmojiResolvable = Snowflake | GuildEmoji | ReactionEmoji | ApplicationEmoji; export interface FetchApplicationCommandOptions extends BaseFetchOptions { + id?: Snowflake; guildId?: Snowflake; locale?: Locale; withLocalizations?: boolean; diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index bda6ff0893ea..b77a4f4acdb3 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -712,8 +712,8 @@ client.on('clientReady', async client => { // Test command manager methods const globalCommand = await client.application?.commands.fetch(globalCommandId); - const guildCommandFromGlobal = await client.application?.commands.fetch(guildCommandId, { guildId: testGuildId }); - const guildCommandFromGuild = await client.guilds.cache.get(testGuildId)?.commands.fetch(guildCommandId); + const guildCommandFromGlobal = await client.application?.commands.fetch({ id: guildCommandId, guildId: testGuildId }); + const guildCommandFromGuild = await client.guilds.cache.get(testGuildId)?.commands.fetch({ id: guildCommandId }); await client.application?.commands.create(slashCommandBuilder); await client.application?.commands.create(contextMenuCommandBuilder); @@ -1589,9 +1589,9 @@ declare const autoModerationRuleManager: AutoModerationRuleManager; } declare const guildApplicationCommandManager: GuildApplicationCommandManager; -expectType>>(guildApplicationCommandManager.fetch()); -expectType>>(guildApplicationCommandManager.fetch(undefined, {})); expectType>(guildApplicationCommandManager.fetch('0')); +expectType>(guildApplicationCommandManager.fetch({ id: '0' })); +expectType>>(guildApplicationCommandManager.fetch()); declare const categoryChannelChildManager: CategoryChannelChildManager; {