Zuletzt aktiv 1 month ago

nuno hat die Gist bearbeitet 1 month ago. Zu Änderung gehen

1 file changed, 55 insertions

gistfile1.txt(Datei erstellt)

@@ -0,0 +1,55 @@
1 + import { ChatInputCommandInteraction } from "discord.js";
2 + import { cfg } from "../../systems/config";
3 + import { resolveUser, hasOfficerRole } from "../../systems/users";
4 + import { setActiveCharacter } from "../../systems/characters";
5 + import { setSessionBorrow } from "../../systems/borrow";
6 + import { replyAndDelete } from "../../utils";
7 + import fs from "fs";
8 + import path from "path";
9 +
10 + const CHARS_PATH = path.join(__dirname, "../../../data/characters.json");
11 +
12 + function 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 +
26 + export 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 + }
Neuer Älter