switch.ts
· 6.0 KiB · TypeScript
Surowy
import { ChatInputCommandInteraction, TextChannel } from "discord.js";
import { cfg } from "@systems/config";
import { resolveUser, hasOfficerRole } from "@systems/users";
import { setActiveCharacter, getActiveCharacter, getCharacterByName, getCharacters } from "@systems/characters";
import { getEffectiveCharacter } from "@systems/borrow";
import { setSessionBorrow, getSessionBorrow, setPersistentPreference, clearPersistentPreference } from "@systems/borrow";
import { polls, updatePollMessage } from "@systems/poll";
import { getClassEmoji } from "@systems/emojis";
import { replyAndDelete } from "@src/utils";
import { format } from "@format";
import fs from "fs";
import path from "path";
const CHARS_PATH = path.join(__dirname, "../../data/characters.json");
function findSharedChar(userKey: string, charName: string): { ownerKey: string; char: any } | null {
try {
const chars = JSON.parse(fs.readFileSync(CHARS_PATH, "utf8"));
for (const [ownerKey, data] of Object.entries(chars) as [string, any][]) {
if (ownerKey === userKey) continue;
const char = data.characters?.find(
(c: any) => c.name.toLowerCase() === charName.toLowerCase() && c.sharedWith?.includes(userKey)
);
if (char) return { ownerKey, char };
}
} catch {}
return null;
}
// Reverse-lookup: find Discord userId for a userKey from current poll voters
function findUserIdInPoll(state: any, userKey: string): string | null {
for (const [id, entry] of [...state.yes.entries(), ...state.no.entries()]) {
if (entry.userKey === userKey) return id;
}
return null;
}
export async function handleSwitch(interaction: ChatInputCommandInteraction): Promise<void> {
const member = await interaction.guild!.members.fetch(interaction.user.id);
const isOfficer = hasOfficerRole(member, cfg("officerRoles"));
const nameArg = interaction.options.getString("name");
const charName = interaction.options.getString("char_name", true);
let userKey: string | null;
if (nameArg) {
if (!isOfficer) return void replyAndDelete(interaction, "❌ Only officers can switch other players' characters.");
userKey = nameArg;
} else {
const user = await resolveUser(member);
userKey = user.userKey;
}
if (!userKey) return void replyAndDelete(interaction, "❌ You are not registered in the system.");
let resolvedChar: any = null;
let borrowedFrom: string | null = null;
// Try own characters first
const set = setActiveCharacter(userKey, charName);
if (set) {
clearPersistentPreference(userKey);
resolvedChar = getActiveCharacter(userKey);
} else {
// Fall back to shared characters
const shared = findSharedChar(userKey, charName);
if (shared) {
setSessionBorrow(userKey, shared.ownerKey, shared.char.name);
setPersistentPreference(userKey, shared.ownerKey, shared.char.name);
resolvedChar = shared.char;
borrowedFrom = shared.ownerKey;
}
}
if (!resolvedChar) return void replyAndDelete(interaction, `❌ No character named **${charName}** found.`);
const currentActive = getEffectiveCharacter(userKey);
if (currentActive.char?.name === resolvedChar.name) {
// Already active — just show current state
const classEmoji = getClassEmoji(resolvedChar.class) || resolvedChar.class;
const borrowNote = currentActive.borrowedFrom ? ` *(shared by ${currentActive.borrowedFrom})*` : "";
return void replyAndDelete(interaction, `${classEmoji} ${resolvedChar.level} ${resolvedChar.name}${borrowNote}`, true);
}
// Check if target character is already in the active poll by another player
const slot = [...polls.keys()][0];
if (slot !== undefined) {
const state = polls.get(slot)!;
for (const [id, entry] of state.yes.entries()) {
const isOwnEntry = id === (nameArg ? `injected:${userKey}` : interaction.user.id) ||
id === `impersonated:${userKey}`;
if (!isOwnEntry && entry.characterName === resolvedChar.name && entry.userKey !== userKey) {
// Character is taken — check if we're the owner
const isOwner = getCharacters(userKey).some((c) => c.name === resolvedChar!.name);
if (isOwner) {
// Show conflict embed — need a button interaction for this, so just error with instructions
const slotHour = slot !== undefined ? polls.get(slot!)?.slot : cfg("slots")[0]?.tgHour ?? 20;
const charDisplay = format.char(resolvedChar);
return void replyAndDelete(interaction,
`⚠️ ${charDisplay} is already scheduled for TG at ${slotHour}:00. Use the poll buttons to reclaim it.`,
true
);
}
const slotHour = slot !== undefined ? polls.get(slot)?.slot : cfg("slots")[0]?.tgHour ?? 20;
return void replyAndDelete(interaction,
`❌ ${format.char(resolvedChar)} is already scheduled for TG at ${slotHour}:00. Pick a different character.`,
true
);
}
}
}
// Update poll embed if user has voted
if (slot !== undefined) {
const state = polls.get(slot)!;
const userId = nameArg
? findUserIdInPoll(state, userKey)
: interaction.user.id;
if (userId && (state.yes.has(userId) || state.no.has(userId))) {
const updateEntry = (map: Map<string, any>) => {
const entry = map.get(userId);
if (entry) {
entry.characterName = resolvedChar.name;
entry.characterClass = resolvedChar.class;
entry.characterLevel = resolvedChar.level;
entry.characterNation = resolvedChar.nation;
entry.borrowedFrom = borrowedFrom ?? undefined;
}
};
updateEntry(state.yes);
updateEntry(state.no);
const channel = await interaction.client.channels.fetch(cfg("pollChannelId")) as TextChannel;
await updatePollMessage(channel, slot);
}
}
const classEmoji = getClassEmoji(resolvedChar.class) || resolvedChar.class;
const borrowNote = borrowedFrom ? ` *(shared by ${borrowedFrom})*` : "";
return void replyAndDelete(interaction, `🔄 ${classEmoji} ${resolvedChar.level} ${resolvedChar.name}${borrowNote}`, true);
}
| 1 | import { ChatInputCommandInteraction, TextChannel } from "discord.js"; |
| 2 | import { cfg } from "@systems/config"; |
| 3 | import { resolveUser, hasOfficerRole } from "@systems/users"; |
| 4 | import { setActiveCharacter, getActiveCharacter, getCharacterByName, getCharacters } from "@systems/characters"; |
| 5 | import { getEffectiveCharacter } from "@systems/borrow"; |
| 6 | import { setSessionBorrow, getSessionBorrow, setPersistentPreference, clearPersistentPreference } from "@systems/borrow"; |
| 7 | import { polls, updatePollMessage } from "@systems/poll"; |
| 8 | import { getClassEmoji } from "@systems/emojis"; |
| 9 | import { replyAndDelete } from "@src/utils"; |
| 10 | import { format } from "@format"; |
| 11 | |
| 12 | import fs from "fs"; |
| 13 | import path from "path"; |
| 14 | |
| 15 | const CHARS_PATH = path.join(__dirname, "../../data/characters.json"); |
| 16 | |
| 17 | function findSharedChar(userKey: string, charName: string): { ownerKey: string; char: any } | null { |
| 18 | try { |
| 19 | const chars = JSON.parse(fs.readFileSync(CHARS_PATH, "utf8")); |
| 20 | for (const [ownerKey, data] of Object.entries(chars) as [string, any][]) { |
| 21 | if (ownerKey === userKey) continue; |
| 22 | const char = data.characters?.find( |
| 23 | (c: any) => c.name.toLowerCase() === charName.toLowerCase() && c.sharedWith?.includes(userKey) |
| 24 | ); |
| 25 | if (char) return { ownerKey, char }; |
| 26 | } |
| 27 | } catch {} |
| 28 | return null; |
| 29 | } |
| 30 | |
| 31 | // Reverse-lookup: find Discord userId for a userKey from current poll voters |
| 32 | function findUserIdInPoll(state: any, userKey: string): string | null { |
| 33 | for (const [id, entry] of [...state.yes.entries(), ...state.no.entries()]) { |
| 34 | if (entry.userKey === userKey) return id; |
| 35 | } |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | export async function handleSwitch(interaction: ChatInputCommandInteraction): Promise<void> { |
| 40 | const member = await interaction.guild!.members.fetch(interaction.user.id); |
| 41 | const isOfficer = hasOfficerRole(member, cfg("officerRoles")); |
| 42 | const nameArg = interaction.options.getString("name"); |
| 43 | const charName = interaction.options.getString("char_name", true); |
| 44 | |
| 45 | let userKey: string | null; |
| 46 | if (nameArg) { |
| 47 | if (!isOfficer) return void replyAndDelete(interaction, "❌ Only officers can switch other players' characters."); |
| 48 | userKey = nameArg; |
| 49 | } else { |
| 50 | const user = await resolveUser(member); |
| 51 | userKey = user.userKey; |
| 52 | } |
| 53 | |
| 54 | if (!userKey) return void replyAndDelete(interaction, "❌ You are not registered in the system."); |
| 55 | |
| 56 | let resolvedChar: any = null; |
| 57 | let borrowedFrom: string | null = null; |
| 58 | |
| 59 | // Try own characters first |
| 60 | const set = setActiveCharacter(userKey, charName); |
| 61 | if (set) { |
| 62 | clearPersistentPreference(userKey); |
| 63 | resolvedChar = getActiveCharacter(userKey); |
| 64 | } else { |
| 65 | // Fall back to shared characters |
| 66 | const shared = findSharedChar(userKey, charName); |
| 67 | if (shared) { |
| 68 | setSessionBorrow(userKey, shared.ownerKey, shared.char.name); |
| 69 | setPersistentPreference(userKey, shared.ownerKey, shared.char.name); |
| 70 | resolvedChar = shared.char; |
| 71 | borrowedFrom = shared.ownerKey; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (!resolvedChar) return void replyAndDelete(interaction, `❌ No character named **${charName}** found.`); |
| 76 | |
| 77 | const currentActive = getEffectiveCharacter(userKey); |
| 78 | if (currentActive.char?.name === resolvedChar.name) { |
| 79 | // Already active — just show current state |
| 80 | const classEmoji = getClassEmoji(resolvedChar.class) || resolvedChar.class; |
| 81 | const borrowNote = currentActive.borrowedFrom ? ` *(shared by ${currentActive.borrowedFrom})*` : ""; |
| 82 | return void replyAndDelete(interaction, `${classEmoji} ${resolvedChar.level} ${resolvedChar.name}${borrowNote}`, true); |
| 83 | } |
| 84 | |
| 85 | // Check if target character is already in the active poll by another player |
| 86 | const slot = [...polls.keys()][0]; |
| 87 | if (slot !== undefined) { |
| 88 | const state = polls.get(slot)!; |
| 89 | for (const [id, entry] of state.yes.entries()) { |
| 90 | const isOwnEntry = id === (nameArg ? `injected:${userKey}` : interaction.user.id) || |
| 91 | id === `impersonated:${userKey}`; |
| 92 | if (!isOwnEntry && entry.characterName === resolvedChar.name && entry.userKey !== userKey) { |
| 93 | // Character is taken — check if we're the owner |
| 94 | const isOwner = getCharacters(userKey).some((c) => c.name === resolvedChar!.name); |
| 95 | if (isOwner) { |
| 96 | // Show conflict embed — need a button interaction for this, so just error with instructions |
| 97 | const slotHour = slot !== undefined ? polls.get(slot!)?.slot : cfg("slots")[0]?.tgHour ?? 20; |
| 98 | const charDisplay = format.char(resolvedChar); |
| 99 | return void replyAndDelete(interaction, |
| 100 | `⚠️ ${charDisplay} is already scheduled for TG at ${slotHour}:00. Use the poll buttons to reclaim it.`, |
| 101 | true |
| 102 | ); |
| 103 | } |
| 104 | const slotHour = slot !== undefined ? polls.get(slot)?.slot : cfg("slots")[0]?.tgHour ?? 20; |
| 105 | return void replyAndDelete(interaction, |
| 106 | `❌ ${format.char(resolvedChar)} is already scheduled for TG at ${slotHour}:00. Pick a different character.`, |
| 107 | true |
| 108 | ); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Update poll embed if user has voted |
| 114 | if (slot !== undefined) { |
| 115 | const state = polls.get(slot)!; |
| 116 | const userId = nameArg |
| 117 | ? findUserIdInPoll(state, userKey) |
| 118 | : interaction.user.id; |
| 119 | |
| 120 | if (userId && (state.yes.has(userId) || state.no.has(userId))) { |
| 121 | const updateEntry = (map: Map<string, any>) => { |
| 122 | const entry = map.get(userId); |
| 123 | if (entry) { |
| 124 | entry.characterName = resolvedChar.name; |
| 125 | entry.characterClass = resolvedChar.class; |
| 126 | entry.characterLevel = resolvedChar.level; |
| 127 | entry.characterNation = resolvedChar.nation; |
| 128 | entry.borrowedFrom = borrowedFrom ?? undefined; |
| 129 | } |
| 130 | }; |
| 131 | updateEntry(state.yes); |
| 132 | updateEntry(state.no); |
| 133 | |
| 134 | const channel = await interaction.client.channels.fetch(cfg("pollChannelId")) as TextChannel; |
| 135 | await updatePollMessage(channel, slot); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | const classEmoji = getClassEmoji(resolvedChar.class) || resolvedChar.class; |
| 140 | const borrowNote = borrowedFrom ? ` *(shared by ${borrowedFrom})*` : ""; |
| 141 | return void replyAndDelete(interaction, `🔄 ${classEmoji} ${resolvedChar.level} ${resolvedChar.name}${borrowNote}`, true); |
| 142 | } |