Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
docs: Remove duplicate APIEmoji (#9880)
* types: remove duplicate type definition

* chore: add `Emoji` to method

* types(resolvePartialEmoji): overload method
  • Loading branch information
Jiralite committed Oct 10, 2023
1 parent 7422d9f commit 8cfadb6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
13 changes: 0 additions & 13 deletions packages/discord.js/src/structures/Emoji.js
Expand Up @@ -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}
Expand Down Expand Up @@ -101,8 +93,3 @@ class Emoji extends Base {
}

exports.Emoji = Emoji;

/**
* @external APIEmoji
* @see {@link https://discord.com/developers/docs/resources/emoji#emoji-object}
*/
20 changes: 17 additions & 3 deletions packages/discord.js/src/util/Util.js
Expand Up @@ -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 `<a:name:id>`)
* @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);
Expand All @@ -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) {
Expand Down
15 changes: 13 additions & 2 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -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<APIPartialEmoji> | 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<T extends Channel | Role>(
item: T,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions packages/discord.js/typings/index.test-d.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -2363,3 +2367,9 @@ expectType<true>(partialUser.partial);
expectType<null>(partialUser.username);
expectType<null>(partialUser.tag);
expectType<null>(partialUser.discriminator);

declare const emoji: Emoji;
{
expectType<PartialEmojiOnlyId>(resolvePartialEmoji('12345678901234567'));
expectType<PartialEmoji | null>(resolvePartialEmoji(emoji));
}

0 comments on commit 8cfadb6

Please sign in to comment.