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

feat: use get sticker pack endpoint #10445

Merged
merged 7 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions packages/core/src/api/sticker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { RequestData, REST } from '@discordjs/rest';
import {
Routes,
type RESTGetAPIStickerPack,
type RESTGetAPIStickerResult,
type RESTGetStickerPacksResult,
type Snowflake,
Expand All @@ -11,6 +12,17 @@ import {
export class StickersAPI {
public constructor(private readonly rest: REST) {}

/**
* Fetches a sticker pack
*
* @see {@link https://discord.com/developers/docs/resources/sticker#get-sticker-pack}
* @param packId - The id of the sticker pack
* @param options - The options for fetching the sticker pack
*/
public async getStickerPack(packId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.get(Routes.stickerPack(packId), { signal }) as Promise<RESTGetAPIStickerPack>;
}

/**
* Fetches all of the sticker packs
*
Expand Down
21 changes: 19 additions & 2 deletions packages/discord.js/src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,32 @@ class Client extends BaseClient {
return new Sticker(this, data);
}

/**
* Options for fetching sticker packs.
* @typedef {Object} StickerPackFetchOptions
* @property {Snowflake} [packId] The id of the sticker pack to fetch
*/

/**
* Obtains the list of available sticker packs.
* @returns {Promise<Collection<Snowflake, StickerPack>>}
* @param {StickerPackFetchOptions} [options={}] Options for fetching sticker packs
* @returns {Promise<Collection<Snowflake, StickerPack>|StickerPack>}
* A collection of sticker packs, or a single sticker pack if a packId was provided
* @example
* client.fetchStickerPacks()
* .then(packs => console.log(`Available sticker packs are: ${packs.map(pack => pack.name).join(', ')}`))
* .catch(console.error);
* @example
* client.fetchStickerPacks({ packId: '751604115435421716' })
* .then(pack => console.log(`Sticker pack name: ${pack.name}`))
* .catch(console.error);
*/
async fetchStickerPacks() {
async fetchStickerPacks({ packId } = {}) {
if (packId) {
const data = await this.rest.get(Routes.stickerPack(packId));
return new StickerPack(this, data);
}

const data = await this.rest.get(Routes.stickerPacks());
return new Collection(data.sticker_packs.map(stickerPack => [stickerPack.id, new StickerPack(this, stickerPack)]));
}
Expand Down
3 changes: 2 additions & 1 deletion packages/discord.js/src/structures/Sticker.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ class Sticker extends Base {
* @returns {Promise<?StickerPack>} The sticker pack or `null` if this sticker does not belong to one.
*/
async fetchPack() {
return (this.packId && (await this.client.fetchStickerPacks()).get(this.packId)) ?? null;
if (!this.packId) return null;
return this.client.fetchStickerPacks({ packId: this.packId });
}

/**
Expand Down
7 changes: 6 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,8 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public fetchGuildTemplate(template: GuildTemplateResolvable): Promise<GuildTemplate>;
public fetchVoiceRegions(): Promise<Collection<string, VoiceRegion>>;
public fetchSticker(id: Snowflake): Promise<Sticker>;
public fetchStickerPacks(): Promise<Collection<Snowflake, StickerPack>>;
public fetchStickerPacks(options: { packId: Snowflake }): Promise<StickerPack>;
public fetchStickerPacks(options?: StickerPackFetchOptions): Promise<Collection<Snowflake, StickerPack>>;
/** @deprecated Use {@link Client.fetchStickerPacks} instead. */
public fetchPremiumStickerPacks(): ReturnType<Client['fetchStickerPacks']>;
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
Expand Down Expand Up @@ -1055,6 +1056,10 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public removeAllListeners<Event extends string | symbol>(event?: Exclude<Event, keyof ClientEvents>): this;
}

export interface StickerPackFetchOptions {
packId?: Snowflake;
}

export class ClientApplication extends Application {
private constructor(client: Client<true>, data: RawClientApplicationData);
public botPublic: boolean | null;
Expand Down
5 changes: 5 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ import {
ChannelSelectMenuComponent,
MentionableSelectMenuComponent,
Poll,
StickerPack,
} from '.';
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
Expand Down Expand Up @@ -2572,3 +2573,7 @@ declare const poll: Poll;
answerId: 1,
});
}

expectType<Collection<Snowflake, StickerPack>>(await client.fetchStickerPacks());
expectType<Collection<Snowflake, StickerPack>>(await client.fetchStickerPacks({}));
expectType<StickerPack>(await client.fetchStickerPacks({ packId: snowflake }));
Loading