mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 19:13:31 +01:00
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>
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
import type { RequestData, REST } from '@discordjs/rest';
|
import type { RequestData, REST } from '@discordjs/rest';
|
||||||
import {
|
import {
|
||||||
Routes,
|
Routes,
|
||||||
|
type RESTGetAPIStickerPack,
|
||||||
type RESTGetAPIStickerResult,
|
type RESTGetAPIStickerResult,
|
||||||
type RESTGetStickerPacksResult,
|
type RESTGetStickerPacksResult,
|
||||||
type Snowflake,
|
type Snowflake,
|
||||||
@@ -11,6 +12,17 @@ import {
|
|||||||
export class StickersAPI {
|
export class StickersAPI {
|
||||||
public constructor(private readonly rest: REST) {}
|
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
|
* Fetches all of the sticker packs
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -342,15 +342,32 @@ class Client extends BaseClient {
|
|||||||
return new Sticker(this, data);
|
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.
|
* 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
|
* @example
|
||||||
* client.fetchStickerPacks()
|
* client.fetchStickerPacks()
|
||||||
* .then(packs => console.log(`Available sticker packs are: ${packs.map(pack => pack.name).join(', ')}`))
|
* .then(packs => console.log(`Available sticker packs are: ${packs.map(pack => pack.name).join(', ')}`))
|
||||||
* .catch(console.error);
|
* .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());
|
const data = await this.rest.get(Routes.stickerPacks());
|
||||||
return new Collection(data.sticker_packs.map(stickerPack => [stickerPack.id, new StickerPack(this, stickerPack)]));
|
return new Collection(data.sticker_packs.map(stickerPack => [stickerPack.id, new StickerPack(this, stickerPack)]));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,8 +182,9 @@ class Sticker extends Base {
|
|||||||
* Fetches the pack that contains this sticker.
|
* Fetches the pack that contains this sticker.
|
||||||
* @returns {Promise<?StickerPack>} The sticker pack or `null` if this sticker does not belong to one.
|
* @returns {Promise<?StickerPack>} The sticker pack or `null` if this sticker does not belong to one.
|
||||||
*/
|
*/
|
||||||
async fetchPack() {
|
fetchPack() {
|
||||||
return (this.packId && (await this.client.fetchStickerPacks()).get(this.packId)) ?? null;
|
if (!this.packId) return Promise.resolve(null);
|
||||||
|
return this.client.fetchStickerPacks({ packId: this.packId });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
7
packages/discord.js/typings/index.d.ts
vendored
7
packages/discord.js/typings/index.d.ts
vendored
@@ -1019,7 +1019,8 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
|
|||||||
public fetchGuildTemplate(template: GuildTemplateResolvable): Promise<GuildTemplate>;
|
public fetchGuildTemplate(template: GuildTemplateResolvable): Promise<GuildTemplate>;
|
||||||
public fetchVoiceRegions(): Promise<Collection<string, VoiceRegion>>;
|
public fetchVoiceRegions(): Promise<Collection<string, VoiceRegion>>;
|
||||||
public fetchSticker(id: Snowflake): Promise<Sticker>;
|
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. */
|
/** @deprecated Use {@link Client.fetchStickerPacks} instead. */
|
||||||
public fetchPremiumStickerPacks(): ReturnType<Client['fetchStickerPacks']>;
|
public fetchPremiumStickerPacks(): ReturnType<Client['fetchStickerPacks']>;
|
||||||
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
|
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
|
||||||
@@ -1054,6 +1055,10 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
|
|||||||
public removeAllListeners<Event extends string | symbol>(event?: Exclude<Event, keyof ClientEvents>): this;
|
public removeAllListeners<Event extends string | symbol>(event?: Exclude<Event, keyof ClientEvents>): this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface StickerPackFetchOptions {
|
||||||
|
packId?: Snowflake;
|
||||||
|
}
|
||||||
|
|
||||||
export class ClientApplication extends Application {
|
export class ClientApplication extends Application {
|
||||||
private constructor(client: Client<true>, data: RawClientApplicationData);
|
private constructor(client: Client<true>, data: RawClientApplicationData);
|
||||||
public botPublic: boolean | null;
|
public botPublic: boolean | null;
|
||||||
|
|||||||
@@ -208,6 +208,7 @@ import {
|
|||||||
Poll,
|
Poll,
|
||||||
ApplicationEmoji,
|
ApplicationEmoji,
|
||||||
ApplicationEmojiManager,
|
ApplicationEmojiManager,
|
||||||
|
StickerPack,
|
||||||
} from '.';
|
} from '.';
|
||||||
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
import { expectAssignable, expectDeprecated, expectNotAssignable, expectNotType, expectType } from 'tsd';
|
||||||
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
|
import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders';
|
||||||
@@ -2587,3 +2588,7 @@ declare const poll: Poll;
|
|||||||
answerId: 1,
|
answerId: 1,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expectType<Collection<Snowflake, StickerPack>>(await client.fetchStickerPacks());
|
||||||
|
expectType<Collection<Snowflake, StickerPack>>(await client.fetchStickerPacks({}));
|
||||||
|
expectType<StickerPack>(await client.fetchStickerPacks({ packId: snowflake }));
|
||||||
|
|||||||
Reference in New Issue
Block a user