From 632ab4cca23a89194e931302134b2f61c575d07a Mon Sep 17 00:00:00 2001 From: Almeida Date: Mon, 19 Aug 2024 13:54:07 +0000 Subject: [PATCH] feat: use get sticker pack endpoint --- packages/core/src/api/sticker.ts | 12 +++++++++++ packages/discord.js/src/client/Client.js | 21 +++++++++++++++++-- packages/discord.js/src/structures/Sticker.js | 5 +++-- packages/discord.js/typings/index.d.ts | 7 ++++++- packages/discord.js/typings/index.test-d.ts | 5 +++++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/packages/core/src/api/sticker.ts b/packages/core/src/api/sticker.ts index 6734f23f4834..1d5dcf1e0202 100644 --- a/packages/core/src/api/sticker.ts +++ b/packages/core/src/api/sticker.ts @@ -3,6 +3,7 @@ import type { RequestData, REST } from '@discordjs/rest'; import { Routes, + type RESTGetAPIStickerPack, type RESTGetAPIStickerResult, type RESTGetStickerPacksResult, type Snowflake, @@ -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 = {}) { + return this.rest.get(Routes.stickerPack(packId), { signal }) as Promise; + } + /** * Fetches all of the sticker packs * diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index f45b0388b2cb..27b2fac46b2b 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -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>} + * @param {StickerPackFetchOptions} [options={}] Options for fetching sticker packs + * @returns {Promise|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)])); } diff --git a/packages/discord.js/src/structures/Sticker.js b/packages/discord.js/src/structures/Sticker.js index 07f859969453..67258aafd93a 100644 --- a/packages/discord.js/src/structures/Sticker.js +++ b/packages/discord.js/src/structures/Sticker.js @@ -182,8 +182,9 @@ class Sticker extends Base { * Fetches the pack that contains this sticker. * @returns {Promise} 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; + fetchPack() { + if (!this.packId) return null; + return this.client.fetchStickerPacks({ packId: this.packId }); } /** diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index b1071e1d3558..02a105ed8c6d 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1020,7 +1020,8 @@ export class Client extends BaseClient { public fetchGuildTemplate(template: GuildTemplateResolvable): Promise; public fetchVoiceRegions(): Promise>; public fetchSticker(id: Snowflake): Promise; - public fetchStickerPacks(): Promise>; + public fetchStickerPacks(options: { packId: Snowflake }): Promise; + public fetchStickerPacks(options?: StickerPackFetchOptions): Promise>; /** @deprecated Use {@link Client.fetchStickerPacks} instead. */ public fetchPremiumStickerPacks(): ReturnType; public fetchWebhook(id: Snowflake, token?: string): Promise; @@ -1055,6 +1056,10 @@ export class Client extends BaseClient { public removeAllListeners(event?: Exclude): this; } +export interface StickerPackFetchOptions { + packId?: Snowflake; +} + export class ClientApplication extends Application { private constructor(client: Client, data: RawClientApplicationData); public botPublic: boolean | null; diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index 407f6023e8d2..7ce3ad2c5b4d 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -205,6 +205,7 @@ import { ChannelSelectMenuComponent, MentionableSelectMenuComponent, Poll, + StickerPack, } from '.'; import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd'; import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders'; @@ -2561,3 +2562,7 @@ declare const poll: Poll; answerId: 1, }); } + +expectType>(await client.fetchStickerPacks()); +expectType>(await client.fetchStickerPacks({})); +expectType(await client.fetchStickerPacks({ packId: snowflake }));