Skip to content

Commit aa7adef

Browse files
committed
fix: padding edge cases in secsToTimeStr()
1 parent d641817 commit aa7adef

File tree

4 files changed

+17
-25
lines changed

4 files changed

+17
-25
lines changed

src/commands/VideoInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ export class VideoInfoCmd extends SlashCommand {
240240
if(typeof duration === "number") {
241241
embed.addFields({
242242
name: t("commands.video_info.embedFields.duration"),
243-
value: secsToTimeStr(duration, locale, true),
243+
value: secsToTimeStr(duration, locale),
244244
inline: true,
245245
});
246246
}

src/lib/text.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,21 @@ export function secsToYtTime(seconds: number) {
130130
}
131131

132132
/** Returns the passed amount of seconds in a human-readable format */
133-
export function secsToTimeStr(seconds: number, locale = "en-US", padded = false) {
133+
export function secsToTimeStr(seconds: number, locale = "en-US", padded = true) {
134134
const t = tr.use(locale);
135135

136136
const d = Math.floor(seconds / (60 * 60 * 24)),
137137
h = Math.floor(seconds / (60 * 60)) % 24,
138138
m = Math.floor(seconds / 60) % 60,
139139
s = Math.floor(seconds) % 60;
140140

141-
const pad = (n: number) => padded ? String(n).padStart(2, "0") : n;
141+
const pad = (n: number) => padded && n !== 0 ? String(n).padStart(2, "0") : n;
142142

143143
return ([
144144
[(60 * 60 * 24), `${d}${t("general.time.short.days")}`],
145-
[(60 * 60), `${pad(h)}${t("general.time.short.hours")}`],
146-
[60, `${pad(m)}${t("general.time.short.minutes")}`],
147-
[0, `${pad(s)}${t("general.time.short.seconds")}`],
145+
[(60 * 60), `${seconds >= (60 * 60 * 24) ? pad(h) : h}${t("general.time.short.hours")}`],
146+
[60, `${seconds >= (60 * 60) ? pad(m) : m}${t("general.time.short.minutes")}`],
147+
[0, `${seconds >= 60 ? pad(s) : s}${t("general.time.short.seconds")}`],
148148
] as const)
149149
.filter(([d]) => seconds >= d)
150150
.map(([, s]) => s)

src/main.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { initRegistry, registerCommandsForGuild } from "@lib/registry.ts";
77
import { autoPlural } from "@lib/text.ts";
88
import { envVarEq, getEnvVar } from "@lib/env.ts";
99
import { initTranslations } from "@lib/translate.ts";
10-
import { metChanId, metGuildId, updateMetrics } from "@src/metrics.ts";
10+
import { metChanId, metGuildId, metUpdInterval, updateMetrics } from "@src/metrics.ts";
1111
import { GuildConfig } from "@models/GuildConfig.model.ts";
1212

1313
//#region validate env
@@ -60,9 +60,6 @@ async function init() {
6060

6161
//#region intervalChks
6262

63-
const metUpdIvRaw = getEnvVar("METRICS_UPDATE_INTERVAL", "number");
64-
const metUpdInterval = Math.max(isNaN(metUpdIvRaw) ? 60 : metUpdIvRaw, 1);
65-
6663
const chkGldIntervalRaw = getEnvVar("GUILD_CHECK_INTERVAL", "number");
6764
const chkGldInterval = Math.max(isNaN(chkGldIntervalRaw) ? 300 : chkGldIntervalRaw, 10);
6865

src/metrics.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Col } from "@lib/embedify.ts";
66
import { getEnvVar } from "@lib/env.ts";
77
import { cmdInstances } from "@lib/registry.ts";
88
import { em } from "@lib/db.ts";
9-
import { autoPlural } from "@lib/text.ts";
9+
import { autoPlural, secsToTimeStr } from "@lib/text.ts";
1010
import { UserSettings } from "@models/UserSettings.model.ts";
1111
import pkg from "@root/package.json" with { type: "json" };
1212

@@ -15,6 +15,9 @@ import pkg from "@root/package.json" with { type: "json" };
1515
export const metGuildId = getEnvVar("METRICS_GUILD", "stringOrUndefined");
1616
export const metChanId = getEnvVar("METRICS_CHANNEL", "stringOrUndefined");
1717

18+
const metUpdIvRaw = getEnvVar("METRICS_UPDATE_INTERVAL", "number");
19+
export const metUpdInterval = Math.max(isNaN(metUpdIvRaw) ? 60 : metUpdIvRaw, 1);
20+
1821
const initTime = Date.now();
1922
const commitHash = await getCommitHash(true);
2023

@@ -166,12 +169,12 @@ async function useMetricsMsg(metrics: MetricsData) {
166169
const ebd = new EmbedBuilder()
167170
.setTitle("Bot metrics:")
168171
.setFields([
172+
{ name: "Guilds:", value: `${guildsAmt} ${autoPlural("guild", guildsAmt)}`, inline: true },
169173
{ name: "Users:", value: `${usersAmt} in DB`, inline: true },
170-
{ name: "Guilds:", value: `${guildsAmt} guilds`, inline: true },
171174
{ name: "Members:", value: `${totalMembersAmt} total\n${uniqueMembersAmt} unique`, inline: true },
172175
{ name: `${autoPlural("Command", cmdsTotal)} (${cmdsTotal}):`, value: `${slashCmdAmt} ${autoPlural("slash command", slashCmdAmt)}\n${ctxCmdAmt} ${autoPlural("context command", ctxCmdAmt)}`, inline: false },
173-
{ name: "Uptime:", value: `${uptimeStr}\n${time(new Date(initTime), "R")}`, inline: false },
174-
{ name: "Metrics updated:", value: time(new Date(), "R"), inline: false },
176+
{ name: "Uptime:", value: `${time(new Date(initTime), "R")}\nTime: ${uptimeStr}`, inline: false },
177+
{ name: "Metrics updated:", value: `${time(new Date(), "R")}\nInterval: ${secsToTimeStr(metUpdInterval)}`, inline: false },
175178
])
176179
.setFooter({ text: `v${pkg.version} - ${commitHash}` })
177180
.setColor(Col.Info);
@@ -193,15 +196,7 @@ async function useMetricsMsg(metrics: MetricsData) {
193196

194197
/** Returns the uptime in a human-readable format */
195198
function getUptime() {
196-
const upt = Date.now() - initTime;
197-
198-
return ([
199-
[(1000 * 60 * 60 * 24), `${Math.floor(upt / (1000 * 60 * 60 * 24))}d`],
200-
[(1000 * 60 * 60), `${Math.floor(upt / (1000 * 60 * 60)) % 24}h`],
201-
[(1000 * 60), `${Math.floor(upt / (1000 * 60)) % 60}m`],
202-
[0, `${Math.floor(upt / 1000) % 60}s`],
203-
] as const)
204-
.filter(([d]) => upt >= d)
205-
.map(([, s]) => s)
206-
.join(" ");
199+
const upt = Math.floor((Date.now() - initTime) / 1000);
200+
201+
return secsToTimeStr(upt, "en-US");
207202
}

0 commit comments

Comments
 (0)