blob: c0a43cba65710f3d92aedb8a78e031f7286e38f0 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +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 {
pineafan63fc5e22022-08-04 22:04:10 +01008 const split = name.split(".");
pineafanbd02b4a2022-08-05 22:01:38 +01009 let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis;
pineafan4f164f32022-02-26 22:07:12 +000010 split.forEach(part => {
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 }
pineafan4f164f32022-02-26 22:07:12 +000019 });
pineafan8b4b17f2022-02-27 20:42:52 +000020 if ( format === "id" ) {
21 if (id === undefined) return "0";
22 return id.toString();
23 }
24 if (id === undefined) {
pineafan63fc5e22022-08-04 22:04:10 +010025 return "<a:_:946346549271732234>";
pineafan8b4b17f2022-02-27 20:42:52 +000026 } else if (id.toString().startsWith("a")) {
pineafan63fc5e22022-08-04 22:04:10 +010027 return `<a:_:${id.toString().slice(1, id.toString().length)}>`;
pineafan8b4b17f2022-02-27 20:42:52 +000028 }
pineafan377794f2022-04-18 19:01:01 +010029 return `<:_:${id}>`;
pineafan4f164f32022-02-26 22:07:12 +000030}
31
32export default getEmojiByName;