Compare commits

..

2 commits

Author SHA1 Message Date
208dbf0813
better time delta when creating a reminder
All checks were successful
PR Check / lint-and-format (pull_request) Successful in 25s
2024-10-01 00:03:28 +02:00
78be496eda
better timeDeltaToString function 2024-09-30 23:57:57 +02:00
4 changed files with 33 additions and 25 deletions

View file

@ -60,14 +60,14 @@
"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 in", "c_reminder1": "A reminder has been set up for",
"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.",
"c_reminder5": "Reminders of", "c_reminder5": "Reminders of",
"c_reminder6": "Page", "c_reminder6": "Page",
"c_reminder7": "No message", "c_reminder7": "No message",
"c_reminder8": "Expires in", "c_reminder8": "Expires",
"c_reminder9": "Do on", "c_reminder9": "Do on",
"c_reminder10": "The user has no pending reminders or page no.", "c_reminder10": "The user has no pending reminders or page no.",
"c_reminder11": "empty", "c_reminder11": "empty",
@ -76,7 +76,6 @@
"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,14 +60,14 @@
"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 dans", "c_reminder1": "Un rappel a été configuré pour",
"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.",
"c_reminder5": "Rappels de", "c_reminder5": "Rappels de",
"c_reminder6": "Page", "c_reminder6": "Page",
"c_reminder7": "Pas de message", "c_reminder7": "Pas de message",
"c_reminder8": "Expire dans", "c_reminder8": "Expire",
"c_reminder9": "Fais le", "c_reminder9": "Fais le",
"c_reminder10": "L'utilisateur n'a aucun rappel en attente ou page n°", "c_reminder10": "L'utilisateur n'a aucun rappel en attente ou page n°",
"c_reminder11": "vide", "c_reminder11": "vide",
@ -76,7 +76,6 @@
"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

@ -88,6 +88,8 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
const timeoutId = setTimeoutReminder(client, info, data.option, timeout); const timeoutId = setTimeoutReminder(client, info, data.option, timeout);
const expiration_date = info.createdAt + timeout * 1000;
// Add the remind to the db // Add the remind to the db
client.db.run( client.db.run(
"INSERT INTO reminder ( \ "INSERT INTO reminder ( \
@ -95,7 +97,7 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? );", ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? );",
[ [
info.message, info.message,
`${info.createdAt + timeout * 1000}`, `${expiration_date}`,
data.option.valueOf(), data.option.valueOf(),
info.channelId, info.channelId,
`${info.createdAt}`, `${info.createdAt}`,
@ -110,7 +112,7 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
} }
// Send confirmation to user // Send confirmation to user
ok(`${loc.get("c_reminder1")} ${data.time}.`); ok(`${loc.get("c_reminder1")} ${timeDeltaToString(info.locale, expiration_date)}.`);
}, },
); );
}); });
@ -191,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: `${loc.get("c_reminder17")} ${timeDeltaToString(info.createdAt)}`, text: timeDeltaToString(info.locale, info.createdAt).capitalize(),
}); });
// Mention everybody if needed // Mention everybody if needed
@ -424,7 +426,7 @@ export const embedListReminders = async (
if (text.length > 1024) { if (text.length > 1024) {
text = `${text.substring(0, 1021)}...`; text = `${text.substring(0, 1021)}...`;
} }
const expiration = `${loc.get("c_reminder8")} ${timeDeltaToString(remind.expiration_date)}`; const expiration = `${loc.get("c_reminder8")} ${timeDeltaToString(local, remind.expiration_date)}`;
embed.addFields({ embed.addFields({
name: `#${remind.id}${loc.get("c_reminder9")} ${showDate( name: `#${remind.id}${loc.get("c_reminder9")} ${showDate(
local, local,

View file

@ -3,15 +3,15 @@ import { RegexC, RegExpFlags } from "./regex";
/** /**
* Parsed string adapted with TZ (locales) and format for the specified lang * Parsed string adapted with TZ (locales) and format for the specified lang
* @param tz Lang * @param lang Locale
* @param locale Locales * @param translation Translation for "at"
* @param date Date * @param date Date
* @returns String * @returns String
*/ */
export const showDate = (tz: string, locale: Map<string, unknown>, date: Date) => { export const showDate = (lang: string, translation: Map<string, unknown>, date: Date) => {
const localeInfo = new Intl.Locale(tz); const localeInfo = new Intl.Locale(lang);
const intlTimezone = moment.tz.zonesForCountry(localeInfo.region ?? localeInfo.baseName); const intlTimezone = moment.tz.zonesForCountry(localeInfo.region ?? localeInfo.baseName);
const formattedDate = new Intl.DateTimeFormat(tz, { const formattedDate = new Intl.DateTimeFormat(lang, {
timeZone: intlTimezone ? intlTimezone[0] : "Factory", timeZone: intlTimezone ? intlTimezone[0] : "Factory",
dateStyle: "short", dateStyle: "short",
timeStyle: "medium", timeStyle: "medium",
@ -19,14 +19,14 @@ export const showDate = (tz: string, locale: Map<string, unknown>, date: Date) =
.format(date) .format(date)
.split(" "); .split(" ");
return `${formattedDate[0]} ${locale.get("u_time_at")} ${formattedDate[1]}`; return `${formattedDate[0]} ${translation.get("u_time_at")} ${formattedDate[1]}`;
}; };
export enum TimeSecond { export enum TimeSecond {
Year = 31536000, Year = 60 * 60 * 24 * 365,
Week = 604800, Week = 60 * 60 * 24 * 7,
Day = 86400, Day = 60 * 60 * 24,
Hour = 3600, Hour = 60 * 60,
Minute = 60, Minute = 60,
Second = 1, Second = 1,
} }
@ -95,13 +95,21 @@ export const strToSeconds = (time: string) => {
/** /**
* Calculating the difference between a date and now * Calculating the difference between a date and now
* @param lang Locale
* @param time Time * @param time Time
* @returns Delta between the time and now * @returns Delta between the time and now
*/ */
export const timeDeltaToString = (time: number) => { export const timeDeltaToString = (lang: string, time: number) => {
const now = Date.now(); const now = Date.now();
// TODO: adapt the output and not always parse the time as seconds const secondsDifference = Math.round((time - now) / 1000) - 2;
// https://git.mylloon.fr/ConfrerieDuKassoulait/Botanique/issues/189
// Use Intl.RelativeTimeFormat ? const rtf = new Intl.RelativeTimeFormat(lang, { numeric: "auto" });
return `${strToSeconds(`${(now - time) / 1000}`)} secs`; for (const [unit, value] of Object.entries(TimeSecond)) {
const delta = secondsDifference / (value as number);
if (delta >= 1) {
return rtf.format(Math.round(delta), unit.toLowerCase() as Intl.RelativeTimeFormatUnit);
}
}
return rtf.format(secondsDifference, "second");
}; };