From 381fae0807fce8565398b137d38c2988d8ab5536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Borges=20Martins?= Date: Wed, 11 Jan 2023 05:31:52 -0300 Subject: [PATCH 1/4] chore(locales): case log `expiration` -> `duration` --- apps/yuudachi/locales/en-US/translation.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/yuudachi/locales/en-US/translation.json b/apps/yuudachi/locales/en-US/translation.json index 61854c5a6..086ccb536 100644 --- a/apps/yuudachi/locales/en-US/translation.json +++ b/apps/yuudachi/locales/en-US/translation.json @@ -539,7 +539,7 @@ "mod_log": { "case_log": { "description": "**Member**: `{{- target_tag}}` ({{target_id}})\n**Action**: {{- action}}", - "expiration": "\n**Expiration**: {{- time}}", + "duration": "\n**Duration**: `{{- time}}` {{- timestamp}}", "context": "**Context:** {{- link}}", "context_sub": "Beam me up, Yuu", "reason": "\n**Reason:** {{- reason}}", From 1b045d3ae1935ffa4201f865c61ae22ebfce6405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Borges=20Martins?= Date: Wed, 11 Jan 2023 05:33:10 -0300 Subject: [PATCH 2/4] feat(utils): util to find the duration accounting for small changes --- apps/yuudachi/src/util/findClosestDuration.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 apps/yuudachi/src/util/findClosestDuration.ts diff --git a/apps/yuudachi/src/util/findClosestDuration.ts b/apps/yuudachi/src/util/findClosestDuration.ts new file mode 100644 index 000000000..d703b0817 --- /dev/null +++ b/apps/yuudachi/src/util/findClosestDuration.ts @@ -0,0 +1,24 @@ +const DurationLabel = { + "1m": 60 * 1_000, + "5m": 5 * 60 * 1_000, + "10m": 10 * 60 * 1_000, + "1h": 60 * 60 * 1_000, + "3h": 3 * 60 * 60 * 1_000, + "6h": 6 * 60 * 60 * 1_000, + "12h": 12 * 60 * 60 * 1_000, + "1d": 24 * 60 * 60 * 1_000, + "2d": 2 * 24 * 60 * 60 * 1_000, + "3d": 3 * 24 * 60 * 60 * 1_000, + "7d": 7 * 24 * 60 * 60 * 1_000, +}; + +export function findClosestDuration(value: number): string { + const keys = Object.keys(DurationLabel); + const durations = Object.values(DurationLabel); + + const closest = durations.reduce((prev, curr) => { + return Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev; + }); + + return keys[durations.indexOf(closest)]!; +} From d2114ec124526c1ebdaaf69bb177b33f687ff92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Borges=20Martins?= Date: Wed, 11 Jan 2023 05:34:24 -0300 Subject: [PATCH 3/4] chore(caseLog): case log `expiration` -> `duration` --- apps/yuudachi/src/functions/logging/generateCaseLog.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/yuudachi/src/functions/logging/generateCaseLog.ts b/apps/yuudachi/src/functions/logging/generateCaseLog.ts index 58802e748..fb40fb9e0 100644 --- a/apps/yuudachi/src/functions/logging/generateCaseLog.ts +++ b/apps/yuudachi/src/functions/logging/generateCaseLog.ts @@ -3,6 +3,7 @@ import { Client, type Snowflake, hyperlink, time, TimestampStyles, messageLink, import i18next from "i18next"; import type { Sql } from "postgres"; import { caseActionLabel } from "../../util/actionKeys.js"; +import { findClosestDuration } from "../../util/findClosestDuration.js"; import { type Case, CaseAction } from "../cases/createCase.js"; import { getGuildSetting, SettingsKeys } from "../settings/getGuildSetting.js"; @@ -36,8 +37,11 @@ export async function generateCaseLog(case_: Case, logChannelId: Snowflake, loca }); if (case_.actionExpiration) { - msg += i18next.t("log.mod_log.case_log.expiration", { - time: time(new Date(case_.actionExpiration), TimestampStyles.RelativeTime), + const expirationDate = new Date(case_.actionExpiration); + + msg += i18next.t("log.mod_log.case_log.duration", { + time: findClosestDuration(expirationDate.getTime() - Date.now()), + timestamp: time(expirationDate, TimestampStyles.RelativeTime), lng: locale, }); } From e5c3c2911d77e79b9ef35d72fb35833a262c3cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Borges=20Martins?= Date: Wed, 11 Jan 2023 21:26:51 -0300 Subject: [PATCH 4/4] chore(genLog): use `@naval-base/ms` for conversion --- .../src/functions/logging/generateCaseLog.ts | 4 ++-- apps/yuudachi/src/util/findClosestDuration.ts | 24 ------------------- 2 files changed, 2 insertions(+), 26 deletions(-) delete mode 100644 apps/yuudachi/src/util/findClosestDuration.ts diff --git a/apps/yuudachi/src/functions/logging/generateCaseLog.ts b/apps/yuudachi/src/functions/logging/generateCaseLog.ts index fb40fb9e0..b43f4ddeb 100644 --- a/apps/yuudachi/src/functions/logging/generateCaseLog.ts +++ b/apps/yuudachi/src/functions/logging/generateCaseLog.ts @@ -1,9 +1,9 @@ +import { ms } from "@naval-base/ms"; import { logger, kSQL, container } from "@yuudachi/framework"; import { Client, type Snowflake, hyperlink, time, TimestampStyles, messageLink, channelLink } from "discord.js"; import i18next from "i18next"; import type { Sql } from "postgres"; import { caseActionLabel } from "../../util/actionKeys.js"; -import { findClosestDuration } from "../../util/findClosestDuration.js"; import { type Case, CaseAction } from "../cases/createCase.js"; import { getGuildSetting, SettingsKeys } from "../settings/getGuildSetting.js"; @@ -40,7 +40,7 @@ export async function generateCaseLog(case_: Case, logChannelId: Snowflake, loca const expirationDate = new Date(case_.actionExpiration); msg += i18next.t("log.mod_log.case_log.duration", { - time: findClosestDuration(expirationDate.getTime() - Date.now()), + time: ms(expirationDate.getTime() - Date.now()), timestamp: time(expirationDate, TimestampStyles.RelativeTime), lng: locale, }); diff --git a/apps/yuudachi/src/util/findClosestDuration.ts b/apps/yuudachi/src/util/findClosestDuration.ts deleted file mode 100644 index d703b0817..000000000 --- a/apps/yuudachi/src/util/findClosestDuration.ts +++ /dev/null @@ -1,24 +0,0 @@ -const DurationLabel = { - "1m": 60 * 1_000, - "5m": 5 * 60 * 1_000, - "10m": 10 * 60 * 1_000, - "1h": 60 * 60 * 1_000, - "3h": 3 * 60 * 60 * 1_000, - "6h": 6 * 60 * 60 * 1_000, - "12h": 12 * 60 * 60 * 1_000, - "1d": 24 * 60 * 60 * 1_000, - "2d": 2 * 24 * 60 * 60 * 1_000, - "3d": 3 * 24 * 60 * 60 * 1_000, - "7d": 7 * 24 * 60 * 60 * 1_000, -}; - -export function findClosestDuration(value: number): string { - const keys = Object.keys(DurationLabel); - const durations = Object.values(DurationLabel); - - const closest = durations.reduce((prev, curr) => { - return Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev; - }); - - return keys[durations.indexOf(closest)]!; -}