tg-bot-ts/src/subcommands/poll/confirm.ts

47 lines
1.9 KiB
TypeScript

import { ChatInputCommandInteraction, TextChannel } from "discord.js";
import { Config } from "@systems/config";
import { polls, updatePollMessage } from "@systems/poll";
import { replyAndDelete } from "../../utils";
import { Nation } from "../../types";
export async function handleConfirm(interaction: ChatInputCommandInteraction): Promise<void> {
const decision = interaction.options.getString("decision", true) as "yes" | "no";
const oneTimeMsg = interaction.options.getString("message") ?? null;
const doTag = interaction.options.getBoolean("tag") ?? false;
const slot = [...polls.keys()][0];
if (slot === undefined) return void replyAndDelete(interaction, "❌ No active poll found.");
const state = polls.get(slot)!;
state.confirmed = decision;
state.locked = true;
const channel = await interaction.client.channels.fetch(Config.get({ section: "channels", key: "poll" })) as TextChannel;
if (oneTimeMsg) {
const isYes = decision === "yes";
const key = isYes ? "confirmYes" : "confirmNo";
const saved = Config.get({ section: "poll", key });
Config.set({ section: "poll", key, value: oneTimeMsg });
await updatePollMessage(channel, slot);
Config.set({ section: "poll", key, value: saved });
} else {
await updatePollMessage(channel, slot);
}
if (doTag) await tagRoles(channel, interaction);
return void replyAndDelete(interaction, `${decision === "yes" ? "✅" : "❌"} TG confirmed as **${decision}**.`);
}
async function tagRoles(channel: TextChannel, interaction: ChatInputCommandInteraction): Promise<void> {
const roles = Config.get({ section: "roles", key: "tag" });
const guild = interaction.guild!;
await guild.roles.fetch();
const mentions = roles
.map((name) => guild.roles.cache.find((r) => r.name === name))
.filter(Boolean)
.map((r) => `<@&${r!.id}>`)
.join(" ");
if (mentions) await channel.send(mentions);
}