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