Ostatnio aktywny 1 month ago

lock.ts Surowy
1import { ChatInputCommandInteraction, TextChannel } from "discord.js";
2import { cfg } from "@systems/config";
3import { polls, lockPoll, updatePollMessage } from "@systems/poll";
4import { replyAndDelete } from "@utils";
5
6export async function handleLock(interaction: ChatInputCommandInteraction): Promise<void> {
7 const oneTimeMsg = interaction.options.getString("message") ?? undefined;
8 const simulateClose = interaction.options.getBoolean("simulate_close") ?? false;
9 const slot = getActiveSlot();
10 if (slot === undefined) return void replyAndDelete(interaction, "❌ No active poll found.");
11
12 // Use lockPoll() so lockedYesKeys gets snapshotted — same path as the cron
13 lockPoll(slot);
14
15 const channel = await interaction.client.channels.fetch(cfg("pollChannelId")) as TextChannel;
16
17 if (simulateClose) {
18 // Simulate TG end: show Submit Score button (same path as onPollClose cron)
19 await updatePollMessage(channel, slot, oneTimeMsg, true);
20 return void replyAndDelete(interaction, "🔒 Poll locked + 📊 Submit Score button shown (simulate close).");
21 }
22
23 // Normal lock: voting closes, no submit button yet
24 await updatePollMessage(channel, slot, oneTimeMsg);
25 return void replyAndDelete(interaction, "🔒 Poll locked.");
26}
27
28function getActiveSlot(): number | undefined {
29 return [...polls.keys()][0];
30}