tg-bot-ts/src/subcommands/poll/mark-left.ts

65 lines
No EOL
2.7 KiB
TypeScript

import { ChatInputCommandInteraction, TextChannel } from "discord.js";
import { Config } from "@systems/config";
import { resolveUser, hasOfficerRole } from "@systems/users";
import { Leaves } from "@systems/leaves";
import { polls, updatePollMessage } from "@systems/poll";
import { CharacterRegistry } from "@registry/character-registry";
import { replyAndDelete } from "@utils";
import { TGKey } from "@systems/tg-key";
export async function handleMarkLeft(interaction: ChatInputCommandInteraction): Promise<void> {
const member = await interaction.guild!.members.fetch(interaction.user.id);
if (!hasOfficerRole(member, Config.get({ section: "roles", key: "officer" }))) {
return void replyAndDelete(interaction, "❌ Only officers can mark characters as left.", true);
}
const charName = interaction.options.getString("char_name", true);
const officer = await resolveUser(member);
// Find character and its owner
const char = CharacterRegistry.find(charName);
if (!char) return void replyAndDelete(interaction, `❌ Character **${charName}** not found.`, true);
const slot = [...polls.keys()][0];
if (slot === undefined) return void replyAndDelete(interaction, "❌ No active poll.", true);
const historyKey = TGKey.current({ slot });
Leaves.mark({
characterName: char.name,
ownerKey: char.ownerKey,
historyKey,
markedBy: officer.userKey ?? "unknown",
});
const channel = interaction.channel as TextChannel;
await updatePollMessage(channel, slot);
const count = Leaves.countForChar({ characterName: char.name });
return void replyAndDelete(interaction,
`🪲 **${char.name}** marked as left (total leaves: ${count}).`,
true
);
}
export async function handleUnmarkLeft(interaction: ChatInputCommandInteraction): Promise<void> {
const member = await interaction.guild!.members.fetch(interaction.user.id);
if (!hasOfficerRole(member, Config.get({ section: "roles", key: "officer" }))) {
return void replyAndDelete(interaction, "❌ Only officers can unmark characters.", true);
}
const charName = interaction.options.getString("char_name", true);
const char = CharacterRegistry.find(charName);
if (!char) return void replyAndDelete(interaction, `❌ Character **${charName}** not found.`, true);
const slot = [...polls.keys()][0];
if (slot === undefined) return void replyAndDelete(interaction, "❌ No active poll.", true);
const historyKey = TGKey.current({ slot });
Leaves.unmark({ characterName: char.name, historyKey });
const channel = interaction.channel as TextChannel;
await updatePollMessage(channel, slot);
return void replyAndDelete(interaction, `✅ Removed left mark from **${char.name}**.`, true);
}