import { ChatInputCommandInteraction, TextChannel } from "discord.js"; import { Config } from "@systems/config"; import { hasOfficerRole } from "@systems/users"; import { polls, updatePollMessage } from "@systems/poll"; import { persist } from "@systems/pollPersistence"; import { VoteEntry, PollState } from "@types"; import { Discord } from "@discord"; import { replyAndDelete } from "@utils"; interface FoundEntry { slot: number; state: PollState; entry: VoteEntry; } function findYesEntry(userKey: string): FoundEntry | null { const slot = [...polls.keys()][0]; if (slot === undefined) return null; const state = polls.get(slot)!; const entry = [...state.yes.values()].find((e) => e.userKey === userKey); return entry ? { slot, state, entry } : null; } export async function handleSleepCheckSet(interaction: ChatInputCommandInteraction): Promise { const member = await interaction.guild!.members.fetch(interaction.user.id); if (!hasOfficerRole(member, Config.get({ section: "roles", key: "officer" }))) { return void replyAndDelete(interaction, "❌ Officer only.", true); } const opts = Discord.Interaction.options(interaction); const userKey = opts.string({ key: "name", required: true })!; const found = findYesEntry(userKey); if (!found) { return void replyAndDelete(interaction, `❌ **${userKey}** hasn't voted Yes in the active poll.`, true); } found.entry.sleepCheckPending = true; persist.save(polls); const channel = await interaction.client.channels.fetch( Config.get({ section: "channels", key: "poll" }) ) as TextChannel; await updatePollMessage(channel, found.slot, undefined, found.state.scoreSubmitOpen === true); return void replyAndDelete(interaction, `💤 **${userKey}** flagged for sleep check. The confirmation DM goes out at the usual scheduled time.`, true); } export async function handleSleepCheckClear(interaction: ChatInputCommandInteraction): Promise { const member = await interaction.guild!.members.fetch(interaction.user.id); if (!hasOfficerRole(member, Config.get({ section: "roles", key: "officer" }))) { return void replyAndDelete(interaction, "❌ Officer only.", true); } const opts = Discord.Interaction.options(interaction); const userKey = opts.string({ key: "name", required: true })!; const found = findYesEntry(userKey); if (!found) { return void replyAndDelete(interaction, `❌ **${userKey}** hasn't voted Yes in the active poll.`, true); } if (!found.entry.sleepCheckPending) { return void replyAndDelete(interaction, `â„šī¸ **${userKey}** doesn't have a pending sleep check.`, true); } found.entry.sleepCheckPending = false; persist.save(polls); const channel = await interaction.client.channels.fetch( Config.get({ section: "channels", key: "poll" }) ) as TextChannel; await updatePollMessage(channel, found.slot, undefined, found.state.scoreSubmitOpen === true); return void replyAndDelete(interaction, `✅ Sleep check cleared for **${userKey}**.`, true); } export const SleepCheckCommands = { set: handleSleepCheckSet, clear: handleSleepCheckClear, };