nations.ts
· 1.9 KiB · TypeScript
Ham
import { GuildMember } from "discord.js";
import { Nation } from "../types";
import { getActiveCharacter } from "./characters";
export const NATION_UNICODE: Record<string, string> = {
Capella: "🔴",
Procyon: "🔵",
};
export const NATION_KEY: Record<Nation, "capella" | "procyon"> = {
[Nation.Capella]: "capella",
[Nation.Procyon]: "procyon",
};
export const NATION_FROM_KEY: Record<"capella" | "procyon", Nation> = {
"capella": Nation.Capella,
"procyon": Nation.Procyon,
};
export function nationKey(nation: Nation): "capella" | "procyon" {
return NATION_KEY[nation];
}
// Resolve a user's nation — character nation takes priority over Discord role
export function resolveNation(member: GuildMember, userKey: string | null): Nation | null {
// 1. Active character's nation
if (userKey) {
const char = getActiveCharacter(userKey);
if (char) return char.nation;
}
// 2. Discord role fallback
if (member.roles.cache.some((r) => r.name === Nation.Capella)) return Nation.Capella;
if (member.roles.cache.some((r) => r.name === Nation.Procyon)) return Nation.Procyon;
return null;
}
export function oppositeNation(nation: Nation): Nation {
return nation === Nation.Capella ? Nation.Procyon : Nation.Capella;
}
export const Nations = {
key(nation: Nation): "capella" | "procyon" {
return NATION_KEY[nation];
},
resolve(member: GuildMember, userKey: string|null): Nation | null {
// 1. Active character's nation
if (userKey) {
const char = getActiveCharacter(userKey);
if (char) return char.nation;
}
// 2. Discord role fallback
if (member.roles.cache.some((r) => r.name === Nation.Capella)) return Nation.Capella;
if (member.roles.cache.some((r) => r.name === Nation.Procyon)) return Nation.Procyon;
return null;
},
opposite(nation: Nation): Nation {
return nation === Nation.Capella ? Nation.Procyon : Nation.Capella;
},
}
| 1 | import { GuildMember } from "discord.js"; |
| 2 | import { Nation } from "../types"; |
| 3 | import { getActiveCharacter } from "./characters"; |
| 4 | |
| 5 | export const NATION_UNICODE: Record<string, string> = { |
| 6 | Capella: "🔴", |
| 7 | Procyon: "🔵", |
| 8 | }; |
| 9 | |
| 10 | export const NATION_KEY: Record<Nation, "capella" | "procyon"> = { |
| 11 | [Nation.Capella]: "capella", |
| 12 | [Nation.Procyon]: "procyon", |
| 13 | }; |
| 14 | |
| 15 | export const NATION_FROM_KEY: Record<"capella" | "procyon", Nation> = { |
| 16 | "capella": Nation.Capella, |
| 17 | "procyon": Nation.Procyon, |
| 18 | }; |
| 19 | |
| 20 | export 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 |
| 25 | export 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 | |
| 39 | export function oppositeNation(nation: Nation): Nation { |
| 40 | return nation === Nation.Capella ? Nation.Procyon : Nation.Capella; |
| 41 | } |
| 42 | |
| 43 | export 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 | } |