| import emojis from "../config/emojis.js" |
| import _ from "lodash"; |
| |
| interface EmojisIndex { |
| [key: string]: string | EmojisIndex | EmojisIndex[]; |
| } |
| |
| const EMOJIPATHS: string[] = []; |
| |
| function getEmojiPaths(obj: EmojisIndex, path: string[] = []) { |
| for (const key in obj) { |
| if (typeof obj[key] === "string") { |
| EMOJIPATHS.push([...path, key].join(".")); |
| } else { |
| getEmojiPaths(obj[key] as EmojisIndex, [...path, key]); |
| } |
| } |
| } |
| getEmojiPaths(emojis); |
| |
| |
| function getEmojiByName(name: typeof EMOJIPATHS[number], format?: string): string { |
| const parts = name.split("."); |
| let id: string | EmojisIndex | EmojisIndex[] | undefined = emojis; |
| for (const part of parts) { |
| if (typeof id === "string" || id === undefined) { |
| throw new Error(`Emoji ${name} not found`); |
| } |
| if (_.isArray(id)) { |
| id = id[parseInt(part)]; |
| } else { |
| id = id[part]; |
| } |
| } |
| if (typeof id !== "string" && id !== undefined) { |
| throw new Error(`Emoji ${name} not found`); |
| } |
| return getEmojiFromId(id, format); |
| } |
| |
| function getEmojiFromId(id: string | undefined, format?: string): string { |
| if (format === "id") { |
| if (id === undefined) return "0"; |
| return id.toString(); |
| } |
| if (id === undefined) { |
| return ""; |
| } else if (id.toString().startsWith("a")) { |
| return `<a:N:${id.toString().slice(1, id.toString().length)}>`; |
| } |
| return `<:N:${id}>`; |
| } |
| |
| export default getEmojiByName; |