Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Jul 13, 2024
1 parent 95e6f52 commit dac3c91
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/lib/PaginatedMessage.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
? []
Expand Down
5 changes: 3 additions & 2 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ const badgesKey = `${BOT_TYPE_LOWERCASE.toLowerCase()}_badges` as 'osb_badges' |
const usernameWithBadgesCache = new LRUCache<string, string>({ 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 });
Expand All @@ -347,7 +347,8 @@ export async function getUsername(_id: string | bigint): Promise<string> {
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;
}
Expand Down
45 changes: 32 additions & 13 deletions src/mahoji/commands/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -47,11 +47,12 @@ export function getPos(page: number, record: number) {
return `${page * LB_PAGE_SIZE + 1 + record}. `;
}

export type AsyncPageString = () => Promise<string>;
export async function doMenu(
interaction: ChatInputCommandInteraction,
user: MUser,
channelID: string,
pages: string[] | (() => Promise<string>)[],
pages: string[] | AsyncPageString[],
title: string
) {
if (pages.length === 0) {
Expand All @@ -75,7 +76,6 @@ export async function doMenu(

function doMenuWrapper({
user,
interaction,
channelID,
users,
title,
Expand All @@ -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<MessageEditOptions>)[] = [];
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);
}
Expand Down

0 comments on commit dac3c91

Please sign in to comment.