* fix display of time

* add message when the list is empty
* wip: add button to update when the list is empty
* fix reminder deletion
This commit is contained in:
Mylloon 2023-01-16 18:06:30 +01:00
parent 2593f92dad
commit 5c7ede0033
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 52 additions and 29 deletions

View file

@ -219,29 +219,39 @@ export default {
user = interaction.user; user = interaction.user;
} }
const idPrec = 'reminderList-prec';
const idNext = 'reminderList-next';
const row = new ActionRowBuilder<ButtonBuilder>()
.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 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<ButtonBuilder>()
.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<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId(idUpdate)
.setLabel('Update')
.setStyle(ButtonStyle.Primary)
);
}
return await interaction.reply({ ephemeral: true, embeds: [list], components: [row] }); return await interaction.reply({ ephemeral: true, embeds: [list], components: [row] });
} }
@ -263,9 +273,9 @@ export default {
clearTimeout(reminderInfo.timeout_id); clearTimeout(reminderInfo.timeout_id);
// Delete from database // 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: { default: {
console.error(`${__filename}: unknown subcommand (${subcommand})`); console.error(`${__filename}: unknown subcommand (${subcommand})`);

View file

@ -32,7 +32,7 @@ export default async (client: Client) => {
if (element.expiration_date <= now) { if (element.expiration_date <= now) {
// Reminder expired // 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) { if (res != true) {
throw res; throw res;
} }

View file

@ -44,5 +44,7 @@
"c_reminder6": "Page ", "c_reminder6": "Page ",
"c_reminder7": "Pas de message", "c_reminder7": "Pas de message",
"c_reminder8": "Expire dans", "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 !"
} }

View file

@ -97,7 +97,7 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
* @param userId User ID who created the reminder * @param userId User ID who created the reminder
* @returns what the SQlite request sended * @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 // Delete the reminder for the database
return new Promise((ok, ko) => { return new Promise((ok, ko) => {
// Add the remind to the db // 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) => { export const setTimeoutReminder = (client: Client, info: infoReminder, option: OptionReminder, timeout: number) => {
return Number(setTimeout(() => { return Number(setTimeout(() => {
deleteReminder(client, info.createdAt, info.userId).then((val) => { deleteReminder(client, String(info.createdAt), info.userId).then((val) => {
if (val != true) { if (val != true) {
throw val; throw val;
} }
@ -267,6 +267,15 @@ const listReminders = async (client: Client, userId: string, guildId: string | n
}) as dbReminder[]; }) 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) => { export const embedListReminders = async (client: Client, user: User, guildId: string | null, page: number, local: string) => {
const loc = getLocale(client, local); const loc = getLocale(client, local);
const reminders = await listReminders(client, user.id, guildId); const reminders = await listReminders(client, user.id, guildId);
@ -303,7 +312,9 @@ export const embedListReminders = async (client: Client, user: User, guildId: st
curseur++; 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 };
}; };

View file

@ -50,5 +50,5 @@ export const strToSeconds = (time: string) => {
export const timeDeltaToString = (time: number) => { export const timeDeltaToString = (time: number) => {
const now = Date.now(); const now = Date.now();
// TODO adapt the output and not always parse the time as seconds // TODO adapt the output and not always parse the time as seconds
return `${strToSeconds(`${now - time}`)} secs`; return `${strToSeconds(`${(now - time) / 1000}`)} secs`;
}; };