nuno hat die Gist bearbeitet 3 weeks ago. Zu Änderung gehen
1 file changed, 65 insertions
nations.ts(Datei erstellt)
| @@ -0,0 +1,65 @@ | |||
| 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 | + | } | |
Neuer
Älter