char-slop/city-role-bot

discord auto role applicator bot for charlotte polycule city

git clone https://git.t4t.associates/char-slop/city-role-bot

Charlotte Sominitial commitdae4431

main
1.1 KiB23 linesraw
1const CUSTOM_EMOJI = /^<a?:([^:]+):(\d+)>$/
2const UNICODE_EMOJI =
3  /^\p{Extended_Pictographic}[\u{1F3FB}-\u{1F3FF}]?(?:\u200D\p{Extended_Pictographic}[\u{1F3FB}-\u{1F3FF}]?)*$/u
4
5// discord sends some unicode emoji with and some without a variation
6// selector, so we strip it on both sides to keep comparisons stable
7const stripVariation = (emoji: string): string => emoji.replace(/\uFE0F/g, '')
8
9/** Normalizes a unicode emoji or a `<a:name:id>` mention to the `name:id` /
10 *  unicode form the reactions API expects. Returns undefined for anything else. */
11export function normalizeEmoji(input: string): string | undefined {
12  const trimmed = input.trim()
13  const custom = CUSTOM_EMOJI.exec(trimmed)
14  if (custom !== null) return `${custom[1]}:${custom[2]}`
15  const unicode = stripVariation(trimmed)
16  return UNICODE_EMOJI.test(unicode) ? unicode : undefined
17}
18
19/** Reaction key for an event emoji, matching `normalizeEmoji` output. */
20export function reactionKey(emoji: { id: string | null; name: string | null }): string | undefined {
21  if (emoji.name === null) return undefined
22  return emoji.id === null ? stripVariation(emoji.name) : `${emoji.name}:${emoji.id}`
23}