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