From 0de9aecb897abf5f0958725c3aa7d23de167076e Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 17 Sep 2024 22:47:52 +0200 Subject: [PATCH] Better detection of invalid times for reminder --- src/commands/misc/reminder.ts | 19 +++++++++++++------ src/locales/en-US.json | 1 + src/locales/fr.json | 1 + src/utils/reminder.ts | 8 +++++++- src/utils/time.ts | 10 ++++++++++ 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/commands/misc/reminder.ts b/src/commands/misc/reminder.ts index 3138a34..0bf35b0 100644 --- a/src/commands/misc/reminder.ts +++ b/src/commands/misc/reminder.ts @@ -158,12 +158,19 @@ export default { channelId: interaction.channelId, userId: interaction.user.id, guildId: interaction.guildId, - }).then((msg) => - interaction.reply({ - content: msg as string, - ephemeral: true, - }), - ); + }) + .then((msg) => + interaction.reply({ + content: msg as string, + ephemeral: true, + }), + ) + .catch((err) => { + interaction.reply({ + content: err, + ephemeral: true, + }); + }); } else { // Show modal to user to get at least the time const modal = new ModalBuilder() diff --git a/src/locales/en-US.json b/src/locales/en-US.json index 807ac06..4f3dd13 100644 --- a/src/locales/en-US.json +++ b/src/locales/en-US.json @@ -77,6 +77,7 @@ "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", "c_play_desc": "Plays a song/playlist, no query displays the now playing song", diff --git a/src/locales/fr.json b/src/locales/fr.json index 9354717..d0af1c9 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -77,6 +77,7 @@ "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", "c_play_desc": "Joue une chanson/playlist, pas de requête affiche la chanson en cours actuellement", diff --git a/src/utils/reminder.ts b/src/utils/reminder.ts index c2a9912..f735920 100644 --- a/src/utils/reminder.ts +++ b/src/utils/reminder.ts @@ -64,8 +64,15 @@ const splitTime = (time: string) => { */ export const newReminder = async (client: Client, time: string, info: infoReminder) => new Promise((ok, ko) => { + const loc = getLocale(client, info.locale); + const data = splitTime(time); const timeout = strToSeconds(data.time); + if (timeout < 0) { + ko(loc.get("c_reminder18")); + return; + } + const timeoutId = setTimeoutReminder(client, info, data.option, timeout); // Add the remind to the db @@ -90,7 +97,6 @@ export const newReminder = async (client: Client, time: string, info: infoRemind } // Send confirmation to user - const loc = getLocale(client, info.locale); ok(`${loc.get("c_reminder1")} ${data.time}.`); }, ); diff --git a/src/utils/time.ts b/src/utils/time.ts index 89b36f9..b754314 100644 --- a/src/utils/time.ts +++ b/src/utils/time.ts @@ -24,6 +24,11 @@ enum TimeSecond { * @returns time in seconds */ export const strToSeconds = (time: string) => { + if (time.length > 15) { + // 15 is a magic value, weird to be this long + return -1; + } + const regex = new RegExp( `(?<${TimeSecond[TimeSecond.Year]}>[0-9]+(?=[y|a]))|(?<${ TimeSecond[TimeSecond.Week] @@ -36,6 +41,11 @@ export const strToSeconds = (time: string) => { const data = Object.assign({}, regex.exec(time.toLowerCase())?.groups); + if (Object.keys(data).length === 0) { + // Regex returned an invalid time + return -1; + } + let res = 0; Object.entries(data).forEach(([key, value]) => { if (value) {