pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import emojis from "../config/emojis.json" assert {type: "json"}; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 3 | interface EmojisIndex { |
| 4 | [key: string]: string | EmojisIndex | EmojisIndex[]; |
| 5 | } |
| 6 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 7 | function getEmojiByName(name: string, format?: string): string { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 8 | const split = name.split("."); |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 9 | let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 10 | split.forEach(part => { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 11 | if (typeof id === "string" || id === undefined) { |
| 12 | throw new Error(`Emoji ${name} not found`); |
| 13 | } |
| 14 | if (Array.isArray(id)) { |
| 15 | id = id[parseInt(part)]; |
| 16 | } else { |
| 17 | id = id[part]; |
| 18 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 19 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 20 | if ( format === "id" ) { |
| 21 | if (id === undefined) return "0"; |
| 22 | return id.toString(); |
| 23 | } |
| 24 | if (id === undefined) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | return "<a:_:946346549271732234>"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 26 | } else if (id.toString().startsWith("a")) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | return `<a:_:${id.toString().slice(1, id.toString().length)}>`; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 28 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 29 | return `<:_:${id}>`; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | export default getEmojiByName; |