post.ts
· 1.9 KiB · TypeScript
Raw
import { ChatInputCommandInteraction, TextChannel, EmbedBuilder } from "discord.js";
import { cfg } from "../../systems/config";
import { getCurrentWeek, getWeekKey, getBringer } from "../../systems/wrank";
import { replyAndDelete } from "../../utils";
export async function handleRankPost(interaction: ChatInputCommandInteraction): Promise<void> {
const week = getCurrentWeek();
const goal = cfg("wRankGoal");
const weekKey = getWeekKey();
const formatNation = (nation: "capella" | "procyon"): string => {
const entries = [...week.entries[nation]].sort((a, b) => a.currentRank - b.currentRank);
if (entries.length === 0) return "—";
const bringer = getBringer(nation === "capella" ? "Capella" : "Procyon");
return entries.map((e) => {
const isDone = e.tgCount >= goal;
const delta = e.previousRank !== undefined ? e.currentRank - e.previousRank : 0;
const deltaStr = delta < 0 ? ` ↑${Math.abs(delta)}` : delta > 0 ? ` ↓${delta}` : "";
const bringerStr = bringer === e.userKey && isDone
? ` · ${nation === "capella" ? "Luminous Bringer" : "Storm Bringer"}`
: "";
return `${isDone ? "🟡" : "⬜"}${e.currentRank}${deltaStr} «${e.characterName}» — ${e.weeklyPoints} pts (${e.tgCount}/${goal}${bringerStr})`;
}).join("\n");
};
const embed = new EmbedBuilder()
.setTitle(`⚔️ W.Rank Leaderboard — ${weekKey}`)
.setColor(0xe8a317)
.addFields(
{ name: "🔵 Capella", value: formatNation("capella"), inline: true },
{ name: "🔴 Procyon", value: formatNation("procyon"), inline: true },
)
.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, "✅ Leaderboard posted.");
}
| 1 | import { ChatInputCommandInteraction, TextChannel, EmbedBuilder } from "discord.js"; |
| 2 | import { cfg } from "../../systems/config"; |
| 3 | import { getCurrentWeek, getWeekKey, getBringer } from "../../systems/wrank"; |
| 4 | import { replyAndDelete } from "../../utils"; |
| 5 | |
| 6 | export async function handleRankPost(interaction: ChatInputCommandInteraction): Promise<void> { |
| 7 | const week = getCurrentWeek(); |
| 8 | const goal = cfg("wRankGoal"); |
| 9 | const weekKey = getWeekKey(); |
| 10 | |
| 11 | const formatNation = (nation: "capella" | "procyon"): string => { |
| 12 | const entries = [...week.entries[nation]].sort((a, b) => a.currentRank - b.currentRank); |
| 13 | if (entries.length === 0) return "—"; |
| 14 | const bringer = getBringer(nation === "capella" ? "Capella" : "Procyon"); |
| 15 | return entries.map((e) => { |
| 16 | const isDone = e.tgCount >= goal; |
| 17 | const delta = e.previousRank !== undefined ? e.currentRank - e.previousRank : 0; |
| 18 | const deltaStr = delta < 0 ? ` ↑${Math.abs(delta)}` : delta > 0 ? ` ↓${delta}` : ""; |
| 19 | const bringerStr = bringer === e.userKey && isDone |
| 20 | ? ` · ${nation === "capella" ? "Luminous Bringer" : "Storm Bringer"}` |
| 21 | : ""; |
| 22 | return `${isDone ? "🟡" : "⬜"}${e.currentRank}${deltaStr} «${e.characterName}» — ${e.weeklyPoints} pts (${e.tgCount}/${goal}${bringerStr})`; |
| 23 | }).join("\n"); |
| 24 | }; |
| 25 | |
| 26 | const embed = new EmbedBuilder() |
| 27 | .setTitle(`⚔️ W.Rank Leaderboard — ${weekKey}`) |
| 28 | .setColor(0xe8a317) |
| 29 | .addFields( |
| 30 | { name: "🔵 Capella", value: formatNation("capella"), inline: true }, |
| 31 | { name: "🔴 Procyon", value: formatNation("procyon"), inline: true }, |
| 32 | ) |
| 33 | .setTimestamp(); |
| 34 | |
| 35 | const channelId = cfg("resultsChannelId") || cfg("pollChannelId"); |
| 36 | const channel = await interaction.client.channels.fetch(channelId) as TextChannel; |
| 37 | await channel.send({ embeds: [embed] }); |
| 38 | return void replyAndDelete(interaction, "✅ Leaderboard posted."); |
| 39 | } |
| 40 |