nuno ha revisionato questo gist 4 weeks ago. Vai alla revisione
1 file changed, 71 insertions
.ts(file creato)
| @@ -0,0 +1,71 @@ | |||
| 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 | + | } | |
Più nuovi
Più vecchi