const CUSTOM_EMOJI = /^$/ const UNICODE_EMOJI = /^\p{Extended_Pictographic}[\u{1F3FB}-\u{1F3FF}]?(?:\u200D\p{Extended_Pictographic}[\u{1F3FB}-\u{1F3FF}]?)*$/u // discord sends some unicode emoji with and some without a variation // selector, so we strip it on both sides to keep comparisons stable const stripVariation = (emoji: string): string => emoji.replace(/\uFE0F/g, '') /** Normalizes a unicode emoji or a `` mention to the `name:id` / * unicode form the reactions API expects. Returns undefined for anything else. */ export function normalizeEmoji(input: string): string | undefined { const trimmed = input.trim() const custom = CUSTOM_EMOJI.exec(trimmed) if (custom !== null) return `${custom[1]}:${custom[2]}` const unicode = stripVariation(trimmed) return UNICODE_EMOJI.test(unicode) ? unicode : undefined } /** Reaction key for an event emoji, matching `normalizeEmoji` output. */ export function reactionKey(emoji: { id: string | null; name: string | null }): string | undefined { if (emoji.name === null) return undefined return emoji.id === null ? stripVariation(emoji.name) : `${emoji.name}:${emoji.id}` }