import { ChatInputCommandInteraction, TextChannel, EmbedBuilder } from "discord.js"; import { Config } from "@systems/config"; import { loadResult, todayString } from "@systems/history"; import { normalizeSlot, detectSlot } from "@systems/scores"; import { replyAndDelete } from "@src/utils"; import { Nation } from "@types"; export async function handleResultPost(interaction: ChatInputCommandInteraction): Promise { const slotArg = interaction.options.getString("slot"); let slot: number | null = null; if (slotArg) { slot = normalizeSlot(slotArg); if (slot === null) return void replyAndDelete(interaction, `❌ Could not parse slot "${slotArg}".`); } else { slot = detectSlot() ?? Config.get({ section: "poll", key: "slots" })[0]?.tgHour ?? 20; } const result = loadResult(todayString(), slot); if (!result) return void replyAndDelete(interaction, `❌ No result found for ${slot}:00 TG.`); const kd = result.nationKD; const formatScores = (nation: Nation): string => { const scores = result.scores.filter((s) => s.nation === nation); if (scores.length === 0) return "—"; return scores .sort((a, b) => b.pts - a.pts) .map((s) => `**${s.characterName}** (${s.class}) — ${s.pts} pts`) .join("\n"); }; const embed = new EmbedBuilder() .setTitle(`⚔️ TG Results — ${result.date} ${slot}:00`) .setColor(0xe8a317) .addFields( { name: "🔵 Capella", value: `${kd.capella.k}K / ${kd.capella.d}D\n${formatScores(Nation.Capella)}`, inline: true }, { name: "🔴 Procyon", value: `${kd.procyon.k}K / ${kd.procyon.d}D\n${formatScores(Nation.Procyon)}`, inline: true }, ) .setFooter({ text: `Source of truth: ${kd.source}` }) .setTimestamp(); const channelId = Config.get({ section: "channels", key: "results" }) || Config.get({ section: "channels", key: "poll" }); const channel = await interaction.client.channels.fetch(channelId) as TextChannel; await channel.send({ embeds: [embed] }); return void replyAndDelete(interaction, "✅ Results posted."); }