Zuletzt aktiv 1 month ago

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

1 file changed, 96 insertions

interactions.ts(Datei erstellt)

@@ -0,0 +1,96 @@
1 + import { Interaction, ChatInputCommandInteraction, ButtonInteraction } from "discord.js";
2 + import { handleButton } from "@handlers/buttons";
3 + import { handleTgCommand } from "@commands/tg";
4 + import { handleTgConfigCommand } from "@commands/tgConfig";
5 + import { handleBorrowAcceptButton } from "@subcommands/char/accept";
6 + import { handleBorrowDeclineButton } from "@subcommands/char/decline";
7 + import { handleConflictButton } from "@systems/conflict";
8 + import { handleImpersonateButton } from "@subcommands/impersonate";
9 + import { handleAutocomplete } from "@handlers/autocomplete";
10 +
11 + export async function handleInteraction(interaction: Interaction): Promise<void> {
12 + try {
13 + if (interaction.isAutocomplete()) {
14 + await handleAutocomplete(interaction);
15 + return;
16 + }
17 +
18 + if (interaction.isButton()) {
19 + const btn = interaction as ButtonInteraction;
20 +
21 + // Conflict resolution buttons
22 + if (btn.customId.startsWith("conflict_")) {
23 + return await handleConflictButton(btn);
24 + }
25 +
26 + if (btn.customId.startsWith("impersonate_")) {
27 + return await handleImpersonateButton(btn);
28 + }
29 +
30 + if (btn.customId.startsWith("switch_after_reclaim:")) {
31 + const [, userKey, charName] = btn.customId.split(":");
32 + const { setActiveCharacter } = require("@systems/characters");
33 + const { setPersistentPreference, clearSessionBorrowForUser, getEffectiveCharacter } = require("@systems/borrow");
34 +
35 + // Check if it's a shared char
36 + const fs = require("fs");
37 + const path = require("path");
38 + const chars = JSON.parse(fs.readFileSync(path.join(__dirname, "../../data/characters.json"), "utf8"));
39 +
40 + let resolved = false;
41 + // Try own char first
42 + if (chars[userKey]?.characters?.find((c: any) => c.name === charName)) {
43 + setActiveCharacter(userKey, charName);
44 + clearSessionBorrowForUser(userKey);
45 + resolved = true;
46 + } else {
47 + // Try shared char
48 + for (const [ownerKey, data] of Object.entries(chars) as [string, any][]) {
49 + const char = data.characters?.find((c: any) => c.name === charName && c.sharedWith?.includes(userKey));
50 + if (char) {
51 + setPersistentPreference(userKey, ownerKey, charName);
52 + clearSessionBorrowForUser(userKey);
53 + resolved = true;
54 + break;
55 + }
56 + }
57 + }
58 +
59 + if (resolved) {
60 + await btn.reply({ content: `✅ Switched to **${charName}**.`, ephemeral: true });
61 + } else {
62 + await btn.reply({ content: `❌ Could not switch to **${charName}**.`, ephemeral: true });
63 + }
64 + return;
65 + }
66 +
67 + // Borrow accept/decline buttons from DM
68 + if (btn.customId.startsWith("borrow_accept:")) {
69 + const [, ownerKey, requesterKey] = btn.customId.split(":");
70 + return await handleBorrowAcceptButton(btn, ownerKey, requesterKey);
71 + }
72 + if (btn.customId.startsWith("borrow_decline:")) {
73 + const [, ownerKey, requesterKey] = btn.customId.split(":");
74 + return await handleBorrowDeclineButton(btn, ownerKey, requesterKey);
75 + }
76 +
77 + return await handleButton(btn);
78 + }
79 +
80 + if (interaction.isChatInputCommand()) {
81 + const cmd = interaction as ChatInputCommandInteraction;
82 + if (cmd.commandName === "tg") await handleTgCommand(cmd);
83 + if (cmd.commandName === "tg-config") await handleTgConfigCommand(cmd);
84 + }
85 + } catch (err) {
86 + console.error("Interaction error:", err);
87 + try {
88 + const msg = { content: "❌ An error occurred.", ephemeral: true };
89 + if ((interaction as any).replied || (interaction as any).deferred) {
90 + await (interaction as any).followUp(msg);
91 + } else {
92 + await (interaction as any).reply(msg);
93 + }
94 + } catch {}
95 + }
96 + }
Neuer Älter