blob: 3fa2b53463a2a0268672d5aef62cd7baf799df91 [file] [log] [blame]
Skyler Grey75ea9172022-08-06 10:22:23 +01001import emojis from "../config/emojis.json" assert { type: "json" };
pineafan4f164f32022-02-26 22:07:12 +00002
pineafanbd02b4a2022-08-05 22:01:38 +01003interface EmojisIndex {
4 [key: string]: string | EmojisIndex | EmojisIndex[];
5}
6
PineaFandf4996f2023-01-01 14:20:06 +00007function getEmojiByName(name: string | null, format?: string): string {
8 if (!name) return "";
Skyler Grey75ea9172022-08-06 10:22:23 +01009 const parts = name.split(".");
pineafanbd02b4a2022-08-05 22:01:38 +010010 let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis;
Skyler Grey75ea9172022-08-06 10:22:23 +010011 for (const part of parts) {
pineafanbd02b4a2022-08-05 22:01:38 +010012 if (typeof id === "string" || id === undefined) {
13 throw new Error(`Emoji ${name} not found`);
14 }
15 if (Array.isArray(id)) {
16 id = id[parseInt(part)];
17 } else {
18 id = id[part];
19 }
Skyler Grey75ea9172022-08-06 10:22:23 +010020 }
21 if (typeof id !== "string" && id !== undefined) {
22 throw new Error(`Emoji ${name} not found`);
23 }
24 if (format === "id") {
pineafan8b4b17f2022-02-27 20:42:52 +000025 if (id === undefined) return "0";
26 return id.toString();
27 }
28 if (id === undefined) {
PineaFandf4996f2023-01-01 14:20:06 +000029 return "";
pineafan8b4b17f2022-02-27 20:42:52 +000030 } else if (id.toString().startsWith("a")) {
pineafan63fc5e22022-08-04 22:04:10 +010031 return `<a:_:${id.toString().slice(1, id.toString().length)}>`;
pineafan8b4b17f2022-02-27 20:42:52 +000032 }
pineafan377794f2022-04-18 19:01:01 +010033 return `<:_:${id}>`;
pineafan4f164f32022-02-26 22:07:12 +000034}
35
36export default getEmojiByName;