blob: 9df17a4b32dc309a1265b26580e17c920ed59846 [file] [log] [blame]
Skyler Grey75ea9172022-08-06 10:22:23 +01001import emojis from "../config/emojis.json" assert { type: "json" };
Samuel Shuert27bf3cd2023-03-03 15:51:25 -05002import lodash from 'lodash';
pineafan4f164f32022-02-26 22:07:12 +00003
Samuel Shuert27bf3cd2023-03-03 15:51:25 -05004const isArray = lodash.isArray;
pineafanbd02b4a2022-08-05 22:01:38 +01005interface EmojisIndex {
6 [key: string]: string | EmojisIndex | EmojisIndex[];
7}
8
PineaFandf4996f2023-01-01 14:20:06 +00009function getEmojiByName(name: string | null, format?: string): string {
10 if (!name) return "";
Skyler Grey75ea9172022-08-06 10:22:23 +010011 const parts = name.split(".");
pineafanbd02b4a2022-08-05 22:01:38 +010012 let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis;
Skyler Grey75ea9172022-08-06 10:22:23 +010013 for (const part of parts) {
pineafanbd02b4a2022-08-05 22:01:38 +010014 if (typeof id === "string" || id === undefined) {
15 throw new Error(`Emoji ${name} not found`);
16 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050017 if (isArray(id)) {
pineafanbd02b4a2022-08-05 22:01:38 +010018 id = id[parseInt(part)];
19 } else {
20 id = id[part];
21 }
Skyler Grey75ea9172022-08-06 10:22:23 +010022 }
23 if (typeof id !== "string" && id !== undefined) {
24 throw new Error(`Emoji ${name} not found`);
25 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050026 return getEmojiFromId(id, format);
27}
28
29function getEmojiFromId(id: string | undefined, format?: string): string {
Skyler Grey75ea9172022-08-06 10:22:23 +010030 if (format === "id") {
pineafan8b4b17f2022-02-27 20:42:52 +000031 if (id === undefined) return "0";
32 return id.toString();
33 }
34 if (id === undefined) {
PineaFandf4996f2023-01-01 14:20:06 +000035 return "";
pineafan8b4b17f2022-02-27 20:42:52 +000036 } else if (id.toString().startsWith("a")) {
pineafan63fc5e22022-08-04 22:04:10 +010037 return `<a:_:${id.toString().slice(1, id.toString().length)}>`;
pineafan8b4b17f2022-02-27 20:42:52 +000038 }
pineafan377794f2022-04-18 19:01:01 +010039 return `<:_:${id}>`;
pineafan4f164f32022-02-26 22:07:12 +000040}
41
42export default getEmojiByName;