.ts
· 2.6 KiB · TypeScript
Surowy
async function handleSwitchAfterReclaim(btn: ButtonInteraction): Promise<void> {
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
}
}
| 1 | async function handleSwitchAfterReclaim(btn: ButtonInteraction): Promise<void> { |
| 2 | const prefix = btn.customId.startsWith("companion_switch:") ? "companion_switch:" : "switch_after_reclaim:"; |
| 3 | const withoutPrefix = btn.customId.slice(prefix.length); |
| 4 | const firstColon = withoutPrefix.indexOf(":"); |
| 5 | const userKey = withoutPrefix.slice(0, firstColon); |
| 6 | const rest = withoutPrefix.slice(firstColon + 1); |
| 7 | const lastColon = rest.lastIndexOf(":"); |
| 8 | const charName = rest.slice(0, lastColon); |
| 9 | const prevVoteType = (rest.slice(lastColon + 1) || "yes") as "yes" | "no"; |
| 10 | |
| 11 | const impersonating = getImpersonation(btn.user.id); |
| 12 | const voteId = impersonating ? `impersonated:${impersonating}` : btn.user.id; |
| 13 | |
| 14 | const chars = JSON.parse( |
| 15 | fs.readFileSync(path.join(__dirname, "../../data/characters.json"), "utf8") |
| 16 | ); |
| 17 | |
| 18 | // Resolve char without switching |
| 19 | let resolvedChar: any = null; |
| 20 | let borrowedFrom: string | null = null; |
| 21 | |
| 22 | const ownEntry = chars[userKey]?.characters?.find((c: any) => c.name === charName); |
| 23 | if (ownEntry) { |
| 24 | resolvedChar = ownEntry; |
| 25 | } else { |
| 26 | for (const [ownerKey, data] of Object.entries(chars) as [string, any][]) { |
| 27 | const char = data.characters?.find( |
| 28 | (c: any) => c.name === charName && c.sharedWith?.includes(userKey) |
| 29 | ); |
| 30 | if (char) { |
| 31 | resolvedChar = char; |
| 32 | borrowedFrom = ownerKey; |
| 33 | break; |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if (!resolvedChar) { |
| 39 | await btn.reply({ content: `❌ Could not switch to **${charName}**.`, ephemeral: true }); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // Delegate to shared switch logic |
| 44 | const result = await Character.performSwitch(userKey, resolvedChar, borrowedFrom, btn, prevVoteType); |
| 45 | |
| 46 | if (result.replyData) { |
| 47 | await btn.reply(result.replyData); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | console.log(`[switchAfterReclaim] result:`, JSON.stringify({ success: result.success, conflicted: result.conflicted, hasReplyData: !!result.replyData, message: result.message })); |
| 52 | |
| 53 | if (result.replyData) { |
| 54 | const { content, components } = result.replyData; |
| 55 | console.log(`[switchAfterReclaim] replyData, isCompanion=${btn.customId.startsWith("companion_switch:")}`); |
| 56 | if (btn.customId.startsWith("companion_switch:")) { |
| 57 | await Ephemeral.update(voteId, "companion", content, components); |
| 58 | } else { |
| 59 | await btn.reply(result.replyData); |
| 60 | } |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (result.success && result.message) { |
| 65 | await Ephemeral.update(voteId, "companion", result.message, []); |
| 66 | if (!btn.customId.startsWith("companion_switch:")) { |
| 67 | await btn.reply({ content: result.message, ephemeral: true }); |
| 68 | } |
| 69 | // companion_switch: companion was updated above, no second reply needed |
| 70 | } |
| 71 | } |