emojis.ts
· 804 B · TypeScript
Eredeti
import fs from "fs";
import path from "path";
import { EmojiMap, ClassKey } from "../types";
const EMOJI_PATH = path.join(__dirname, "../../messages/emojis.json");
let _emojis: EmojiMap = {};
export function loadEmojis(): void {
try { _emojis = JSON.parse(fs.readFileSync(EMOJI_PATH, "utf8")); }
catch (err) { console.error("Failed to load emojis.json:", err); _emojis = {}; }
}
export function getEmoji(key: string): string {
return _emojis[key] ?? "";
}
export function getClassEmoji(cls: ClassKey): string {
return getEmoji(cls.toLowerCase());
}
export function getNationEmoji(nation: string): string {
return getEmoji(nation.toLowerCase());
}
export function resolveEmojiTokens(text: string): string {
return text.replace(/\{emoji:([^}]+)\}/g, (_, key: string) => getEmoji(key));
}
| 1 | import fs from "fs"; |
| 2 | import path from "path"; |
| 3 | import { EmojiMap, ClassKey } from "../types"; |
| 4 | |
| 5 | const EMOJI_PATH = path.join(__dirname, "../../messages/emojis.json"); |
| 6 | let _emojis: EmojiMap = {}; |
| 7 | |
| 8 | export function loadEmojis(): void { |
| 9 | try { _emojis = JSON.parse(fs.readFileSync(EMOJI_PATH, "utf8")); } |
| 10 | catch (err) { console.error("Failed to load emojis.json:", err); _emojis = {}; } |
| 11 | } |
| 12 | |
| 13 | export function getEmoji(key: string): string { |
| 14 | return _emojis[key] ?? ""; |
| 15 | } |
| 16 | |
| 17 | export function getClassEmoji(cls: ClassKey): string { |
| 18 | return getEmoji(cls.toLowerCase()); |
| 19 | } |
| 20 | |
| 21 | export function getNationEmoji(nation: string): string { |
| 22 | return getEmoji(nation.toLowerCase()); |
| 23 | } |
| 24 | |
| 25 | export function resolveEmojiTokens(text: string): string { |
| 26 | return text.replace(/\{emoji:([^}]+)\}/g, (_, key: string) => getEmoji(key)); |
| 27 | } |
| 28 |