From 5c7ede0033062234c6c911380ff3dfdf6e9d4551 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 16 Jan 2023 18:06:30 +0100 Subject: [PATCH] * fix display of time * add message when the list is empty * wip: add button to update when the list is empty * fix reminder deletion --- src/commands/misc/reminder.ts | 56 +++++++++++++++++++++-------------- src/events/client/ready.ts | 2 +- src/locales/fr.json | 4 ++- src/utils/reminder.ts | 17 +++++++++-- src/utils/time.ts | 2 +- 5 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/commands/misc/reminder.ts b/src/commands/misc/reminder.ts index e01db9f..7504afd 100644 --- a/src/commands/misc/reminder.ts +++ b/src/commands/misc/reminder.ts @@ -219,29 +219,39 @@ export default { user = interaction.user; } - const idPrec = 'reminderList-prec'; - const idNext = 'reminderList-next'; - - const row = new ActionRowBuilder() - .addComponents( - new ButtonBuilder() - .setCustomId(idPrec) - .setLabel('Prec') - .setStyle(ButtonStyle.Primary) - .setDisabled(true)) - .addComponents( - new ButtonBuilder() - .setCustomId(idNext) - .setLabel('Next') - .setStyle(ButtonStyle.Primary), - ); - // Buttons interactions - collect(client, interaction, idPrec); - collect(client, interaction, idNext); - const page = interaction.options.getInteger(loc_default?.get(`c_${filename}_sub2_opt2_name`) as string) ?? 1; + const { remindersExists, list } = await embedListReminders(client, user, interaction.guildId, page, interaction.locale); - const list = await embedListReminders(client, user, interaction.guildId, page, interaction.locale); + let row; + if (remindersExists) { + const idPrec = 'reminderList-prec'; + const idNext = 'reminderList-next'; + row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(idPrec) + .setLabel('Prec') + .setStyle(ButtonStyle.Primary) + .setDisabled(true)) + .addComponents( + new ButtonBuilder() + .setCustomId(idNext) + .setLabel('Next') + .setStyle(ButtonStyle.Primary), + ); + // Buttons interactions + collect(client, interaction, idPrec); + collect(client, interaction, idNext); + } else { + const idUpdate = 'reminderList-update'; + row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(idUpdate) + .setLabel('Update') + .setStyle(ButtonStyle.Primary) + ); + } return await interaction.reply({ ephemeral: true, embeds: [list], components: [row] }); } @@ -263,9 +273,9 @@ export default { clearTimeout(reminderInfo.timeout_id); // Delete from database - deleteReminder(client, Number(reminderInfo.creation_date), reminderInfo.user_id); + return deleteReminder(client, reminderInfo.creation_date, reminderInfo.user_id) + .then(() => interaction.reply({ content: `Reminder **#${id}** supprimé !`, ephemeral: true })); - return interaction.reply({ content: `Reminder **#${id}** supprimé !`, ephemeral: true }); } default: { console.error(`${__filename}: unknown subcommand (${subcommand})`); diff --git a/src/events/client/ready.ts b/src/events/client/ready.ts index 220d367..a72c0af 100644 --- a/src/events/client/ready.ts +++ b/src/events/client/ready.ts @@ -32,7 +32,7 @@ export default async (client: Client) => { if (element.expiration_date <= now) { // Reminder expired - deleteReminder(client, Number(element.creation_date), `${element.user_id}`).then((res) => { + deleteReminder(client, element.creation_date, `${element.user_id}`).then((res) => { if (res != true) { throw res; } diff --git a/src/locales/fr.json b/src/locales/fr.json index 632114d..4093ba7 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -44,5 +44,7 @@ "c_reminder6": "Page ", "c_reminder7": "Pas de message", "c_reminder8": "Expire dans", - "c_reminder9": "Fais le" + "c_reminder9": "Fais le", + "c_reminder10": "L'utilisateur n'a aucun rappel en attente ou page n°", + "c_reminder11": "vide !" } diff --git a/src/utils/reminder.ts b/src/utils/reminder.ts index 0e455b8..dfc507d 100644 --- a/src/utils/reminder.ts +++ b/src/utils/reminder.ts @@ -97,7 +97,7 @@ export const newReminder = async (client: Client, time: string, info: infoRemind * @param userId User ID who created the reminder * @returns what the SQlite request sended */ -export const deleteReminder = (client: Client, createdAt: number, userId: string) => { +export const deleteReminder = (client: Client, createdAt: string, userId: string) => { // Delete the reminder for the database return new Promise((ok, ko) => { // Add the remind to the db @@ -145,7 +145,7 @@ export const sendReminder = (client: Client, info: infoReminder, option: OptionR */ export const setTimeoutReminder = (client: Client, info: infoReminder, option: OptionReminder, timeout: number) => { return Number(setTimeout(() => { - deleteReminder(client, info.createdAt, info.userId).then((val) => { + deleteReminder(client, String(info.createdAt), info.userId).then((val) => { if (val != true) { throw val; } @@ -267,6 +267,15 @@ const listReminders = async (client: Client, userId: string, guildId: string | n }) as dbReminder[]; }; +/** + * Return the embed of the reminders + * @param client Client + * @param user User + * @param guildId Guild ID + * @param page Page requested + * @param local Lang + * @returns Pretty embed who list reminders + */ export const embedListReminders = async (client: Client, user: User, guildId: string | null, page: number, local: string) => { const loc = getLocale(client, local); const reminders = await listReminders(client, user.id, guildId); @@ -303,7 +312,9 @@ export const embedListReminders = async (client: Client, user: User, guildId: st curseur++; } }); + } else { + embed.addFields({ name: '\u200b', value: `${loc.get('c_reminder10')}${page} ${loc.get('c_reminder11')}` }); } - return embed; + return { remindersExists: reminders.length > 0 ? true : false, list: embed }; }; diff --git a/src/utils/time.ts b/src/utils/time.ts index 4618b8b..6e54c22 100644 --- a/src/utils/time.ts +++ b/src/utils/time.ts @@ -50,5 +50,5 @@ export const strToSeconds = (time: string) => { export const timeDeltaToString = (time: number) => { const now = Date.now(); // TODO adapt the output and not always parse the time as seconds - return `${strToSeconds(`${now - time}`)} secs`; + return `${strToSeconds(`${(now - time) / 1000}`)} secs`; };