interactions.ts
· 3.7 KiB · TypeScript
Eredeti
import { Interaction, ChatInputCommandInteraction, ButtonInteraction } from "discord.js";
import { handleButton } from "@handlers/buttons";
import { handleTgCommand } from "@commands/tg";
import { handleTgConfigCommand } from "@commands/tgConfig";
import { handleBorrowAcceptButton } from "@subcommands/char/accept";
import { handleBorrowDeclineButton } from "@subcommands/char/decline";
import { handleConflictButton } from "@systems/conflict";
import { handleImpersonateButton } from "@subcommands/impersonate";
import { handleAutocomplete } from "@handlers/autocomplete";
export async function handleInteraction(interaction: Interaction): Promise<void> {
try {
if (interaction.isAutocomplete()) {
await handleAutocomplete(interaction);
return;
}
if (interaction.isButton()) {
const btn = interaction as ButtonInteraction;
// Conflict resolution buttons
if (btn.customId.startsWith("conflict_")) {
return await handleConflictButton(btn);
}
if (btn.customId.startsWith("impersonate_")) {
return await handleImpersonateButton(btn);
}
if (btn.customId.startsWith("switch_after_reclaim:")) {
const [, userKey, charName] = btn.customId.split(":");
const { setActiveCharacter } = require("@systems/characters");
const { setPersistentPreference, clearSessionBorrowForUser, getEffectiveCharacter } = require("@systems/borrow");
// Check if it's a shared char
const fs = require("fs");
const path = require("path");
const chars = JSON.parse(fs.readFileSync(path.join(__dirname, "../../data/characters.json"), "utf8"));
let resolved = false;
// Try own char first
if (chars[userKey]?.characters?.find((c: any) => c.name === charName)) {
setActiveCharacter(userKey, charName);
clearSessionBorrowForUser(userKey);
resolved = true;
} else {
// Try shared char
for (const [ownerKey, data] of Object.entries(chars) as [string, any][]) {
const char = data.characters?.find((c: any) => c.name === charName && c.sharedWith?.includes(userKey));
if (char) {
setPersistentPreference(userKey, ownerKey, charName);
clearSessionBorrowForUser(userKey);
resolved = true;
break;
}
}
}
if (resolved) {
await btn.reply({ content: `✅ Switched to **${charName}**.`, ephemeral: true });
} else {
await btn.reply({ content: `❌ Could not switch to **${charName}**.`, ephemeral: true });
}
return;
}
// Borrow accept/decline buttons from DM
if (btn.customId.startsWith("borrow_accept:")) {
const [, ownerKey, requesterKey] = btn.customId.split(":");
return await handleBorrowAcceptButton(btn, ownerKey, requesterKey);
}
if (btn.customId.startsWith("borrow_decline:")) {
const [, ownerKey, requesterKey] = btn.customId.split(":");
return await handleBorrowDeclineButton(btn, ownerKey, requesterKey);
}
return await handleButton(btn);
}
if (interaction.isChatInputCommand()) {
const cmd = interaction as ChatInputCommandInteraction;
if (cmd.commandName === "tg") await handleTgCommand(cmd);
if (cmd.commandName === "tg-config") await handleTgConfigCommand(cmd);
}
} catch (err) {
console.error("Interaction error:", err);
try {
const msg = { content: "❌ An error occurred.", ephemeral: true };
if ((interaction as any).replied || (interaction as any).deferred) {
await (interaction as any).followUp(msg);
} else {
await (interaction as any).reply(msg);
}
} catch {}
}
}
| 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 | } |