gistfile1.txt
· 3.3 KiB · Text
Raw
async function handleSwitchAfterReclaim(btn: ButtonInteraction): Promise<void> {
const parts = btn.customId.split(":");
const userKey = parts[1];
const charName = parts[2];
const prevVoteType = (parts[3] ?? "yes") as "yes" | "no";
const chars = JSON.parse(
fs.readFileSync(path.join(__dirname, "../../data/characters.json"), "utf8")
);
let resolvedChar: any = null;
let borrowedFrom: string | null = null;
// Try own char first
const ownEntry = chars[userKey]?.characters?.find((c: any) => c.name === charName);
if (ownEntry) {
setActiveCharacter(userKey, charName);
clearSessionBorrowForUser(userKey);
resolvedChar = ownEntry;
} 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);
resolvedChar = char;
borrowedFrom = ownerKey;
break;
}
}
}
if (!resolvedChar) {
await btn.reply({ content: `❌ Could not switch to **${charName}**.`, ephemeral: true });
return;
}
// Re-add to poll with previous vote type
const slot = [...polls.keys()][0];
const state = slot !== undefined ? polls.get(slot) : null;
if (state && !state.locked && state.confirmed === null) {
const { char } = getEffectiveCharacter(userKey);
const now = nowFormatted();
const publicMsg = resolveMessage("public", prevVoteType, 1, userKey, null, null);
const voteEntry = {
userKey,
displayName: charName,
characterName: char?.name ?? charName,
characterClass: char?.class ?? resolvedChar.class,
characterLevel: char?.level ?? resolvedChar.level,
characterNation: char?.nation ?? resolvedChar.nation,
borrowedFrom: borrowedFrom ?? undefined,
discordId: btn.user.id,
votedAt: now,
publicMessage: publicMsg ?? undefined,
};
// Find and reuse existing vote ID — avoids duplicate entries
let existingVoteId: string | null = null;
for (const [id, entry] of [...state.yes.entries(), ...state.no.entries()]) {
if (entry.userKey === userKey) {
if (!existingVoteId) existingVoteId = id;
state.yes.delete(id);
state.no.delete(id);
}
}
const voteId = existingVoteId ?? btn.user.id;
if (prevVoteType === "yes") {
state.yes.set(voteId, voteEntry);
} else {
state.no.set(voteId, voteEntry);
}
console.log(`[switch_reclaim] cleaning up for userKey=${userKey}`);
console.log(`[switch_reclaim] yes keys:`, [...state.yes.entries()].map(([id, e]) => `${id}:${e.userKey}`));
console.log(`[switch_reclaim] no keys:`, [...state.no.entries()].map(([id, e]) => `${id}:${e.userKey}`));
const channel = await btn.client.channels.fetch(cfg("pollChannelId")) as TextChannel;
await updatePollMessage(channel, slot!);
}
const charDisplay = resolvedChar ? format.char(resolvedChar) : charName;
const borrowNote = borrowedFrom ? ` *(shared by ${borrowedFrom})*` : "";
await btn.reply({
content: `🔄 ${charDisplay}${borrowNote}${state ? ` — re-added to poll as **${prevVoteType}**.` : ""}`,
ephemeral: true,
});
}
| 1 | async function handleSwitchAfterReclaim(btn: ButtonInteraction): Promise<void> { |
| 2 | const parts = btn.customId.split(":"); |
| 3 | const userKey = parts[1]; |
| 4 | const charName = parts[2]; |
| 5 | const prevVoteType = (parts[3] ?? "yes") as "yes" | "no"; |
| 6 | |
| 7 | const chars = JSON.parse( |
| 8 | fs.readFileSync(path.join(__dirname, "../../data/characters.json"), "utf8") |
| 9 | ); |
| 10 | |
| 11 | let resolvedChar: any = null; |
| 12 | let borrowedFrom: string | null = null; |
| 13 | |
| 14 | // Try own char first |
| 15 | const ownEntry = chars[userKey]?.characters?.find((c: any) => c.name === charName); |
| 16 | if (ownEntry) { |
| 17 | setActiveCharacter(userKey, charName); |
| 18 | clearSessionBorrowForUser(userKey); |
| 19 | resolvedChar = ownEntry; |
| 20 | } else { |
| 21 | // Try shared char |
| 22 | for (const [ownerKey, data] of Object.entries(chars) as [string, any][]) { |
| 23 | const char = data.characters?.find( |
| 24 | (c: any) => c.name === charName && c.sharedWith?.includes(userKey) |
| 25 | ); |
| 26 | if (char) { |
| 27 | setPersistentPreference(userKey, ownerKey, charName); |
| 28 | clearSessionBorrowForUser(userKey); |
| 29 | resolvedChar = char; |
| 30 | borrowedFrom = ownerKey; |
| 31 | break; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if (!resolvedChar) { |
| 37 | await btn.reply({ content: `❌ Could not switch to **${charName}**.`, ephemeral: true }); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | // Re-add to poll with previous vote type |
| 42 | const slot = [...polls.keys()][0]; |
| 43 | const state = slot !== undefined ? polls.get(slot) : null; |
| 44 | |
| 45 | if (state && !state.locked && state.confirmed === null) { |
| 46 | const { char } = getEffectiveCharacter(userKey); |
| 47 | const now = nowFormatted(); |
| 48 | const publicMsg = resolveMessage("public", prevVoteType, 1, userKey, null, null); |
| 49 | const voteEntry = { |
| 50 | userKey, |
| 51 | displayName: charName, |
| 52 | characterName: char?.name ?? charName, |
| 53 | characterClass: char?.class ?? resolvedChar.class, |
| 54 | characterLevel: char?.level ?? resolvedChar.level, |
| 55 | characterNation: char?.nation ?? resolvedChar.nation, |
| 56 | borrowedFrom: borrowedFrom ?? undefined, |
| 57 | discordId: btn.user.id, |
| 58 | votedAt: now, |
| 59 | publicMessage: publicMsg ?? undefined, |
| 60 | }; |
| 61 | |
| 62 | // Find and reuse existing vote ID — avoids duplicate entries |
| 63 | let existingVoteId: string | null = null; |
| 64 | for (const [id, entry] of [...state.yes.entries(), ...state.no.entries()]) { |
| 65 | if (entry.userKey === userKey) { |
| 66 | if (!existingVoteId) existingVoteId = id; |
| 67 | state.yes.delete(id); |
| 68 | state.no.delete(id); |
| 69 | } |
| 70 | } |
| 71 | const voteId = existingVoteId ?? btn.user.id; |
| 72 | |
| 73 | if (prevVoteType === "yes") { |
| 74 | state.yes.set(voteId, voteEntry); |
| 75 | } else { |
| 76 | state.no.set(voteId, voteEntry); |
| 77 | } |
| 78 | |
| 79 | console.log(`[switch_reclaim] cleaning up for userKey=${userKey}`); |
| 80 | console.log(`[switch_reclaim] yes keys:`, [...state.yes.entries()].map(([id, e]) => `${id}:${e.userKey}`)); |
| 81 | console.log(`[switch_reclaim] no keys:`, [...state.no.entries()].map(([id, e]) => `${id}:${e.userKey}`)); |
| 82 | |
| 83 | const channel = await btn.client.channels.fetch(cfg("pollChannelId")) as TextChannel; |
| 84 | await updatePollMessage(channel, slot!); |
| 85 | } |
| 86 | |
| 87 | const charDisplay = resolvedChar ? format.char(resolvedChar) : charName; |
| 88 | const borrowNote = borrowedFrom ? ` *(shared by ${borrowedFrom})*` : ""; |
| 89 | await btn.reply({ |
| 90 | content: `🔄 ${charDisplay}${borrowNote}${state ? ` — re-added to poll as **${prevVoteType}**.` : ""}`, |
| 91 | ephemeral: true, |
| 92 | }); |
| 93 | } |