Zuletzt aktiv 1 month ago

nuno hat die Gist bearbeitet 1 month ago. Zu Änderung gehen

1 file changed, 65 insertions

format.ts(Datei erstellt)

@@ -0,0 +1,65 @@
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 + };
Neuer Älter