From 9598273d5b6ee5181c5d535fc0460956f7280ded Mon Sep 17 00:00:00 2001 From: Syxles Date: Thu, 12 Mar 2026 19:44:02 +0100 Subject: [PATCH] refactor(core): replace unicode-emoji-json with native RGI_Emoji regex Use the native /\p{RGI_Emoji}/v regex available since Node 20+ instead of the manual codepoint ranges in cleanPlayerName and the unicode-emoji-json library in discordHelpers. This removes a dependency while improving emoji detection accuracy. --- core/global.d.ts | 8 -------- core/modules/DiscordBot/discordHelpers.ts | 5 ++--- core/package.json | 1 - package-lock.json | 7 ------- shared/cleanPlayerName.ts | 25 ++++------------------- 5 files changed, 6 insertions(+), 40 deletions(-) diff --git a/core/global.d.ts b/core/global.d.ts index 1a9145a07..ec8978fdb 100644 --- a/core/global.d.ts +++ b/core/global.d.ts @@ -52,14 +52,6 @@ declare function ScanResourceRoot(rootPath: string, callback: (data: object) => declare function VerifyPasswordHash(password: string, hash: string): boolean; -/** - * MARK: Fixes - */ -declare module 'unicode-emoji-json/data-ordered-emoji' { - const emojis: string[]; - export = emojis; -} - //FIXME: checar se eu preciso disso // interface ProcessEnv { // [x: string]: string | undefined; diff --git a/core/modules/DiscordBot/discordHelpers.ts b/core/modules/DiscordBot/discordHelpers.ts index bd35451b0..ec933a266 100644 --- a/core/modules/DiscordBot/discordHelpers.ts +++ b/core/modules/DiscordBot/discordHelpers.ts @@ -1,9 +1,8 @@ const modulename = 'DiscordBot:cmd'; -import orderedEmojis from 'unicode-emoji-json/data-ordered-emoji'; import { ColorResolvable, CommandInteraction, EmbedBuilder, InteractionReplyOptions } from "discord.js"; import consoleFactory from '@lib/console'; const console = consoleFactory(modulename); -const allEmojis = new Set(orderedEmojis); +const emojiRegex = /^\p{RGI_Emoji}$/v; @@ -96,7 +95,7 @@ export const isValidButtonEmoji = (emoji: unknown) => { if (typeof emoji !== 'string') return false; if (/^\d{17,19}$/.test(emoji)) return true; if (/^$/.test(emoji)) return true; - return allEmojis.has(emoji); + return emojiRegex.test(emoji); } diff --git a/core/package.json b/core/package.json index 9b872b239..9616004af 100644 --- a/core/package.json +++ b/core/package.json @@ -59,7 +59,6 @@ "string-argv": "^0.3.2", "systeminformation": "^5.23.5", "throttle-debounce": "^5.0.2", - "unicode-emoji-json": "^0.8.0", "xss": "^1.0.15", "zod": "^3.23.8", "zod-validation-error": "^3.4.0" diff --git a/package-lock.json b/package-lock.json index a7cae8e0e..70b76fe21 100644 --- a/package-lock.json +++ b/package-lock.json @@ -85,7 +85,6 @@ "string-argv": "^0.3.2", "systeminformation": "^5.23.5", "throttle-debounce": "^5.0.2", - "unicode-emoji-json": "^0.8.0", "xss": "^1.0.15", "zod": "^3.23.8", "zod-validation-error": "^3.4.0" @@ -14901,12 +14900,6 @@ "integrity": "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==", "license": "MIT" }, - "node_modules/unicode-emoji-json": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/unicode-emoji-json/-/unicode-emoji-json-0.8.0.tgz", - "integrity": "sha512-3wDXXvp6YGoKGhS2O2H7+V+bYduOBydN1lnI0uVfr1cIdY02uFFiEH1i3kE5CCE4l6UqbLKVmEFW9USxTAMD1g==", - "license": "MIT" - }, "node_modules/unicorn-magic": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/unicorn-magic/-/unicorn-magic-0.1.0.tgz", diff --git a/shared/cleanPlayerName.ts b/shared/cleanPlayerName.ts index a82fa80f1..4c13ea8a2 100644 --- a/shared/cleanPlayerName.ts +++ b/shared/cleanPlayerName.ts @@ -60,28 +60,11 @@ const hexInvalidString = (str: string, limit = 35) => { /** - * Checks if a character is an emoji. - * TODO: this is not perfect, use @mathiasbynens/emoji-regex - * or await for NodeJS 20 and use the native regex /^\p{RGI_Emoji}$/v - * ref: https://v8.dev/features/regexp-v-flag -* NOTE: also remove the library unicode-emoji-json, being used by the discord bot + * Checks if a character is an emoji using the native RGI_Emoji property. + * ref: https://v8.dev/features/regexp-v-flag */ -const isEmoji = (char: string) => { - const codePoint = char.codePointAt(0)!; - return ( - (codePoint >= 0x1F300 && codePoint <= 0x1F5FF) || // Miscellaneous Symbols and Pictographs - (codePoint >= 0x1F600 && codePoint <= 0x1F64F) || // Emoticons - (codePoint >= 0x1F680 && codePoint <= 0x1F6FF) || // Transport and Map Symbols - (codePoint >= 0x1F700 && codePoint <= 0x1F77F) || // Alchemical Symbols - (codePoint >= 0x1F780 && codePoint <= 0x1F7FF) || // Geometric Shapes Extended - (codePoint >= 0x1F800 && codePoint <= 0x1F8FF) || // Supplemental Arrows-C - (codePoint >= 0x1F900 && codePoint <= 0x1F9FF) || // Supplemental Symbols and Pictographs - (codePoint >= 0x1FA00 && codePoint <= 0x1FA6F) || // Chess Symbols - (codePoint >= 0x1FA70 && codePoint <= 0x1FAFF) || // Symbols and Pictographs Extended-A - (codePoint >= 0x2600 && codePoint <= 0x26FF) || // Miscellaneous Symbols - (codePoint >= 0x2700 && codePoint <= 0x27BF) // Dingbats - ); -} +const emojiRegex = /^\p{RGI_Emoji}$/v; +const isEmoji = (char: string) => emojiRegex.test(char); /**