Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: consolidate parameters into a single options object #10718

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions packages/discord.js/src/managers/ApplicationCommandManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ 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
*/

/**
* 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
Expand All @@ -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;
sdanialraza marked this conversation as resolved.
Show resolved Hide resolved

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());
}

Expand Down
23 changes: 13 additions & 10 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3913,14 +3913,16 @@ export class ApplicationCommandManager<
guildId: Snowflake,
): Promise<ApplicationCommand>;
public fetch(
id: Snowflake,
options: FetchApplicationCommandOptions & { guildId: Snowflake },
options: Snowflake | (Omit<FetchApplicationCommandOptions, 'guildId'> & { id: Snowflake }),
): Promise<ApplicationCommandScope>;
public fetch(
options: FetchApplicationCommandOptions & { id: Snowflake; guildId: Snowflake },
): Promise<ApplicationCommand>;
public fetch(options: FetchApplicationCommandOptions): Promise<Collection<Snowflake, ApplicationCommandScope>>;
public fetch(id: Snowflake, options?: FetchApplicationCommandOptions): Promise<ApplicationCommandScope>;
public fetch(
id?: Snowflake,
options?: FetchApplicationCommandOptions,
options: Omit<FetchApplicationCommandOptions, 'id'> & { guildId: Snowflake },
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
public fetch(
options?: Omit<FetchApplicationCommandOptions, 'id' | 'guildId'>,
): Promise<Collection<Snowflake, ApplicationCommandScope>>;
sdanialraza marked this conversation as resolved.
Show resolved Hide resolved
public set(
commands: readonly ApplicationCommandDataResolvable[],
Expand Down Expand Up @@ -4089,11 +4091,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>>;
}
Expand Down Expand Up @@ -5461,6 +5463,7 @@ export type EmojiIdentifierResolvable =
export type EmojiResolvable = Snowflake | GuildEmoji | ReactionEmoji | ApplicationEmoji;

export interface FetchApplicationCommandOptions extends BaseFetchOptions {
id?: Snowflake;
guildId?: Snowflake;
locale?: Locale;
withLocalizations?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
{
Expand Down
Loading