TheCodedProf | 528de57 | 2023-03-11 17:28:29 -0500 | [diff] [blame] | 1 | import emojis from "../config/emojis.js"; |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 2 | import _ from "lodash"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 3 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 4 | interface EmojisIndex { |
| 5 | [key: string]: string | EmojisIndex | EmojisIndex[]; |
| 6 | } |
| 7 | |
TheCodedProf | a38cbb3 | 2023-03-11 17:22:25 -0500 | [diff] [blame] | 8 | const EMOJIPATHS: string[] = []; |
| 9 | |
| 10 | function getEmojiPaths(obj: EmojisIndex, path: string[] = []) { |
| 11 | for (const key in obj) { |
| 12 | if (typeof obj[key] === "string") { |
| 13 | EMOJIPATHS.push([...path, key].join(".")); |
| 14 | } else { |
| 15 | getEmojiPaths(obj[key] as EmojisIndex, [...path, key]); |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | getEmojiPaths(emojis); |
| 20 | |
Skyler Grey | 6a0bab5 | 2023-03-15 00:10:26 +0000 | [diff] [blame] | 21 | function getEmojiByName(name: typeof EMOJIPATHS[number], format?: string): string { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 22 | const parts = name.split("."); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 23 | let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | for (const part of parts) { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 25 | if (typeof id === "string" || id === undefined) { |
| 26 | throw new Error(`Emoji ${name} not found`); |
| 27 | } |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 28 | if (_.isArray(id)) { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 29 | id = id[parseInt(part)]; |
| 30 | } else { |
| 31 | id = id[part]; |
| 32 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 33 | } |
| 34 | if (typeof id !== "string" && id !== undefined) { |
| 35 | throw new Error(`Emoji ${name} not found`); |
| 36 | } |
TheCodedProf | 14d11f6 | 2023-01-28 20:07:50 -0500 | [diff] [blame] | 37 | return getEmojiFromId(id, format); |
| 38 | } |
| 39 | |
| 40 | function getEmojiFromId(id: string | undefined, format?: string): string { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 41 | if (format === "id") { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 42 | if (id === undefined) return "0"; |
| 43 | return id.toString(); |
| 44 | } |
| 45 | if (id === undefined) { |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 46 | return ""; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 47 | } else if (id.toString().startsWith("a")) { |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 48 | return `<a:N:${id.toString().slice(1, id.toString().length)}>`; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 49 | } |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 50 | return `<:N:${id}>`; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | export default getEmojiByName; |