lock.ts
· 1.3 KiB · TypeScript
Eredeti
import { ChatInputCommandInteraction, TextChannel } from "discord.js";
import { cfg } from "@systems/config";
import { polls, lockPoll, updatePollMessage } from "@systems/poll";
import { replyAndDelete } from "@utils";
export async function handleLock(interaction: ChatInputCommandInteraction): Promise<void> {
const oneTimeMsg = interaction.options.getString("message") ?? undefined;
const simulateClose = interaction.options.getBoolean("simulate_close") ?? false;
const slot = getActiveSlot();
if (slot === undefined) return void replyAndDelete(interaction, "❌ No active poll found.");
// Use lockPoll() so lockedYesKeys gets snapshotted — same path as the cron
lockPoll(slot);
const channel = await interaction.client.channels.fetch(cfg("pollChannelId")) as TextChannel;
if (simulateClose) {
// Simulate TG end: show Submit Score button (same path as onPollClose cron)
await updatePollMessage(channel, slot, oneTimeMsg, true);
return void replyAndDelete(interaction, "🔒 Poll locked + 📊 Submit Score button shown (simulate close).");
}
// Normal lock: voting closes, no submit button yet
await updatePollMessage(channel, slot, oneTimeMsg);
return void replyAndDelete(interaction, "🔒 Poll locked.");
}
function getActiveSlot(): number | undefined {
return [...polls.keys()][0];
}
| 1 | import { ChatInputCommandInteraction, TextChannel } from "discord.js"; |
| 2 | import { cfg } from "@systems/config"; |
| 3 | import { polls, lockPoll, updatePollMessage } from "@systems/poll"; |
| 4 | import { replyAndDelete } from "@utils"; |
| 5 | |
| 6 | export 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 | |
| 28 | function getActiveSlot(): number | undefined { |
| 29 | return [...polls.keys()][0]; |
| 30 | } |