blob: 9bbf61fae88437288d998a875c9aa55d0d9105ab [file] [log] [blame]
Skyler Grey75ea9172022-08-06 10:22:23 +01001import emojis from "../config/emojis.json" assert { type: "json" };
TheCodedProf764e6c22023-03-11 16:07:09 -05002import _ from "lodash";
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 }
TheCodedProf764e6c22023-03-11 16:07:09 -050016 if (_.isArray(id)) {
pineafanbd02b4a2022-08-05 22:01:38 +010017 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 }
TheCodedProf14d11f62023-01-28 20:07:50 -050025 return getEmojiFromId(id, format);
26}
27
28function getEmojiFromId(id: string | undefined, format?: string): string {
Skyler Grey75ea9172022-08-06 10:22:23 +010029 if (format === "id") {
pineafan8b4b17f2022-02-27 20:42:52 +000030 if (id === undefined) return "0";
31 return id.toString();
32 }
33 if (id === undefined) {
PineaFandf4996f2023-01-01 14:20:06 +000034 return "";
pineafan8b4b17f2022-02-27 20:42:52 +000035 } else if (id.toString().startsWith("a")) {
TheCodedProf764e6c22023-03-11 16:07:09 -050036 return `<a:N:${id.toString().slice(1, id.toString().length)}>`;
pineafan8b4b17f2022-02-27 20:42:52 +000037 }
TheCodedProf764e6c22023-03-11 16:07:09 -050038 return `<:N:${id}>`;
pineafan4f164f32022-02-26 22:07:12 +000039}
40
41export default getEmojiByName;