feat: more readable time delta #193

Merged
Anri merged 6 commits from date into dev 2024-10-08 20:53:29 +02:00
4 changed files with 23 additions and 46 deletions
Showing only changes of commit 095aa7f68c - Show all commits

View file

@ -60,7 +60,7 @@
"c_reminder_sub3_desc": "Delete a reminder", "c_reminder_sub3_desc": "Delete a reminder",
"c_reminder_sub3_opt1_name": "id", "c_reminder_sub3_opt1_name": "id",
"c_reminder_sub3_opt1_desc": "Reminder to be deleted", "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_reminder2": "The ID entered is not valid.",
"c_reminder3": "Reminder not found, not in the right guild, or not belonging to you.", "c_reminder3": "Reminder not found, not in the right guild, or not belonging to you.",
"c_reminder4": "Unknown user.", "c_reminder4": "Unknown user.",
@ -76,6 +76,7 @@
"c_reminder14": "Message sent in DM as the channel is no longer available.", "c_reminder14": "Message sent in DM as the channel is no longer available.",
"c_reminder15": "Message sent in DM because you have left", "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_reminder16": "Message sent in DM because the Discord guild is no longer available.",
"c_reminder17": "Message from",
"c_reminder18": "Invalid time, try again.", "c_reminder18": "Invalid time, try again.",
"c_play_name": "play", "c_play_name": "play",

View file

@ -60,7 +60,7 @@
"c_reminder_sub3_desc": "Supprime un rappel", "c_reminder_sub3_desc": "Supprime un rappel",
"c_reminder_sub3_opt1_name": "id", "c_reminder_sub3_opt1_name": "id",
"c_reminder_sub3_opt1_desc": "Rappel à supprimé", "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_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_reminder3": "Rappel non trouvé, pas sur le bon serveur ou qui ne vous appartiens pas.",
"c_reminder4": "Utilisateur inconnu.", "c_reminder4": "Utilisateur inconnu.",
@ -76,6 +76,7 @@
"c_reminder14": "Message envoyé en DM car le salon n'est plus disponible.", "c_reminder14": "Message envoyé en DM car le salon n'est plus disponible.",
"c_reminder15": "Message envoyé en DM car vous avez quitté", "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_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_reminder18": "Temps invalide, réessayez.",
"c_play_name": "play", "c_play_name": "play",

View file

@ -193,7 +193,7 @@ export const sendReminder = (client: Client, info: infoReminder, option: OptionR
if (channel?.isSendable()) { if (channel?.isSendable()) {
let content = `<@${info.userId}>`; let content = `<@${info.userId}>`;
embed.setFooter({ embed.setFooter({
text: timeDeltaToString(info.createdAt).capitalize(), text: `${loc.get("c_reminder17")} ${timeDeltaToString(info.createdAt)}`,
}); });
// Mention everybody if needed // Mention everybody if needed

View file

@ -101,50 +101,25 @@ export const strToSeconds = (time: string) => {
*/ */
export const timeDeltaToString = (time: number) => { export const timeDeltaToString = (time: number) => {
const now = Date.now(); 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 if (secondsDifference === 0) {
let years = 0, return "0s";
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;
} }
// Remaining seconds return Object.entries(TimeSecond)
seconds = secondsDifference; .map(([key, value]) => ({
label: key.charAt(0).toLowerCase(),
// Create the detailed time string value: value as TimeSecond,
const detailedTimeParts = []; }))
if (years > 0) detailedTimeParts.push(`${years}y`); .map(({ label, value }) => {
if (weeks > 0) detailedTimeParts.push(`${weeks}w`); if (secondsDifference >= value) {
if (days > 0) detailedTimeParts.push(`${days}d`); const amount = Math.floor(secondsDifference / value);
if (hours > 0) detailedTimeParts.push(`${hours}h`); secondsDifference -= amount * value;
if (minutes > 0) detailedTimeParts.push(`${minutes}m`); return `${amount}${label}`;
if (seconds > 0) detailedTimeParts.push(`${seconds}s`); }
return null;
// Return the formatted string })
return detailedTimeParts.join(" "); .filter(Boolean)
.join(" ");
}; };