Остання активність 1 month ago

gistfile1.txt Неформатований
1import { ChatInputCommandInteraction } from "discord.js";
2import { cfg } from "../../systems/config";
3import { resolveUser, hasOfficerRole } from "../../systems/users";
4import { setActiveCharacter } from "../../systems/characters";
5import { setSessionBorrow } from "../../systems/borrow";
6import { replyAndDelete } from "../../utils";
7import fs from "fs";
8import path from "path";
9
10const CHARS_PATH = path.join(__dirname, "../../../data/characters.json");
11
12function findSharedChar(usermapKey: string, charName: string): { ownerKey: string; charName: string } | null {
13 try {
14 const chars = JSON.parse(fs.readFileSync(CHARS_PATH, "utf8"));
15 for (const [ownerKey, data] of Object.entries(chars) as [string, any][]) {
16 if (ownerKey === usermapKey) continue;
17 const char = data.characters?.find(
18 (c: any) => c.name.toLowerCase() === charName.toLowerCase() && c.sharedWith?.includes(usermapKey)
19 );
20 if (char) return { ownerKey, charName: char.name };
21 }
22 } catch {}
23 return null;
24}
25
26export async function handleCharSetActive(interaction: ChatInputCommandInteraction): Promise<void> {
27 const member = await interaction.guild!.members.fetch(interaction.user.id);
28 const isOfficer = hasOfficerRole(member, cfg("officerRoles"));
29 const nameArg = interaction.options.getString("name");
30 const charName = interaction.options.getString("char_name", true);
31
32 let usermapKey: string | null;
33 if (nameArg) {
34 if (!isOfficer) return void replyAndDelete(interaction, "❌ Only officers can manage other players' characters.");
35 usermapKey = nameArg;
36 } else {
37 const user = await resolveUser(member);
38 usermapKey = user.usermapKey;
39 }
40
41 if (!usermapKey) return void replyAndDelete(interaction, "❌ You are not registered in the system.");
42
43 // Try own characters first
44 const set = setActiveCharacter(usermapKey, charName);
45 if (set) return void replyAndDelete(interaction, `✅ **${charName}** is now your active character.`);
46
47 // Fall back to shared characters
48 const shared = findSharedChar(usermapKey, charName);
49 if (shared) {
50 setSessionBorrow(usermapKey, shared.ownerKey, shared.charName);
51 return void replyAndDelete(interaction, `✅ **${charName}** (shared by **${shared.ownerKey}**) set as active for this session.`);
52 }
53
54 return void replyAndDelete(interaction, `❌ No character named **${charName}** found.`);
55}