Skip to content

Commit

Permalink
Support last updated rendering for leaderboards
Browse files Browse the repository at this point in the history
  • Loading branch information
gausie committed Nov 15, 2023
1 parent c99c6dc commit d31345a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/clients/kol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { stringify } from "querystring";
import sharp from "sharp";
import { dedent } from "ts-dedent";
import TypedEventEmitter, { EventMap } from "typed-emitter";
import { select } from "xpath";
import xpath, { select } from "xpath";

import { config } from "../config.js";
import { cleanString, indent, toWikiLink } from "../utils.js";
Expand Down Expand Up @@ -96,6 +96,7 @@ export type LeaderboardInfo = {
export type SubboardInfo = {
name: string;
runs: RunInfo[];
updated: Date | null;
};

type RunInfo = {
Expand Down Expand Up @@ -647,6 +648,7 @@ export class KoLClient extends (EventEmitter as new () => TypedEmitter<Events>)
)
.map((subboard) => {
const rows = selectMulti("./tr", subboard);

return {
name: (
selectMulti(".//text()", rows[0])[0]?.nodeValue || ""
Expand All @@ -670,10 +672,14 @@ export class KoLClient extends (EventEmitter as new () => TypedEmitter<Events>)
turns: rowText[rowText.length - 1].toString() || "0",
};
}),
updated: xpath.isComment(subboard.nextSibling)
? new Date(subboard.nextSibling.data.slice(9, -1))
: null,
};
}),
};
} catch (error) {
console.log(error);
return undefined;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/commands/kol/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
AutocompleteInteraction,
ChatInputCommandInteraction,
SlashCommandBuilder,
TimestampStyles,
time,
} from "discord.js";

import { createEmbed } from "../../clients/discord.js";
Expand Down Expand Up @@ -168,6 +170,16 @@ const formatSubboard = (subboard: SubboardInfo) => {
if (runs.length > 12) runs.splice(12, 0, "🥉 Bronze Buttons 🥉");
if (runs.length > 1) runs.splice(1, 0, "🥈 Silver Moons 🥈");
if (runs.length) runs.splice(0, 0, "🥇 Gold Star 🥇");

if (subboard.updated) {
runs.splice(
0,
0,
`Last updated ${time(subboard.updated, TimestampStyles.RelativeTime)}`,
"",
);
}

return {
title: subboard.name || "...",
name: subboard.name || "...",
Expand Down

0 comments on commit d31345a

Please sign in to comment.