Better detection of invalid times for reminder
All checks were successful
Lint and Format Check / lint-and-format (pull_request) Successful in 10s
All checks were successful
Lint and Format Check / lint-and-format (pull_request) Successful in 10s
This commit is contained in:
parent
99dd235a39
commit
0de9aecb89
5 changed files with 32 additions and 7 deletions
|
@ -158,12 +158,19 @@ export default {
|
||||||
channelId: interaction.channelId,
|
channelId: interaction.channelId,
|
||||||
userId: interaction.user.id,
|
userId: interaction.user.id,
|
||||||
guildId: interaction.guildId,
|
guildId: interaction.guildId,
|
||||||
}).then((msg) =>
|
})
|
||||||
interaction.reply({
|
.then((msg) =>
|
||||||
content: msg as string,
|
interaction.reply({
|
||||||
ephemeral: true,
|
content: msg as string,
|
||||||
}),
|
ephemeral: true,
|
||||||
);
|
}),
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
interaction.reply({
|
||||||
|
content: err,
|
||||||
|
ephemeral: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// Show modal to user to get at least the time
|
// Show modal to user to get at least the time
|
||||||
const modal = new ModalBuilder()
|
const modal = new ModalBuilder()
|
||||||
|
|
|
@ -77,6 +77,7 @@
|
||||||
"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_reminder17": "Message from",
|
||||||
|
"c_reminder18": "Invalid time, try again.",
|
||||||
|
|
||||||
"c_play_name": "play",
|
"c_play_name": "play",
|
||||||
"c_play_desc": "Plays a song/playlist, no query displays the now playing song",
|
"c_play_desc": "Plays a song/playlist, no query displays the now playing song",
|
||||||
|
|
|
@ -77,6 +77,7 @@
|
||||||
"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_reminder17": "Message d'il y a",
|
||||||
|
"c_reminder18": "Temps invalide, réessayez.",
|
||||||
|
|
||||||
"c_play_name": "play",
|
"c_play_name": "play",
|
||||||
"c_play_desc": "Joue une chanson/playlist, pas de requête affiche la chanson en cours actuellement",
|
"c_play_desc": "Joue une chanson/playlist, pas de requête affiche la chanson en cours actuellement",
|
||||||
|
|
|
@ -64,8 +64,15 @@ const splitTime = (time: string) => {
|
||||||
*/
|
*/
|
||||||
export const newReminder = async (client: Client, time: string, info: infoReminder) =>
|
export const newReminder = async (client: Client, time: string, info: infoReminder) =>
|
||||||
new Promise((ok, ko) => {
|
new Promise((ok, ko) => {
|
||||||
|
const loc = getLocale(client, info.locale);
|
||||||
|
|
||||||
const data = splitTime(time);
|
const data = splitTime(time);
|
||||||
const timeout = strToSeconds(data.time);
|
const timeout = strToSeconds(data.time);
|
||||||
|
if (timeout < 0) {
|
||||||
|
ko(loc.get("c_reminder18"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const timeoutId = setTimeoutReminder(client, info, data.option, timeout);
|
const timeoutId = setTimeoutReminder(client, info, data.option, timeout);
|
||||||
|
|
||||||
// Add the remind to the db
|
// Add the remind to the db
|
||||||
|
@ -90,7 +97,6 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send confirmation to user
|
// Send confirmation to user
|
||||||
const loc = getLocale(client, info.locale);
|
|
||||||
ok(`${loc.get("c_reminder1")} ${data.time}.`);
|
ok(`${loc.get("c_reminder1")} ${data.time}.`);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -24,6 +24,11 @@ enum TimeSecond {
|
||||||
* @returns time in seconds
|
* @returns time in seconds
|
||||||
*/
|
*/
|
||||||
export const strToSeconds = (time: string) => {
|
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(
|
const regex = new RegExp(
|
||||||
`(?<${TimeSecond[TimeSecond.Year]}>[0-9]+(?=[y|a]))|(?<${
|
`(?<${TimeSecond[TimeSecond.Year]}>[0-9]+(?=[y|a]))|(?<${
|
||||||
TimeSecond[TimeSecond.Week]
|
TimeSecond[TimeSecond.Week]
|
||||||
|
@ -36,6 +41,11 @@ export const strToSeconds = (time: string) => {
|
||||||
|
|
||||||
const data = Object.assign({}, regex.exec(time.toLowerCase())?.groups);
|
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;
|
let res = 0;
|
||||||
Object.entries(data).forEach(([key, value]) => {
|
Object.entries(data).forEach(([key, value]) => {
|
||||||
if (value) {
|
if (value) {
|
||||||
|
|
Loading…
Reference in a new issue