blob: f953a4f04f148751c5eca4f309ac8ab1b013c1bf [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
pineafan8b4b17f2022-02-27 20:42:52 +00007function getEmojiByName(name: string, format?: string): string {
Skyler Grey75ea9172022-08-06 10:22:23 +01008 const parts = name.split(".");
pineafanbd02b4a2022-08-05 22:01:38 +01009 let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis;
Skyler Grey75ea9172022-08-06 10:22:23 +010010 for (const part of parts) {
pineafanbd02b4a2022-08-05 22:01:38 +010011 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 }
Skyler Grey75ea9172022-08-06 10:22:23 +010019 }
20 if (typeof id !== "string" && id !== undefined) {
21 throw new Error(`Emoji ${name} not found`);
22 }
23 if (format === "id") {
pineafan8b4b17f2022-02-27 20:42:52 +000024 if (id === undefined) return "0";
25 return id.toString();
26 }
27 if (id === undefined) {
pineafan63fc5e22022-08-04 22:04:10 +010028 return "<a:_:946346549271732234>";
pineafan8b4b17f2022-02-27 20:42:52 +000029 } else if (id.toString().startsWith("a")) {
pineafan63fc5e22022-08-04 22:04:10 +010030 return `<a:_:${id.toString().slice(1, id.toString().length)}>`;
pineafan8b4b17f2022-02-27 20:42:52 +000031 }
pineafan377794f2022-04-18 19:01:01 +010032 return `<:_:${id}>`;
pineafan4f164f32022-02-26 22:07:12 +000033}
34
35export default getEmojiByName;