From 8cfadb6953b86fbdb3ef3c94d14653c519c9ce17 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:44:21 +0200 Subject: [PATCH] docs: Remove duplicate `APIEmoji` (#9880) * types: remove duplicate type definition * chore: add `Emoji` to method * types(resolvePartialEmoji): overload method --- packages/discord.js/src/structures/Emoji.js | 13 ------------- packages/discord.js/src/util/Util.js | 20 +++++++++++++++++--- packages/discord.js/typings/index.d.ts | 15 +++++++++++++-- packages/discord.js/typings/index.test-d.ts | 10 ++++++++++ 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/packages/discord.js/src/structures/Emoji.js b/packages/discord.js/src/structures/Emoji.js index 409d29258eb0..16c22f9271b5 100644 --- a/packages/discord.js/src/structures/Emoji.js +++ b/packages/discord.js/src/structures/Emoji.js @@ -3,14 +3,6 @@ const { DiscordSnowflake } = require('@sapphire/snowflake'); const Base = require('./Base'); -/** - * Represents raw emoji data from the API - * @typedef {APIEmoji} RawEmoji - * @property {?Snowflake} id The emoji's id - * @property {?string} name The emoji's name - * @property {?boolean} animated Whether the emoji is animated - */ - /** * Represents an emoji, see {@link GuildEmoji} and {@link ReactionEmoji}. * @extends {Base} @@ -101,8 +93,3 @@ class Emoji extends Base { } exports.Emoji = Emoji; - -/** - * @external APIEmoji - * @see {@link https://discord.com/developers/docs/resources/emoji#emoji-object} - */ diff --git a/packages/discord.js/src/util/Util.js b/packages/discord.js/src/util/Util.js index e49c43b4a6f4..c4a7e320f666 100644 --- a/packages/discord.js/src/util/Util.js +++ b/packages/discord.js/src/util/Util.js @@ -79,13 +79,21 @@ async function fetchRecommendedShardCount(token, { guildsPerShard = 1_000, multi return Math.ceil((shards * (1_000 / guildsPerShard)) / multipleOf) * multipleOf; } +/** + * A partial emoji object. + * @typedef {Object} PartialEmoji + * @property {boolean} animated Whether the emoji is animated + * @property {Snowflake|undefined} id The id of the emoji + * @property {string} name The name of the emoji + */ + /** * Parses emoji info out of a string. The string must be one of: * * A UTF-8 emoji (no id) * * A URL-encoded UTF-8 emoji (no id) * * A Discord custom emoji (`<:name:id>` or ``) * @param {string} text Emoji string to parse - * @returns {APIEmoji} Object with `animated`, `name`, and `id` properties + * @returns {?PartialEmoji} */ function parseEmoji(text) { if (text.includes('%')) text = decodeURIComponent(text); @@ -94,10 +102,16 @@ function parseEmoji(text) { return match && { animated: Boolean(match[1]), name: match[2], id: match[3] }; } +/** + * A partial emoji object with only an id. + * @typedef {Object} PartialEmojiOnlyId + * @property {Snowflake} id The id of the emoji + */ + /** * Resolves a partial emoji object from an {@link EmojiIdentifierResolvable}, without checking a Client. - * @param {EmojiIdentifierResolvable} emoji Emoji identifier to resolve - * @returns {?RawEmoji} + * @param {Emoji|EmojiIdentifierResolvable} emoji Emoji identifier to resolve + * @returns {?(PartialEmoji|PartialEmojiOnlyId)} Suppling a snowflake yields `PartialEmojiOnlyId`. * @private */ function resolvePartialEmoji(emoji) { diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index e69c7b3b0cab..c24caa2e4d79 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -3202,9 +3202,10 @@ export function makeError(obj: MakeErrorOptions): Error; export function makePlainError(err: Error): MakeErrorOptions; export function mergeDefault(def: unknown, given: unknown): unknown; export function moveElementInArray(array: unknown[], element: unknown, newIndex: number, offset?: boolean): number; -export function parseEmoji(text: string): { animated: boolean; name: string; id: Snowflake | null } | null; +export function parseEmoji(text: string): PartialEmoji | null; export function resolveColor(color: ColorResolvable): number; -export function resolvePartialEmoji(emoji: EmojiIdentifierResolvable): Partial | null; +export function resolvePartialEmoji(emoji: Snowflake): PartialEmojiOnlyId; +export function resolvePartialEmoji(emoji: Emoji | EmojiIdentifierResolvable): PartialEmoji | null; export function verifyString(data: string, error?: typeof Error, errorMessage?: string, allowEmpty?: boolean): string; export function setPosition( item: T, @@ -6159,6 +6160,16 @@ export interface PartialChannelData { rateLimitPerUser?: number; } +export interface PartialEmoji { + animated: boolean; + id: Snowflake | undefined; + name: string; +} + +export interface PartialEmojiOnlyId { + id: Snowflake; +} + export type Partialize< T extends AllowedPartial, NulledKeys extends keyof T | null = null, diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index a44e8805f238..e0364a69a0cf 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -181,6 +181,10 @@ import { PartialGuildMember, PartialMessage, PartialMessageReaction, + resolvePartialEmoji, + PartialEmojiOnlyId, + Emoji, + PartialEmoji, } from '.'; import { expectAssignable, expectNotAssignable, expectNotType, expectType } from 'tsd'; import type { ContextMenuCommandBuilder, SlashCommandBuilder } from '@discordjs/builders'; @@ -2363,3 +2367,9 @@ expectType(partialUser.partial); expectType(partialUser.username); expectType(partialUser.tag); expectType(partialUser.discriminator); + +declare const emoji: Emoji; +{ + expectType(resolvePartialEmoji('12345678901234567')); + expectType(resolvePartialEmoji(emoji)); +}