Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions apps/discord-bot/src/commands/duels/duels.command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,34 @@ interface PreProfileData {
modeIcons: DuelsModeIcons;
}

let modeIcons: Promise<DuelsModeIcons> | undefined;

const getModeIcons = async () => {
if (modeIcons) return modeIcons;

modeIcons = readdir(getAssetPath("duels"))
.then((modeIconPaths) =>
Promise.all(
modeIconPaths.map(async (mode) => [mode.replace(".png", ""), await loadImage(getAssetPath(`duels/${mode}`))])
)
)
.then((icons) => Object.fromEntries(icons))
.catch((error) => {
modeIcons = undefined;
throw error;
});

return modeIcons;
};

@Command({ description: (t) => t("commands.duels") })
export class DuelsCommand extends BaseHypixelCommand<DuelsModes, PreProfileData> {
public constructor() {
super(DUELS_MODES);
}

public async getPreProfileData(): Promise<PreProfileData> {
const modeIconPaths = await readdir(getAssetPath("duels"));
const modeIcons = await Promise.all(
modeIconPaths.map(async (mode) => [mode.replace(".png", ""), await loadImage(getAssetPath(`duels/${mode}`))])
);

return { modeIcons: Object.fromEntries(modeIcons) };
return { modeIcons: await getModeIcons() };
}

public getModeEmojis(modes: GameModeWithSubModes<DuelsModes>[]): ModeEmoji[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class BaseLeaderboardCommand {
cache.clear();
}, 300_000);

currentPage = page || currentPage;
currentPage = page ?? currentPage;

return { ...message, components: [row] };
}
Expand Down Expand Up @@ -271,7 +271,7 @@ export class BaseLeaderboardCommand {
getLeaderboardDataIcon
);

if (params.type === LeaderboardQuery.PAGE && page) cache.set(page, message);
if (params.type === LeaderboardQuery.PAGE && page !== null) cache.set(page, message);

return [message, page];
}
Expand Down
19 changes: 17 additions & 2 deletions packages/assets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,22 @@ const checkAsset = (file: string) =>

export const getAssetPath = (path: string) => join(PATH, checkAsset(path), path);

const getImage = (path: string) => loadImage(getAssetPath(path));
const images = new Map<string, Promise<Image>>();

const getCachedImage = (path: string) => {
const cachedImage = images.get(path);
if (cachedImage) return cachedImage;

const image = loadImage(path).catch((error) => {
images.delete(path);
throw error;
});

images.set(path, image);
return image;
};

const getImage = (path: string) => getCachedImage(getAssetPath(path));

/**
*
Expand Down Expand Up @@ -73,7 +88,7 @@ export function getLogo(
userOrLogoOrPath: User | UserLogo | string | null,
size?: number
): Promise<Image> {
return loadImage(getLogoPath(userOrLogoOrPath as User, size));
return getCachedImage(getLogoPath(userOrLogoOrPath as User, size));
}

export function getLogoPath(
Expand Down
9 changes: 4 additions & 5 deletions packages/discord/src/command/command.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { CommandBuilder } from "./command.builder.js";
import { Container } from "typedi";
import { Logger } from "@statsify/logger";
import { readdir } from "node:fs/promises";
import { statSync } from "node:fs";
import type { CommandResolvable } from "./command.resolvable.js";

export class CommandLoader {
Expand Down Expand Up @@ -58,15 +57,15 @@ export class CommandLoader {
private static async getCommandFiles(dir: string): Promise<string[]> {
const toLoad: string[] = [];

const files = await readdir(dir);
const files = await readdir(dir, { withFileTypes: true });

await Promise.all(
files.map(async (file) => {
const path = `${dir}/${file}`;
const path = `${dir}/${file.name}`;

if (statSync(path).isDirectory()) {
if (file.isDirectory()) {
toLoad.push(...(await this.getCommandFiles(path)));
} else if (file.endsWith(".command.js")) {
} else if (file.name.endsWith(".command.js")) {
toLoad.push(path);
}
})
Expand Down
9 changes: 4 additions & 5 deletions packages/discord/src/event/event.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { GatewayDispatchEvents } from "discord-api-types/v10";
import { Logger } from "@statsify/logger";
import { WebsocketShard } from "tiny-discord";
import { readdir } from "node:fs/promises";
import { statSync } from "node:fs";

export class EventLoader {
private static readonly logger = new Logger("EventLoader");
Expand Down Expand Up @@ -54,15 +53,15 @@ export class EventLoader {
private static async getEventFiles(dir: string): Promise<string[]> {
const toLoad: string[] = [];

const files = await readdir(dir);
const files = await readdir(dir, { withFileTypes: true });

await Promise.all(
files.map(async (file) => {
const path = `${dir}/${file}`;
const path = `${dir}/${file.name}`;

if (statSync(path).isDirectory()) {
if (file.isDirectory()) {
toLoad.push(...(await this.getEventFiles(path)));
} else if (file.endsWith(".event.js")) {
} else if (file.name.endsWith(".event.js")) {
toLoad.push(path);
}
})
Expand Down
Loading