From 57d4d28cb946ba4d61baf2394bb3e4d61d031e15 Mon Sep 17 00:00:00 2001 From: shni Date: Sat, 27 Sep 2025 18:10:59 -0500 Subject: [PATCH] feat: update leaderboard to display user display names instead of IDs --- src/commands/messages/alliaces/leaderboard.ts | 47 ++++++++++++++++--- src/components/buttons/ldRefresh.ts | 1 - 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/commands/messages/alliaces/leaderboard.ts b/src/commands/messages/alliaces/leaderboard.ts index dabcad6..d8f1e17 100644 --- a/src/commands/messages/alliaces/leaderboard.ts +++ b/src/commands/messages/alliaces/leaderboard.ts @@ -6,10 +6,10 @@ import type { Message } from "discord.js"; const MAX_ENTRIES = 10; -function formatRow(index: number, userId: string, points: number): string { +function formatRow(index: number, displayName: string, points: number): string { const rank = String(index + 1).padStart(2, ' '); const pts = String(points).padStart(5, ' '); - return `#${rank} ${userId} (${pts})`; + return `#${rank} ${displayName} (${pts})`; } async function getLeaderboardData(guildId: string) { @@ -43,23 +43,57 @@ function codeBlock(lines: string[]): string { export async function buildLeaderboardPanel(message: Message) { const guild = message.guild!; const guildId = guild.id; - const userId = message.author.username + const userId = message.author.id; const [boards, ranks] = await Promise.all([ getLeaderboardData(guildId), getSelfRanks(guildId, userId) ]); + // Construir mapa de nombres visibles para los usuarios presentes en los top + const ids = new Set(); + for (const x of boards.weekly) ids.add(x.userId); + for (const x of boards.monthly) ids.add(x.userId); + for (const x of boards.total) ids.add(x.userId); + + const idList = Array.from(ids); + const nameMap = new Map(); + // Intentar primero desde el cache del guild + for (const id of idList) { + const m = guild.members.cache.get(id); + if (m) nameMap.set(id, m.displayName || m.user.username || id); + } + // Fetch individual para los que falten (evitar peticiones innecesarias) + for (const id of idList) { + if (nameMap.has(id)) continue; + try { + const m = await guild.members.fetch(id); + if (m) { + nameMap.set(id, m.displayName || m.user.username || id); + continue; + } + } catch {} + try { + const u = await message.client.users.fetch(id); + if (u) { + nameMap.set(id, u.username || id); + continue; + } + } catch {} + // Fallback: no mostrar ID crudo; usar placeholder + nameMap.set(id, 'Usuario desconocido'); + } + const weeklyLines = boards.weekly.length - ? boards.weekly.map((x, i) => formatRow(i, x.userId, x.weeklyPoints)) + ? boards.weekly.map((x, i) => formatRow(i, nameMap.get(x.userId) || 'Usuario desconocido', x.weeklyPoints)) : ['(sin datos)']; const monthlyLines = boards.monthly.length - ? boards.monthly.map((x, i) => formatRow(i, x.userId, x.monthlyPoints)) + ? boards.monthly.map((x, i) => formatRow(i, nameMap.get(x.userId) || 'Usuario desconocido', x.monthlyPoints)) : ['(sin datos)']; const totalLines = boards.total.length - ? boards.total.map((x, i) => formatRow(i, x.userId, x.totalPoints)) + ? boards.total.map((x, i) => formatRow(i, nameMap.get(x.userId) || 'Usuario desconocido', x.totalPoints)) : ['(sin datos)']; const now = new Date(); @@ -124,4 +158,3 @@ export const command: CommandMessage = { }); } }; - diff --git a/src/components/buttons/ldRefresh.ts b/src/components/buttons/ldRefresh.ts index 1159648..65a827b 100644 --- a/src/components/buttons/ldRefresh.ts +++ b/src/components/buttons/ldRefresh.ts @@ -20,4 +20,3 @@ export default { } } }; -