import { ChatInputCommandInteraction, TextChannel, EmbedBuilder } from "discord.js"; import { cfg } from "../../systems/config"; import { loadResult, todayString } from "../../systems/history"; import { normalizeSlot, detectSlot } from "../../systems/scores"; import { replyAndDelete } from "../../utils"; 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() ?? cfg("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: "Capella" | "Procyon"): 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("Capella")}`, inline: true }, { name: "🔴 Procyon", value: `${kd.procyon.k}K / ${kd.procyon.d}D\n${formatScores("Procyon")}`, inline: true }, ) .setFooter({ text: `Source of truth: ${kd.source}` }) .setTimestamp(); const channelId = cfg("resultsChannelId") || cfg("pollChannelId"); const channel = await interaction.client.channels.fetch(channelId) as TextChannel; await channel.send({ embeds: [embed] }); return void replyAndDelete(interaction, "✅ Results posted."); }