59 lines
No EOL
2.2 KiB
TypeScript
59 lines
No EOL
2.2 KiB
TypeScript
import { ChatInputCommandInteraction, TextChannel } from "discord.js";
|
|
import { Config } from "@systems/config";
|
|
import { polls, updatePollMessage } from "@systems/poll";
|
|
import { nowFormatted, resolveMessage } from "@systems/messages";
|
|
import { getEffectiveCharacter } from "@systems/borrow";
|
|
import { replyAndDelete } from "@utils";
|
|
import { VoteEntry, UsermapEntry } from "@types";
|
|
import { UserRegistry } from "@registry/user-registry";
|
|
|
|
export async function handleSeed(interaction: ChatInputCommandInteraction): Promise<void> {
|
|
const slot = [...polls.keys()][0];
|
|
if (slot === undefined) return void replyAndDelete(interaction, "❌ No active poll found.");
|
|
|
|
const state = polls.get(slot)!;
|
|
if (state.locked || state.confirmed !== null) {
|
|
return void replyAndDelete(interaction, "❌ Poll is locked or confirmed.");
|
|
}
|
|
|
|
const usermapEntries = UserRegistry.all();
|
|
|
|
if (usermapEntries.length === 0) {
|
|
return void replyAndDelete(interaction, "❌ No registered users found.");
|
|
}
|
|
|
|
const now = nowFormatted();
|
|
let injected = 0;
|
|
let skipped = 0;
|
|
|
|
for (const { entry } of usermapEntries) {
|
|
const userKey = typeof entry === "string" ? entry : entry.file;
|
|
const { char, borrowedFrom } = getEffectiveCharacter(userKey);
|
|
|
|
if (!char) { skipped++; continue; }
|
|
|
|
const syntheticId = `injected:${userKey}`;
|
|
if (state.yes.has(syntheticId) || state.no.has(syntheticId)) { skipped++; continue; }
|
|
|
|
const publicMsg = resolveMessage("public", "yes", 1, userKey, null, null);
|
|
|
|
const voteEntry: VoteEntry = {
|
|
userKey,
|
|
displayName: char.name,
|
|
characterName: char.name,
|
|
characterClass: char.class,
|
|
characterLevel: char.level,
|
|
characterNation: char.nation,
|
|
borrowedFrom: borrowedFrom ?? undefined,
|
|
votedAt: now,
|
|
publicMessage: publicMsg ?? undefined,
|
|
};
|
|
|
|
state.yes.set(syntheticId, voteEntry);
|
|
injected++;
|
|
}
|
|
|
|
const channel = await interaction.client.channels.fetch(Config.get({ section: "channels", key: "poll" })) as TextChannel;
|
|
await updatePollMessage(channel, slot);
|
|
return void replyAndDelete(interaction, `✅ Seeded **${injected}** player(s)${skipped > 0 ? `, skipped **${skipped}** (no active character)` : ""}.`);
|
|
} |