From 095aa7f68c0a9164c1bfa849c3a230478cc9d86a Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 8 Oct 2024 20:38:09 +0200 Subject: [PATCH] simplify code --- src/locales/en-US.json | 3 ++- src/locales/fr.json | 3 ++- src/utils/reminder.ts | 2 +- src/utils/time.ts | 61 +++++++++++++----------------------------- 4 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/locales/en-US.json b/src/locales/en-US.json index 380fa8f..1231731 100644 --- a/src/locales/en-US.json +++ b/src/locales/en-US.json @@ -60,7 +60,7 @@ "c_reminder_sub3_desc": "Delete a reminder", "c_reminder_sub3_opt1_name": "id", "c_reminder_sub3_opt1_desc": "Reminder to be deleted", - "c_reminder1": "A reminder has been set up for", + "c_reminder1": "A reminder has been set up for in", "c_reminder2": "The ID entered is not valid.", "c_reminder3": "Reminder not found, not in the right guild, or not belonging to you.", "c_reminder4": "Unknown user.", @@ -76,6 +76,7 @@ "c_reminder14": "Message sent in DM as the channel is no longer available.", "c_reminder15": "Message sent in DM because you have left", "c_reminder16": "Message sent in DM because the Discord guild is no longer available.", + "c_reminder17": "Message from", "c_reminder18": "Invalid time, try again.", "c_play_name": "play", diff --git a/src/locales/fr.json b/src/locales/fr.json index 64bbb94..725d499 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -60,7 +60,7 @@ "c_reminder_sub3_desc": "Supprime un rappel", "c_reminder_sub3_opt1_name": "id", "c_reminder_sub3_opt1_desc": "Rappel à supprimé", - "c_reminder1": "Un rappel a été configuré pour", + "c_reminder1": "Un rappel a été configuré pour dans", "c_reminder2": "L'ID renseigné n'est pas valide.", "c_reminder3": "Rappel non trouvé, pas sur le bon serveur ou qui ne vous appartiens pas.", "c_reminder4": "Utilisateur inconnu.", @@ -76,6 +76,7 @@ "c_reminder14": "Message envoyé en DM car le salon n'est plus disponible.", "c_reminder15": "Message envoyé en DM car vous avez quitté", "c_reminder16": "Message envoyé en DM car le serveur Discord n'est plus disponible.", + "c_reminder17": "Message d'il y a", "c_reminder18": "Temps invalide, réessayez.", "c_play_name": "play", diff --git a/src/utils/reminder.ts b/src/utils/reminder.ts index 0cf0a65..025e528 100644 --- a/src/utils/reminder.ts +++ b/src/utils/reminder.ts @@ -193,7 +193,7 @@ export const sendReminder = (client: Client, info: infoReminder, option: OptionR if (channel?.isSendable()) { let content = `<@${info.userId}>`; embed.setFooter({ - text: timeDeltaToString(info.createdAt).capitalize(), + text: `${loc.get("c_reminder17")} ${timeDeltaToString(info.createdAt)}`, }); // Mention everybody if needed diff --git a/src/utils/time.ts b/src/utils/time.ts index f47dd77..edf7bca 100644 --- a/src/utils/time.ts +++ b/src/utils/time.ts @@ -101,50 +101,25 @@ export const strToSeconds = (time: string) => { */ export const timeDeltaToString = (time: number) => { const now = Date.now(); - let secondsDifference = Math.round((time - now) / 1000); + let secondsDifference = Math.abs(Math.ceil((time - now) / 1000) - 2); - // Initialize variables to hold the time components - let years = 0, - weeks = 0, - days = 0, - hours = 0, - minutes = 0, - seconds = 0; - - // Calculate the time components - if (secondsDifference >= TimeSecond.Year) { - years = Math.floor(secondsDifference / TimeSecond.Year); - secondsDifference -= years * TimeSecond.Year; - } - if (secondsDifference >= TimeSecond.Week) { - weeks = Math.floor(secondsDifference / TimeSecond.Week); - secondsDifference -= weeks * TimeSecond.Week; - } - if (secondsDifference >= TimeSecond.Day) { - days = Math.floor(secondsDifference / TimeSecond.Day); - secondsDifference -= days * TimeSecond.Day; - } - if (secondsDifference >= TimeSecond.Hour) { - hours = Math.floor(secondsDifference / TimeSecond.Hour); - secondsDifference -= hours * TimeSecond.Hour; - } - if (secondsDifference >= TimeSecond.Minute) { - minutes = Math.floor(secondsDifference / TimeSecond.Minute); - secondsDifference -= minutes * TimeSecond.Minute; + if (secondsDifference === 0) { + return "0s"; } - // Remaining seconds - seconds = secondsDifference; - - // Create the detailed time string - const detailedTimeParts = []; - if (years > 0) detailedTimeParts.push(`${years}y`); - if (weeks > 0) detailedTimeParts.push(`${weeks}w`); - if (days > 0) detailedTimeParts.push(`${days}d`); - if (hours > 0) detailedTimeParts.push(`${hours}h`); - if (minutes > 0) detailedTimeParts.push(`${minutes}m`); - if (seconds > 0) detailedTimeParts.push(`${seconds}s`); - - // Return the formatted string - return detailedTimeParts.join(" "); + return Object.entries(TimeSecond) + .map(([key, value]) => ({ + label: key.charAt(0).toLowerCase(), + value: value as TimeSecond, + })) + .map(({ label, value }) => { + if (secondsDifference >= value) { + const amount = Math.floor(secondsDifference / value); + secondsDifference -= amount * value; + return `${amount}${label}`; + } + return null; + }) + .filter(Boolean) + .join(" "); };