format.ts
· 2.1 KiB · TypeScript
Ham
import { ClassKey, Nation } from "@src/types";
import { getClassEmoji, getNationEmoji, getEmoji } from "@systems/emojis";
// ─── Individual formatters ────────────────────────────────────────────────────
export interface CharDisplayOptions {
emoji?: boolean; // show class emoji (default: true)
level?: boolean; // show level (default: true)
}
/**
* Format a character for display in embeds and messages.
* Output: <:class:> 79 «Flash»
*/
function char(
c: { class: ClassKey; level: number; name: string },
options?: CharDisplayOptions
): string {
const showEmoji = options?.emoji ?? true;
const showLevel = options?.level ?? true;
const classStr = showEmoji ? (getClassEmoji(c.class) || c.class) : c.class;
const levelStr = showLevel ? `${c.level} ` : "";
return `${classStr} ${levelStr}${c.name}`.trim();
}
/**
* Format a nation name with its emoji.
* Output: <:capella:> Capella
*/
function nation(n: Nation): string {
const emoji = getNationEmoji(n);
return emoji ? `${emoji} ${n}` : n;
}
/**
* Format a score line.
* Output: <:score:> 3000 <:kd:> 32/18
*/
function score(pts: number, k?: number, d?: number): string {
const scoreEmoji = getEmoji("score") || "📊";
const kdEmoji = getEmoji("kd") || "⚔️";
const kdStr = k !== undefined && d !== undefined ? ` ${kdEmoji} ${k}/${d}` : "";
return `${scoreEmoji} ${pts}${kdStr}`;
}
/**
* Parse a Discord custom emoji string to an object for ButtonBuilder.setEmoji()
* Input: "<:fb:1511020923510194428>"
* Output: { name: "fb", id: "1511020923510194428" }
*/
function emoji(emojiStr: string): { name: string; id: string } | string | null {
if (!emojiStr) return null;
const match = emojiStr.match(/^<:(\w+):(\d+)>$/);
if (match) return { name: match[1], id: match[2] };
return emojiStr;
}
// ─── Namespace export ─────────────────────────────────────────────────────────
export const format = {
char,
nation,
score,
emoji,
};
| 1 | import { ClassKey, Nation } from "@src/types"; |
| 2 | import { getClassEmoji, getNationEmoji, getEmoji } from "@systems/emojis"; |
| 3 | |
| 4 | // ─── Individual formatters ──────────────────────────────────────────────────── |
| 5 | |
| 6 | export interface CharDisplayOptions { |
| 7 | emoji?: boolean; // show class emoji (default: true) |
| 8 | level?: boolean; // show level (default: true) |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Format a character for display in embeds and messages. |
| 13 | * Output: <:class:> 79 «Flash» |
| 14 | */ |
| 15 | function char( |
| 16 | c: { class: ClassKey; level: number; name: string }, |
| 17 | options?: CharDisplayOptions |
| 18 | ): string { |
| 19 | const showEmoji = options?.emoji ?? true; |
| 20 | const showLevel = options?.level ?? true; |
| 21 | const classStr = showEmoji ? (getClassEmoji(c.class) || c.class) : c.class; |
| 22 | const levelStr = showLevel ? `${c.level} ` : ""; |
| 23 | return `${classStr} ${levelStr}${c.name}`.trim(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Format a nation name with its emoji. |
| 28 | * Output: <:capella:> Capella |
| 29 | */ |
| 30 | function nation(n: Nation): string { |
| 31 | const emoji = getNationEmoji(n); |
| 32 | return emoji ? `${emoji} ${n}` : n; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Format a score line. |
| 37 | * Output: <:score:> 3000 <:kd:> 32/18 |
| 38 | */ |
| 39 | function score(pts: number, k?: number, d?: number): string { |
| 40 | const scoreEmoji = getEmoji("score") || "📊"; |
| 41 | const kdEmoji = getEmoji("kd") || "⚔️"; |
| 42 | const kdStr = k !== undefined && d !== undefined ? ` ${kdEmoji} ${k}/${d}` : ""; |
| 43 | return `${scoreEmoji} ${pts}${kdStr}`; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Parse a Discord custom emoji string to an object for ButtonBuilder.setEmoji() |
| 48 | * Input: "<:fb:1511020923510194428>" |
| 49 | * Output: { name: "fb", id: "1511020923510194428" } |
| 50 | */ |
| 51 | function emoji(emojiStr: string): { name: string; id: string } | string | null { |
| 52 | if (!emojiStr) return null; |
| 53 | const match = emojiStr.match(/^<:(\w+):(\d+)>$/); |
| 54 | if (match) return { name: match[1], id: match[2] }; |
| 55 | return emojiStr; |
| 56 | } |
| 57 | |
| 58 | // ─── Namespace export ───────────────────────────────────────────────────────── |
| 59 | |
| 60 | export const format = { |
| 61 | char, |
| 62 | nation, |
| 63 | score, |
| 64 | emoji, |
| 65 | }; |