async function handleSwitchAfterReclaim(btn: ButtonInteraction): Promise { const prefix = btn.customId.startsWith("companion_switch:") ? "companion_switch:" : "switch_after_reclaim:"; const withoutPrefix = btn.customId.slice(prefix.length); const firstColon = withoutPrefix.indexOf(":"); const userKey = withoutPrefix.slice(0, firstColon); const rest = withoutPrefix.slice(firstColon + 1); const lastColon = rest.lastIndexOf(":"); const charName = rest.slice(0, lastColon); const prevVoteType = (rest.slice(lastColon + 1) || "yes") as "yes" | "no"; const impersonating = getImpersonation(btn.user.id); const voteId = impersonating ? `impersonated:${impersonating}` : btn.user.id; const chars = JSON.parse( fs.readFileSync(path.join(__dirname, "../../data/characters.json"), "utf8") ); // Resolve char without switching let resolvedChar: any = null; let borrowedFrom: string | null = null; const ownEntry = chars[userKey]?.characters?.find((c: any) => c.name === charName); if (ownEntry) { resolvedChar = ownEntry; } else { 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) { resolvedChar = char; borrowedFrom = ownerKey; break; } } } if (!resolvedChar) { await btn.reply({ content: `❌ Could not switch to **${charName}**.`, ephemeral: true }); return; } // Delegate to shared switch logic const result = await Character.performSwitch(userKey, resolvedChar, borrowedFrom, btn, prevVoteType); if (result.replyData) { await btn.reply(result.replyData); return; } console.log(`[switchAfterReclaim] result:`, JSON.stringify({ success: result.success, conflicted: result.conflicted, hasReplyData: !!result.replyData, message: result.message })); if (result.replyData) { const { content, components } = result.replyData; console.log(`[switchAfterReclaim] replyData, isCompanion=${btn.customId.startsWith("companion_switch:")}`); if (btn.customId.startsWith("companion_switch:")) { await Ephemeral.update(voteId, "companion", content, components); } else { await btn.reply(result.replyData); } return; } if (result.success && result.message) { await Ephemeral.update(voteId, "companion", result.message, []); if (!btn.customId.startsWith("companion_switch:")) { await btn.reply({ content: result.message, ephemeral: true }); } // companion_switch: companion was updated above, no second reply needed } }