export async function handleInteraction(interaction: Interaction): Promise { try { if (interaction.isAutocomplete()) { await handleAutocomplete(interaction); return; } if (interaction.isButton()) { const btn = interaction as ButtonInteraction; console.log("[interactions] interaction btnId:", btn.customId); if (btn.customId.startsWith("conflict_")) { console.log("[interactions] routing to conflict handler:", btn.customId); return await handleConflictButton(btn); } if (btn.customId.startsWith("impersonate_")) { return await handleImpersonateButton(btn); } if (btn.customId.startsWith("switch_after_reclaim:")) { return await handleSwitchAfterReclaim(btn); } 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); } if (btn.customId === "tg_score_submit") { return await handleScoreSubmitButton(btn); } if (btn.customId.startsWith("companion_switch:")) { return await handleSwitchAfterReclaim(btn); } return await handleButton(btn); } if (interaction.isModalSubmit()) { return await modals.handleModal(interaction); } if (interaction.isStringSelectMenu()) { const sel = interaction as StringSelectMenuInteraction; if (sel.customId.startsWith("score_slot_select:")) { const userKey = sel.customId.split(":")[1]; const slot = parseInt(sel.values[0], 10); await sel.showModal(modals.buildScoreModal(userKey, slot)); return; } } if (interaction.isChatInputCommand()) { const cmd = interaction as ChatInputCommandInteraction; if (cmd.commandName === "tg") await handleTgCommand(cmd); if (cmd.commandName === "tg-config") await handleTgConfigCommand(cmd); if (cmd.commandName === "tg-admin") await handleTgAdminCommand(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 {} } }