Skip to content

Commit 1b1ae2f

Browse files
almeidxvladfrangukodiakhq[bot]
authoredAug 20, 2024··
feat: use get sticker pack endpoint (#10445)
* feat: use get sticker pack endpoint * fix: mark fetchPack as async * style: resolve eslint warning --------- Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 1f7d1f8 commit 1b1ae2f

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed
 

‎packages/core/src/api/sticker.ts

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type { RequestData, REST } from '@discordjs/rest';
44
import {
55
Routes,
6+
type RESTGetAPIStickerPack,
67
type RESTGetAPIStickerResult,
78
type RESTGetStickerPacksResult,
89
type Snowflake,
@@ -11,6 +12,17 @@ import {
1112
export class StickersAPI {
1213
public constructor(private readonly rest: REST) {}
1314

15+
/**
16+
* Fetches a sticker pack
17+
*
18+
* @see {@link https://discord.com/developers/docs/resources/sticker#get-sticker-pack}
19+
* @param packId - The id of the sticker pack
20+
* @param options - The options for fetching the sticker pack
21+
*/
22+
public async getStickerPack(packId: Snowflake, { signal }: Pick<RequestData, 'signal'> = {}) {
23+
return this.rest.get(Routes.stickerPack(packId), { signal }) as Promise<RESTGetAPIStickerPack>;
24+
}
25+
1426
/**
1527
* Fetches all of the sticker packs
1628
*

‎packages/discord.js/src/client/Client.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,32 @@ class Client extends BaseClient {
342342
return new Sticker(this, data);
343343
}
344344

345+
/**
346+
* Options for fetching sticker packs.
347+
* @typedef {Object} StickerPackFetchOptions
348+
* @property {Snowflake} [packId] The id of the sticker pack to fetch
349+
*/
350+
345351
/**
346352
* Obtains the list of available sticker packs.
347-
* @returns {Promise<Collection<Snowflake, StickerPack>>}
353+
* @param {StickerPackFetchOptions} [options={}] Options for fetching sticker packs
354+
* @returns {Promise<Collection<Snowflake, StickerPack>|StickerPack>}
355+
* A collection of sticker packs, or a single sticker pack if a packId was provided
348356
* @example
349357
* client.fetchStickerPacks()
350358
* .then(packs => console.log(`Available sticker packs are: ${packs.map(pack => pack.name).join(', ')}`))
351359
* .catch(console.error);
360+
* @example
361+
* client.fetchStickerPacks({ packId: '751604115435421716' })
362+
* .then(pack => console.log(`Sticker pack name: ${pack.name}`))
363+
* .catch(console.error);
352364
*/
353-
async fetchStickerPacks() {
365+
async fetchStickerPacks({ packId } = {}) {
366+
if (packId) {
367+
const data = await this.rest.get(Routes.stickerPack(packId));
368+
return new StickerPack(this, data);
369+
}
370+
354371
const data = await this.rest.get(Routes.stickerPacks());
355372
return new Collection(data.sticker_packs.map(stickerPack => [stickerPack.id, new StickerPack(this, stickerPack)]));
356373
}

‎packages/discord.js/src/structures/Sticker.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ class Sticker extends Base {
182182
* Fetches the pack that contains this sticker.
183183
* @returns {Promise<?StickerPack>} The sticker pack or `null` if this sticker does not belong to one.
184184
*/
185-
async fetchPack() {
186-
return (this.packId && (await this.client.fetchStickerPacks()).get(this.packId)) ?? null;
185+
fetchPack() {
186+
if (!this.packId) return Promise.resolve(null);
187+
return this.client.fetchStickerPacks({ packId: this.packId });
187188
}
188189

189190
/**

‎packages/discord.js/typings/index.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,8 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
10191019
public fetchGuildTemplate(template: GuildTemplateResolvable): Promise<GuildTemplate>;
10201020
public fetchVoiceRegions(): Promise<Collection<string, VoiceRegion>>;
10211021
public fetchSticker(id: Snowflake): Promise<Sticker>;
1022-
public fetchStickerPacks(): Promise<Collection<Snowflake, StickerPack>>;
1022+
public fetchStickerPacks(options: { packId: Snowflake }): Promise<StickerPack>;
1023+
public fetchStickerPacks(options?: StickerPackFetchOptions): Promise<Collection<Snowflake, StickerPack>>;
10231024
/** @deprecated Use {@link Client.fetchStickerPacks} instead. */
10241025
public fetchPremiumStickerPacks(): ReturnType<Client['fetchStickerPacks']>;
10251026
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
@@ -1054,6 +1055,10 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
10541055
public removeAllListeners<Event extends string | symbol>(event?: Exclude<Event, keyof ClientEvents>): this;
10551056
}
10561057

1058+
export interface StickerPackFetchOptions {
1059+
packId?: Snowflake;
1060+
}
1061+
10571062
export class ClientApplication extends Application {
10581063
private constructor(client: Client<true>, data: RawClientApplicationData);
10591064
public botPublic: boolean | null;

‎packages/discord.js/typings/index.test-d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ import {
208208
Poll,
209209
ApplicationEmoji,
210210
ApplicationEmojiManager,
211+
StickerPack,
211212
} from '.';
212213
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
213214
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
@@ -2587,3 +2588,7 @@ declare const poll: Poll;
25872588
answerId: 1,
25882589
});
25892590
}
2591+
2592+
expectType<Collection<Snowflake, StickerPack>>(await client.fetchStickerPacks());
2593+
expectType<Collection<Snowflake, StickerPack>>(await client.fetchStickerPacks({}));
2594+
expectType<StickerPack>(await client.fetchStickerPacks({ packId: snowflake }));

0 commit comments

Comments
 (0)