Better detection of invalid times for reminder
All checks were successful
Lint and Format Check / lint-and-format (pull_request) Successful in 10s

This commit is contained in:
Mylloon 2024-09-17 22:47:52 +02:00
parent 99dd235a39
commit 0de9aecb89
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 32 additions and 7 deletions

View file

@ -158,12 +158,19 @@ export default {
channelId: interaction.channelId,
userId: interaction.user.id,
guildId: interaction.guildId,
}).then((msg) =>
})
.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()

View file

@ -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",

View file

@ -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",

View file

@ -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}.`);
},
);

View file

@ -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) {