import { ChatInputCommandInteraction } from "discord.js"; import { Config } from "@systems/config"; import { CharacterRegistry } from "@registry/character-registry"; import { Score } from "@systems/score"; import { TGKey } from "@systems/tg-key"; import { Discord } from "@discord"; import { RuntimeEvents } from "@systems/runtime"; import { hasModeratorRole } from "@systems/users"; import { parseStatValue } from "@helpers/stat-value"; export async function handleScoreInject(interaction: ChatInputCommandInteraction): Promise { await Discord.Interaction.deferReply(interaction, { ephemeral: true }); const member = await interaction.guild!.members.fetch(interaction.user.id); if (!hasModeratorRole(member, Config.get({ section: "roles", key: "moderator" }))) { await Discord.Interaction.editReply(interaction, "❌ Moderator only."); return; } const opts = Discord.Interaction.options(interaction); const charName = opts.string({ key: "char_name", required: true })!; const playedByArg = opts.string({ key: "played_by" }) ?? undefined; const pts = opts.integer({ key: "pts", required: true })!; const slot = opts.integer({ key: "slot", required: true })!; const date = opts.string({ key: "date" }) ?? new Date().toISOString().slice(0, 10); const k = opts.integer({ key: "k" }) ?? undefined; const d = opts.integer({ key: "d" }) ?? undefined; const atkRaw = opts.string({ key: "atk" }); const defRaw = opts.string({ key: "def" }); const healRaw = opts.string({ key: "heal" }); const atkResult = parseStatValue(atkRaw, "Attack score"); const defResult = parseStatValue(defRaw, "Defense score"); const healResult = parseStatValue(healRaw, "Healing score"); const atk = atkResult.value; const def = defResult.value; const heal = healResult.value; const statError = atkResult.error ?? defResult.error ?? healResult.error; if (statError) { await Discord.Interaction.editReply(interaction, `❌ ${statError}`); return; } const char = CharacterRegistry.find(charName); if (!char) { await Discord.Interaction.editReply(interaction, `❌ Character **${charName}** not found.`); return; } const historyKey = TGKey.from({ date: date, slot }); await Score.submit({ character: char, pts, k, d, atk, def, heal, slot, date, playedBy: playedByArg, submittedByModerator: true, }); await RuntimeEvents.emit("scoreSubmitted", { historyKey, character: char }); await Discord.Interaction.editReply(interaction, `✅ Score injected for **${char.name}** — 📊 ${pts}${k !== undefined ? ` ⚔️ ${k}/${d ?? 0}` : ""} · \`${TGKey.toDisplay(historyKey)}\`` ); } export const ScoreInjectCommands = { inject: handleScoreInject, autocompleteHistory: async (interaction: any) => { const { autocompleteHistoryKey } = require("./result-post"); await autocompleteHistoryKey(interaction); }, };