Ultima attività 3 weeks ago

nations.ts Raw
1import { GuildMember } from "discord.js";
2import { Nation } from "../types";
3import { getActiveCharacter } from "./characters";
4
5export const NATION_UNICODE: Record<string, string> = {
6 Capella: "🔴",
7 Procyon: "🔵",
8};
9
10export const NATION_KEY: Record<Nation, "capella" | "procyon"> = {
11 [Nation.Capella]: "capella",
12 [Nation.Procyon]: "procyon",
13};
14
15export const NATION_FROM_KEY: Record<"capella" | "procyon", Nation> = {
16 "capella": Nation.Capella,
17 "procyon": Nation.Procyon,
18};
19
20export function nationKey(nation: Nation): "capella" | "procyon" {
21 return NATION_KEY[nation];
22}
23
24// Resolve a user's nation — character nation takes priority over Discord role
25export function resolveNation(member: GuildMember, userKey: string | null): Nation | null {
26 // 1. Active character's nation
27 if (userKey) {
28 const char = getActiveCharacter(userKey);
29 if (char) return char.nation;
30 }
31
32 // 2. Discord role fallback
33 if (member.roles.cache.some((r) => r.name === Nation.Capella)) return Nation.Capella;
34 if (member.roles.cache.some((r) => r.name === Nation.Procyon)) return Nation.Procyon;
35
36 return null;
37}
38
39export function oppositeNation(nation: Nation): Nation {
40 return nation === Nation.Capella ? Nation.Procyon : Nation.Capella;
41}
42
43export const Nations = {
44 key(nation: Nation): "capella" | "procyon" {
45 return NATION_KEY[nation];
46 },
47
48 resolve(member: GuildMember, userKey: string|null): Nation | null {
49 // 1. Active character's nation
50 if (userKey) {
51 const char = getActiveCharacter(userKey);
52 if (char) return char.nation;
53 }
54
55 // 2. Discord role fallback
56 if (member.roles.cache.some((r) => r.name === Nation.Capella)) return Nation.Capella;
57 if (member.roles.cache.some((r) => r.name === Nation.Procyon)) return Nation.Procyon;
58
59 return null;
60 },
61
62 opposite(nation: Nation): Nation {
63 return nation === Nation.Capella ? Nation.Procyon : Nation.Capella;
64 },
65}