mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 13:03: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
|
* Options used to fetch Application Commands from Discord
|
||||||
* @typedef {BaseFetchOptions} FetchApplicationCommandOptions
|
* @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 {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 {Locale} [locale] The locale to use when fetching this command
|
||||||
* @property {boolean} [withLocalizations] Whether to fetch all localization data
|
* @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.
|
* 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 {Snowflake|FetchApplicationCommandOptions} [options] Options for fetching application command(s)
|
||||||
* @param {FetchApplicationCommandOptions} [options] Additional options for this fetch
|
|
||||||
* @returns {Promise<ApplicationCommand|Collection<Snowflake, ApplicationCommand>>}
|
* @returns {Promise<ApplicationCommand|Collection<Snowflake, ApplicationCommand>>}
|
||||||
* @example
|
* @example
|
||||||
* // Fetch a single command
|
* // Fetch a single command
|
||||||
@@ -98,28 +98,50 @@ class ApplicationCommandManager extends CachedManager {
|
|||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
* @example
|
* @example
|
||||||
* // Fetch all commands
|
* // 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()
|
* guild.commands.fetch()
|
||||||
* .then(commands => console.log(`Fetched ${commands.size} commands`))
|
* .then(commands => console.log(`Fetched ${commands.size} commands`))
|
||||||
* .catch(console.error);
|
* .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 } = {}) {
|
async fetch(options) {
|
||||||
if (typeof id === 'object') {
|
if (!options) return this._fetchMany();
|
||||||
({ guildId, cache = true, locale, withLocalizations } = id);
|
|
||||||
} else if (id) {
|
if (typeof options === 'string') return this._fetchSingle({ id: options });
|
||||||
if (!force) {
|
|
||||||
const existing = this.cache.get(id);
|
const { cache, force, guildId, id, locale, withLocalizations } = options;
|
||||||
if (existing) return existing;
|
|
||||||
}
|
if (id) return this._fetchSingle({ cache, force, guildId, id });
|
||||||
const command = await this.client.rest.get(this.commandPath({ id, guildId }));
|
|
||||||
return this._add(command, cache);
|
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 }), {
|
const data = await this.client.rest.get(this.commandPath({ guildId }), {
|
||||||
headers: {
|
headers: {
|
||||||
'X-Discord-Locale': locale,
|
'X-Discord-Locale': locale,
|
||||||
},
|
},
|
||||||
query: makeURLSearchParams({ with_localizations: withLocalizations }),
|
query: makeURLSearchParams({ with_localizations: withLocalizations }),
|
||||||
});
|
});
|
||||||
|
|
||||||
return data.reduce((coll, command) => coll.set(command.id, this._add(command, cache, guildId)), new Collection());
|
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,
|
guildId: Snowflake,
|
||||||
): Promise<ApplicationCommand>;
|
): Promise<ApplicationCommand>;
|
||||||
public fetch(
|
public fetch(
|
||||||
id: Snowflake,
|
options: Snowflake | (Omit<FetchApplicationCommandOptions, 'guildId'> & { id: Snowflake }),
|
||||||
options: FetchApplicationCommandOptions & { guildId: Snowflake },
|
): Promise<ApplicationCommandScope>;
|
||||||
): Promise<ApplicationCommand>;
|
|
||||||
public fetch(options: FetchApplicationCommandOptions): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
|
||||||
public fetch(id: Snowflake, options?: FetchApplicationCommandOptions): Promise<ApplicationCommandScope>;
|
|
||||||
public fetch(
|
public fetch(
|
||||||
id?: Snowflake,
|
options: FetchApplicationCommandOptions & { id: Snowflake; guildId: Snowflake },
|
||||||
options?: FetchApplicationCommandOptions,
|
): Promise<ApplicationCommand>;
|
||||||
|
public fetch(
|
||||||
|
options?: Omit<FetchApplicationCommandOptions, 'id'>,
|
||||||
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
|
||||||
public set(
|
public set(
|
||||||
commands: readonly ApplicationCommandDataResolvable[],
|
commands: readonly ApplicationCommandDataResolvable[],
|
||||||
@@ -4089,11 +4088,11 @@ export class GuildApplicationCommandManager extends ApplicationCommandManager<Ap
|
|||||||
command: ApplicationCommandResolvable,
|
command: ApplicationCommandResolvable,
|
||||||
data: Partial<ApplicationCommandDataResolvable>,
|
data: Partial<ApplicationCommandDataResolvable>,
|
||||||
): Promise<ApplicationCommand>;
|
): Promise<ApplicationCommand>;
|
||||||
public fetch(id: Snowflake, options?: FetchGuildApplicationCommandFetchOptions): Promise<ApplicationCommand>;
|
|
||||||
public fetch(options: FetchGuildApplicationCommandFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
|
|
||||||
public fetch(
|
public fetch(
|
||||||
id?: undefined,
|
options: Snowflake | (FetchGuildApplicationCommandFetchOptions & { id: Snowflake }),
|
||||||
options?: FetchGuildApplicationCommandFetchOptions,
|
): Promise<ApplicationCommand>;
|
||||||
|
public fetch(
|
||||||
|
options?: Omit<FetchGuildApplicationCommandFetchOptions, 'id'>,
|
||||||
): Promise<Collection<Snowflake, ApplicationCommand>>;
|
): Promise<Collection<Snowflake, ApplicationCommand>>;
|
||||||
public set(commands: readonly ApplicationCommandDataResolvable[]): 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 type EmojiResolvable = Snowflake | GuildEmoji | ReactionEmoji | ApplicationEmoji;
|
||||||
|
|
||||||
export interface FetchApplicationCommandOptions extends BaseFetchOptions {
|
export interface FetchApplicationCommandOptions extends BaseFetchOptions {
|
||||||
|
id?: Snowflake;
|
||||||
guildId?: Snowflake;
|
guildId?: Snowflake;
|
||||||
locale?: Locale;
|
locale?: Locale;
|
||||||
withLocalizations?: boolean;
|
withLocalizations?: boolean;
|
||||||
|
|||||||
@@ -712,8 +712,8 @@ client.on('clientReady', async client => {
|
|||||||
|
|
||||||
// Test command manager methods
|
// Test command manager methods
|
||||||
const globalCommand = await client.application?.commands.fetch(globalCommandId);
|
const globalCommand = await client.application?.commands.fetch(globalCommandId);
|
||||||
const guildCommandFromGlobal = await client.application?.commands.fetch(guildCommandId, { guildId: testGuildId });
|
const guildCommandFromGlobal = await client.application?.commands.fetch({ id: guildCommandId, guildId: testGuildId });
|
||||||
const guildCommandFromGuild = await client.guilds.cache.get(testGuildId)?.commands.fetch(guildCommandId);
|
const guildCommandFromGuild = await client.guilds.cache.get(testGuildId)?.commands.fetch({ id: guildCommandId });
|
||||||
|
|
||||||
await client.application?.commands.create(slashCommandBuilder);
|
await client.application?.commands.create(slashCommandBuilder);
|
||||||
await client.application?.commands.create(contextMenuCommandBuilder);
|
await client.application?.commands.create(contextMenuCommandBuilder);
|
||||||
@@ -1589,9 +1589,9 @@ declare const autoModerationRuleManager: AutoModerationRuleManager;
|
|||||||
}
|
}
|
||||||
|
|
||||||
declare const guildApplicationCommandManager: GuildApplicationCommandManager;
|
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('0'));
|
||||||
|
expectType<Promise<ApplicationCommand>>(guildApplicationCommandManager.fetch({ id: '0' }));
|
||||||
|
expectType<Promise<Collection<Snowflake, ApplicationCommand>>>(guildApplicationCommandManager.fetch());
|
||||||
|
|
||||||
declare const categoryChannelChildManager: CategoryChannelChildManager;
|
declare const categoryChannelChildManager: CategoryChannelChildManager;
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user