blob: 7824982481900b8d965cf8d02498312a6f358963 [file] [log] [blame]
PineaFan100df682023-01-02 13:26:08 +00001// @ts-expect-error
Skyler Grey75ea9172022-08-06 10:22:23 +01002import emojis from "../config/emojis.json" assert { type: "json" };
pineafan4f164f32022-02-26 22:07:12 +00003
pineafanbd02b4a2022-08-05 22:01:38 +01004interface EmojisIndex {
5 [key: string]: string | EmojisIndex | EmojisIndex[];
6}
7
PineaFandf4996f2023-01-01 14:20:06 +00008function getEmojiByName(name: string | null, format?: string): string {
9 if (!name) return "";
Skyler Grey75ea9172022-08-06 10:22:23 +010010 const parts = name.split(".");
pineafanbd02b4a2022-08-05 22:01:38 +010011 let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis;
Skyler Grey75ea9172022-08-06 10:22:23 +010012 for (const part of parts) {
pineafanbd02b4a2022-08-05 22:01:38 +010013 if (typeof id === "string" || id === undefined) {
14 throw new Error(`Emoji ${name} not found`);
15 }
16 if (Array.isArray(id)) {
17 id = id[parseInt(part)];
18 } else {
19 id = id[part];
20 }
Skyler Grey75ea9172022-08-06 10:22:23 +010021 }
22 if (typeof id !== "string" && id !== undefined) {
23 throw new Error(`Emoji ${name} not found`);
24 }
25 if (format === "id") {
pineafan8b4b17f2022-02-27 20:42:52 +000026 if (id === undefined) return "0";
27 return id.toString();
28 }
29 if (id === undefined) {
PineaFandf4996f2023-01-01 14:20:06 +000030 return "";
pineafan8b4b17f2022-02-27 20:42:52 +000031 } else if (id.toString().startsWith("a")) {
pineafan63fc5e22022-08-04 22:04:10 +010032 return `<a:_:${id.toString().slice(1, id.toString().length)}>`;
pineafan8b4b17f2022-02-27 20:42:52 +000033 }
pineafan377794f2022-04-18 19:01:01 +010034 return `<:_:${id}>`;
pineafan4f164f32022-02-26 22:07:12 +000035}
36
37export default getEmojiByName;