nuno 已修改 3 weeks ago. 還原成這個修訂版本
1 file changed, 75 insertions
gistfile1.txt(檔案已創建)
| @@ -0,0 +1,75 @@ | |||
| 1 | + | import { ChatInputCommandInteraction } from "discord.js"; | |
| 2 | + | import { Config } from "@systems/config"; | |
| 3 | + | import { resolveUser, hasOfficerRole } from "@systems/users"; | |
| 4 | + | import { submitScore, detectSlot, normalizeSlot } from "@systems/scores"; | |
| 5 | + | import { getEffectiveCharacter } from "@systems/borrow"; | |
| 6 | + | import { replyAndDelete } from "@utils"; | |
| 7 | + | import { getEmoji } from "@systems/emojis"; | |
| 8 | + | ||
| 9 | + | export async function handleScoreSet(interaction: ChatInputCommandInteraction): Promise<void> { | |
| 10 | + | const member = await interaction.guild!.members.fetch(interaction.user.id); | |
| 11 | + | const isOfficer = hasOfficerRole(member, Config.get({ section: "roles", key: "officer" })); | |
| 12 | + | const nameArg = interaction.options.getString("name"); | |
| 13 | + | const ptsArg = interaction.options.getInteger("pts", true); | |
| 14 | + | const slotArg = interaction.options.getString("slot"); | |
| 15 | + | const kills = interaction.options.getInteger("k") ?? undefined; | |
| 16 | + | const deaths = interaction.options.getInteger("d") ?? undefined; | |
| 17 | + | const k = interaction.options.getInteger("k") ?? undefined; | |
| 18 | + | const d = interaction.options.getInteger("d") ?? undefined; | |
| 19 | + | const atk = interaction.options.getInteger("atk") ?? undefined; | |
| 20 | + | const def = interaction.options.getInteger("def") ?? undefined; | |
| 21 | + | const heal = interaction.options.getInteger("heal") ?? undefined; | |
| 22 | + | ||
| 23 | + | let userKey: string | null; | |
| 24 | + | if (nameArg) { | |
| 25 | + | if (!isOfficer) return void replyAndDelete(interaction, "❌ Only officers can submit scores for other players."); | |
| 26 | + | userKey = nameArg; | |
| 27 | + | } else { | |
| 28 | + | const user = await resolveUser(member); | |
| 29 | + | userKey = user.userKey; | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | if (!userKey) return void replyAndDelete(interaction, "❌ You are not registered in the system."); | |
| 33 | + | ||
| 34 | + | const { char, borrowedFrom } = getEffectiveCharacter(userKey); | |
| 35 | + | if (!char) return void replyAndDelete(interaction, "❌ No active character found. Use `/tg char set-active` first."); | |
| 36 | + | ||
| 37 | + | let slot: number | null = null; | |
| 38 | + | if (slotArg) { | |
| 39 | + | slot = normalizeSlot(slotArg); | |
| 40 | + | if (slot === null) return void replyAndDelete(interaction, `❌ Could not parse slot "${slotArg}".`); | |
| 41 | + | } else { | |
| 42 | + | slot = detectSlot() ?? Config.get({ section: "poll", key: "slots" }).find((s) => s.active)?.tgHour ?? 20; | |
| 43 | + | } | |
| 44 | + | ||
| 45 | + | await submitScore({ | |
| 46 | + | userKey: borrowedFrom ?? userKey, | |
| 47 | + | playedBy: borrowedFrom ? userKey : undefined, | |
| 48 | + | characterName: char.name, | |
| 49 | + | cls: char.class, | |
| 50 | + | nation: char.nation, | |
| 51 | + | pts: ptsArg, | |
| 52 | + | k, | |
| 53 | + | d, | |
| 54 | + | slot, | |
| 55 | + | atk, | |
| 56 | + | def, | |
| 57 | + | heal, | |
| 58 | + | submittedByOfficer: isOfficer && !!nameArg, | |
| 59 | + | }); | |
| 60 | + | ||
| 61 | + | const scoreEmoji = getEmoji("score") || "📊"; | |
| 62 | + | const kdEmoji = getEmoji("kd") || "⚔️"; | |
| 63 | + | const borrowNote = borrowedFrom ? ` *(borrowed from ${borrowedFrom})*` : ""; | |
| 64 | + | const kdNote = kills !== undefined && deaths !== undefined ? `\n${kdEmoji} ${kills}/${deaths}` : ""; | |
| 65 | + | const statsNote = [ | |
| 66 | + | atk !== undefined ? `ATK: ${atk}` : null, | |
| 67 | + | def !== undefined ? `DEF: ${def}` : null, | |
| 68 | + | heal !== undefined ? `HEAL: ${heal}` : null, | |
| 69 | + | ].filter(Boolean).join(" · "); | |
| 70 | + | ||
| 71 | + | return void replyAndDelete(interaction, | |
| 72 | + | `✅ ${scoreEmoji} **${ptsArg}** submitted for **${char.name}**${borrowNote} (${slot}:00 TG)${kdNote}${statsNote ? `\n${statsNote}` : ""}`, | |
| 73 | + | true | |
| 74 | + | ); | |
| 75 | + | } | |
上一頁
下一頁