From dac3c910aad14efcf74aae96c3bd03fcef722afe Mon Sep 17 00:00:00 2001 From: gc <30398469+gc@users.noreply.github.com> Date: Sat, 13 Jul 2024 15:51:41 +1000 Subject: [PATCH] Fixes --- src/lib/PaginatedMessage.ts | 5 ++-- src/lib/util.ts | 5 ++-- src/mahoji/commands/leaderboard.ts | 45 +++++++++++++++++++++--------- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/lib/PaginatedMessage.ts b/src/lib/PaginatedMessage.ts index 447ea30e1e..8f184a31fd 100644 --- a/src/lib/PaginatedMessage.ts +++ b/src/lib/PaginatedMessage.ts @@ -1,7 +1,7 @@ import { UserError } from '@oldschoolgg/toolkit'; import type { BaseMessageOptions, ComponentType, MessageEditOptions, TextChannel } from 'discord.js'; import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js'; -import { Time } from 'e'; +import { Time, isFunction } from 'e'; import { InteractionID } from './InteractionID'; import type { PaginatedMessagePage } from './util'; @@ -77,8 +77,9 @@ export class PaginatedMessage { const rawPage = !Array.isArray(this.pages) ? await this.pages.generate({ currentPage: this.index }) : this.pages[this.index]; + return { - ...rawPage, + ...(isFunction(rawPage) ? await rawPage() : rawPage), components: numberOfPages === 1 ? [] diff --git a/src/lib/util.ts b/src/lib/util.ts index 265d8c8363..c5251f077f 100644 --- a/src/lib/util.ts +++ b/src/lib/util.ts @@ -335,7 +335,7 @@ const badgesKey = `${BOT_TYPE_LOWERCASE.toLowerCase()}_badges` as 'osb_badges' | const usernameWithBadgesCache = new LRUCache({ max: 2000 }); export function cacheUsername(id: string, username: string, badges: string) { const current = usernameWithBadgesCache.get(id); - const newValue = `${badges} ${username}`; + const newValue = `${badges ? `${badges} ` : ''}${username}`; if (!current || current !== newValue) { usernameWithBadgesCache.set(id, newValue); redis.setUser(id, { username: cleanUsername(username), [badgesKey]: badges }); @@ -347,7 +347,8 @@ export async function getUsername(_id: string | bigint): Promise { if (cached) return cached; const user = await redis.getUser(id); if (!user.username) return 'Unknown'; - const newValue = `${user[badgesKey]} ${user.username}`; + const badges = user[badgesKey]; + const newValue = `${badges ? `${badges} ` : ''}${user.username}`; usernameWithBadgesCache.set(id, newValue); return newValue; } diff --git a/src/mahoji/commands/leaderboard.ts b/src/mahoji/commands/leaderboard.ts index 20a783f512..c07926510b 100644 --- a/src/mahoji/commands/leaderboard.ts +++ b/src/mahoji/commands/leaderboard.ts @@ -1,6 +1,6 @@ import { toTitleCase } from '@oldschoolgg/toolkit'; import type { CommandRunOptions } from '@oldschoolgg/toolkit'; -import type { ChatInputCommandInteraction } from 'discord.js'; +import type { ChatInputCommandInteraction, MessageEditOptions } from 'discord.js'; import { EmbedBuilder } from 'discord.js'; import { ApplicationCommandOptionType } from 'discord.js'; import { calcWhatPercent, chunk, isFunction } from 'e'; @@ -47,11 +47,12 @@ export function getPos(page: number, record: number) { return `${page * LB_PAGE_SIZE + 1 + record}. `; } +export type AsyncPageString = () => Promise; export async function doMenu( interaction: ChatInputCommandInteraction, user: MUser, channelID: string, - pages: string[] | (() => Promise)[], + pages: string[] | AsyncPageString[], title: string ) { if (pages.length === 0) { @@ -75,7 +76,6 @@ export async function doMenu( function doMenuWrapper({ user, - interaction, channelID, users, title, @@ -91,17 +91,36 @@ function doMenuWrapper({ formatter?: (val: number) => string; }) { const chunked = chunk(users, LB_PAGE_SIZE); - const pages = []; - for (const chnk of chunked) { - const page = chnk - .map( - (user, i) => async () => - `${getPos(i, i)}**${await getUsername(user.id)}:** ${formatter ? formatter(user.score) : user.score.toLocaleString()}` - ) - .join('\n'); - pages.push(page); + const pages: (() => Promise)[] = []; + for (let c = 0; c < chunked.length; c++) { + const makePage = async () => { + const chnk = chunked[c]; + const unwaited = chnk.map( + async (user, i) => + `${getPos(c, i)}**${await getUsername(user.id)}:** ${formatter ? formatter(user.score) : user.score.toLocaleString()}` + ); + const pageText = (await Promise.all(unwaited)).join('\n'); + return { embeds: [new EmbedBuilder().setTitle(title).setDescription(pageText)] }; + }; + pages.push(makePage); + } + if (pages.length === 0) { + return 'There are no users on this leaderboard.'; } - doMenu(interaction, user, channelID, pages, title); + const channel = globalClient.channels.cache.get(channelID); + if (!channelIsSendable(channel)) return 'Invalid channel.'; + + makePaginatedMessage( + channel, + pages.map(p => { + if (isFunction(p)) { + return p; + } + + return { embeds: [new EmbedBuilder().setTitle(title).setDescription(p)] }; + }), + user.id + ); return lbMsg(title, ironmanOnly); }