mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
refactor!: consolidate parameters into a single options object (#10718)
BREAKING CHANGE: `ApplicationCommandManager#fetch` and `GuildApplicationCommandManager#fetch` no longer accept 2 parameters. Instead, the first parameter accepts `id` or `options` which can now also have the `id` property. --------- Co-authored-by: Micah Benac <OfficialSirH@users.noreply.github.com> Co-authored-by: Almeida <github@almeidx.dev>
This commit is contained in:
@@ -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<ApplicationCommand|Collection<Snowflake, ApplicationCommand>>}
|
||||
* @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());
|
||||
}
|
||||
|
||||
|
||||
22
packages/discord.js/typings/index.d.ts
vendored
22
packages/discord.js/typings/index.d.ts
vendored
@@ -3913,14 +3913,13 @@ export class ApplicationCommandManager<
|
||||
guildId: Snowflake,
|
||||
): Promise<ApplicationCommand>;
|
||||
public fetch(
|
||||
id: Snowflake,
|
||||
options: FetchApplicationCommandOptions & { guildId: Snowflake },
|
||||
): Promise<ApplicationCommand>;
|
||||
public fetch(options: FetchApplicationCommandOptions): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
||||
public fetch(id: Snowflake, options?: FetchApplicationCommandOptions): Promise<ApplicationCommandScope>;
|
||||
options: Snowflake | (Omit<FetchApplicationCommandOptions, 'guildId'> & { id: Snowflake }),
|
||||
): Promise<ApplicationCommandScope>;
|
||||
public fetch(
|
||||
id?: Snowflake,
|
||||
options?: FetchApplicationCommandOptions,
|
||||
options: FetchApplicationCommandOptions & { id: Snowflake; guildId: Snowflake },
|
||||
): Promise<ApplicationCommand>;
|
||||
public fetch(
|
||||
options?: Omit<FetchApplicationCommandOptions, 'id'>,
|
||||
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
||||
public set(
|
||||
commands: readonly ApplicationCommandDataResolvable[],
|
||||
@@ -4089,11 +4088,11 @@ export class GuildApplicationCommandManager extends ApplicationCommandManager<Ap
|
||||
command: ApplicationCommandResolvable,
|
||||
data: Partial<ApplicationCommandDataResolvable>,
|
||||
): Promise<ApplicationCommand>;
|
||||
public fetch(id: Snowflake, options?: FetchGuildApplicationCommandFetchOptions): Promise<ApplicationCommand>;
|
||||
public fetch(options: FetchGuildApplicationCommandFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||
public fetch(
|
||||
id?: undefined,
|
||||
options?: FetchGuildApplicationCommandFetchOptions,
|
||||
options: Snowflake | (FetchGuildApplicationCommandFetchOptions & { id: Snowflake }),
|
||||
): Promise<ApplicationCommand>;
|
||||
public fetch(
|
||||
options?: Omit<FetchGuildApplicationCommandFetchOptions, 'id'>,
|
||||
): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||
public set(commands: readonly ApplicationCommandDataResolvable[]): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Promise<Collection<Snowflake, ApplicationCommand>>>(guildApplicationCommandManager.fetch());
|
||||
expectType<Promise<Collection<Snowflake, ApplicationCommand>>>(guildApplicationCommandManager.fetch(undefined, {}));
|
||||
expectType<Promise<ApplicationCommand>>(guildApplicationCommandManager.fetch('0'));
|
||||
expectType<Promise<ApplicationCommand>>(guildApplicationCommandManager.fetch({ id: '0' }));
|
||||
expectType<Promise<Collection<Snowflake, ApplicationCommand>>>(guildApplicationCommandManager.fetch());
|
||||
|
||||
declare const categoryChannelChildManager: CategoryChannelChildManager;
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user