blob: 19b88b4d5d87fe266ae9cfd15753ed7ad066e897 [file] [log] [blame]
TheCodedProfa38cbb32023-03-11 17:22:25 -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
21
22function getEmojiByName(name: typeof EMOJIPATHS[number], format?: string): string {
Skyler Grey75ea9172022-08-06 10:22:23 +010023 const parts = name.split(".");
pineafanbd02b4a2022-08-05 22:01:38 +010024 let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis;
Skyler Grey75ea9172022-08-06 10:22:23 +010025 for (const part of parts) {
pineafanbd02b4a2022-08-05 22:01:38 +010026 if (typeof id === "string" || id === undefined) {
27 throw new Error(`Emoji ${name} not found`);
28 }
TheCodedProf764e6c22023-03-11 16:07:09 -050029 if (_.isArray(id)) {
pineafanbd02b4a2022-08-05 22:01:38 +010030 id = id[parseInt(part)];
31 } else {
32 id = id[part];
33 }
Skyler Grey75ea9172022-08-06 10:22:23 +010034 }
35 if (typeof id !== "string" && id !== undefined) {
36 throw new Error(`Emoji ${name} not found`);
37 }
TheCodedProf14d11f62023-01-28 20:07:50 -050038 return getEmojiFromId(id, format);
39}
40
41function getEmojiFromId(id: string | undefined, format?: string): string {
Skyler Grey75ea9172022-08-06 10:22:23 +010042 if (format === "id") {
pineafan8b4b17f2022-02-27 20:42:52 +000043 if (id === undefined) return "0";
44 return id.toString();
45 }
46 if (id === undefined) {
PineaFandf4996f2023-01-01 14:20:06 +000047 return "";
pineafan8b4b17f2022-02-27 20:42:52 +000048 } else if (id.toString().startsWith("a")) {
TheCodedProf764e6c22023-03-11 16:07:09 -050049 return `<a:N:${id.toString().slice(1, id.toString().length)}>`;
pineafan8b4b17f2022-02-27 20:42:52 +000050 }
TheCodedProf764e6c22023-03-11 16:07:09 -050051 return `<:N:${id}>`;
pineafan4f164f32022-02-26 22:07:12 +000052}
53
54export default getEmojiByName;