Skip to content

Commit

Permalink
fix: fetching bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Apr 3, 2022
1 parent 0fbcca4 commit 6e6ee58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
24 changes: 11 additions & 13 deletions packages/discord.js/src/managers/ApplicationCommandManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,26 @@ class ApplicationCommandManager extends CachedManager {
* .catch(console.error);
*/
async fetch(id, { guildId, cache = true, force = false, locale, withLocalizations } = {}) {
const fetchOptions = {
headers: {
'X-Discord-Locale': locale,
},
query:
typeof withLocalizations === 'boolean'
? new URLSearchParams({ with_localizations: withLocalizations })
: undefined,
};

if (typeof id === 'object') {
({ guildId, cache = true } = id);
({ 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 }), fetchOptions);
const command = await this.client.rest.get(this.commandPath({ id, guildId }));
return this._add(command, cache);
}

const data = await this.client.rest.get(this.commandPath({ guildId }), fetchOptions);
const data = await this.client.rest.get(this.commandPath({ guildId }), {
headers: {
'X-Discord-Locale': locale,
},
query:
typeof withLocalizations === 'boolean'
? new URLSearchParams({ with_localizations: withLocalizations })
: undefined,
});
return data.reduce((coll, command) => coll.set(command.id, this._add(command, cache, guildId)), new Collection());
}

Expand Down
11 changes: 8 additions & 3 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2996,6 +2996,8 @@ export class ChannelManager extends CachedManager<Snowflake, AnyChannel, Channel
public fetch(id: Snowflake, options?: FetchChannelOptions): Promise<AnyChannel | null>;
}

export type FetchGuildApplicationCommandFetchOptions = Omit<FetchApplicationCommandOptions, 'guildId'>;

export class GuildApplicationCommandManager extends ApplicationCommandManager<ApplicationCommand, {}, Guild> {
private constructor(guild: Guild, iterable?: Iterable<RawApplicationCommandData>);
public guild: Guild;
Expand All @@ -3005,9 +3007,12 @@ export class GuildApplicationCommandManager extends ApplicationCommandManager<Ap
command: ApplicationCommandResolvable,
data: ApplicationCommandDataResolvable,
): Promise<ApplicationCommand>;
public fetch(id: Snowflake, options?: BaseFetchOptions): Promise<ApplicationCommand>;
public fetch(options: BaseFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
public fetch(id?: undefined, options?: BaseFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
public fetch(id: Snowflake, options?: FetchGuildApplicationCommandFetchOptions): Promise<ApplicationCommand>;
public fetch(options: FetchGuildApplicationCommandFetchOptions): Promise<Collection<Snowflake, ApplicationCommand>>;
public fetch(
id?: undefined,
options?: FetchGuildApplicationCommandFetchOptions,
): Promise<Collection<Snowflake, ApplicationCommand>>;
public set(commands: ApplicationCommandDataResolvable[]): Promise<Collection<Snowflake, ApplicationCommand>>;
}

Expand Down

0 comments on commit 6e6ee58

Please sign in to comment.