blob: ebcb2571dba779a7e314597b2b8d07ce3fe2e801 [file] [log] [blame]
TheCodedProf528de572023-03-11 17:28:29 -05001import emojis from "../config/emojis.js";
TheCodedProf764e6c22023-03-11 16:07:09 -05002import _ from "lodash";
pineafan4f164f32022-02-26 22:07:12 +00003
pineafanbd02b4a2022-08-05 22:01:38 +01004interface EmojisIndex {
5 [key: string]: string | EmojisIndex | EmojisIndex[];
6}
7
TheCodedProfa38cbb32023-03-11 17:22:25 -05008const EMOJIPATHS: string[] = [];
9
10function 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}
19getEmojiPaths(emojis);
20
TheCodedProf528de572023-03-11 17:28:29 -050021function getEmojiByName(name: (typeof EMOJIPATHS)[number], format?: string): string {
Skyler Grey75ea9172022-08-06 10:22:23 +010022 const parts = name.split(".");
pineafanbd02b4a2022-08-05 22:01:38 +010023 let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis;
Skyler Grey75ea9172022-08-06 10:22:23 +010024 for (const part of parts) {
pineafanbd02b4a2022-08-05 22:01:38 +010025 if (typeof id === "string" || id === undefined) {
26 throw new Error(`Emoji ${name} not found`);
27 }
TheCodedProf764e6c22023-03-11 16:07:09 -050028 if (_.isArray(id)) {
pineafanbd02b4a2022-08-05 22:01:38 +010029 id = id[parseInt(part)];
30 } else {
31 id = id[part];
32 }
Skyler Grey75ea9172022-08-06 10:22:23 +010033 }
34 if (typeof id !== "string" && id !== undefined) {
35 throw new Error(`Emoji ${name} not found`);
36 }
TheCodedProf14d11f62023-01-28 20:07:50 -050037 return getEmojiFromId(id, format);
38}
39
40function getEmojiFromId(id: string | undefined, format?: string): string {
Skyler Grey75ea9172022-08-06 10:22:23 +010041 if (format === "id") {
pineafan8b4b17f2022-02-27 20:42:52 +000042 if (id === undefined) return "0";
43 return id.toString();
44 }
45 if (id === undefined) {
PineaFandf4996f2023-01-01 14:20:06 +000046 return "";
pineafan8b4b17f2022-02-27 20:42:52 +000047 } else if (id.toString().startsWith("a")) {
TheCodedProf764e6c22023-03-11 16:07:09 -050048 return `<a:N:${id.toString().slice(1, id.toString().length)}>`;
pineafan8b4b17f2022-02-27 20:42:52 +000049 }
TheCodedProf764e6c22023-03-11 16:07:09 -050050 return `<:N:${id}>`;
pineafan4f164f32022-02-26 22:07:12 +000051}
52
53export default getEmojiByName;