From 2e1b350e151654ed6922098fa74af6f4b0d04abe Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 16 Jan 2023 18:43:07 +0100 Subject: [PATCH] add buttons logic --- src/buttons/misc/reminderList-next.ts | 40 +++++++++++++++------------ src/buttons/misc/reminderList-prec.ts | 39 +++++++++++++++----------- src/commands/misc/reminder.ts | 3 +- src/locales/fr.json | 2 +- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/buttons/misc/reminderList-next.ts b/src/buttons/misc/reminderList-next.ts index e19e134..1d23761 100644 --- a/src/buttons/misc/reminderList-next.ts +++ b/src/buttons/misc/reminderList-next.ts @@ -1,40 +1,46 @@ -import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, Colors, EmbedBuilder, MessageComponentInteraction } from 'discord.js'; +import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, MessageComponentInteraction, User } from 'discord.js'; +import { getLocale } from '../../utils/locales'; import { getFilename } from '../../utils/misc'; +import { embedListReminders } from '../../utils/reminder'; export default { data: { name: getFilename(__filename), }, - interaction: async (_: MessageComponentInteraction, client: Client) => { - // TODO: Dynamically change that - const pageMax = 1; - const page = 1; + interaction: async (interaction: MessageComponentInteraction, client: Client) => { + const loc = getLocale(client, interaction.locale); + const embed_desc = interaction.message.embeds.at(0)?.description as string; + + // Retrieve Pages + const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc)?.[0]); + let page = Number(/(?!• \s+)\d(?=\/)/gm.exec(embed_desc)?.[0]); + if (page + 1 > pageMax) { + page = 1; + } else { + page++; + } + + // Retrieve user + const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc)?.[0] as string; + const user = client.users.cache.get(userId) as User; // Fetch list - client.db; + const list = await embedListReminders(client, user, interaction.guildId, page, interaction.locale); const row = new ActionRowBuilder() .addComponents( new ButtonBuilder() .setCustomId('reminderList-prec') - .setLabel('Prec') + .setLabel(loc.get('c_reminder12')) .setStyle(ButtonStyle.Primary)) .addComponents( new ButtonBuilder() .setCustomId('reminderList-next') - .setLabel('Next') - .setStyle(ButtonStyle.Primary) - .setDisabled(page === pageMax ? true : false), + .setLabel(loc.get('c_reminder13')) + .setStyle(ButtonStyle.Primary), ); - - const list = new EmbedBuilder() - .setColor(Colors.DarkGrey) - .setTitle('List title') - .setDescription('List desc'); - return { - content: 'next', embeds: [list], components: [row], }; diff --git a/src/buttons/misc/reminderList-prec.ts b/src/buttons/misc/reminderList-prec.ts index da2bec8..71e5cf4 100644 --- a/src/buttons/misc/reminderList-prec.ts +++ b/src/buttons/misc/reminderList-prec.ts @@ -1,39 +1,46 @@ -import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, Colors, EmbedBuilder, MessageComponentInteraction } from 'discord.js'; +import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, MessageComponentInteraction, User } from 'discord.js'; +import { getLocale } from '../../utils/locales'; import { getFilename } from '../../utils/misc'; +import { embedListReminders } from '../../utils/reminder'; export default { data: { name: getFilename(__filename), }, - interaction: async (_: MessageComponentInteraction, client: Client) => { - // TODO: Dynamically change that - const page = 1; + interaction: async (interaction: MessageComponentInteraction, client: Client) => { + const loc = getLocale(client, interaction.locale); + const embed_desc = interaction.message.embeds.at(0)?.description as string; + + // Retrieve Pages + const pageMax = Number(/(\d+)(?!.*\d)/gm.exec(embed_desc)?.[0]); + let page = Number(/(?!• \s+)\d(?=\/)/gm.exec(embed_desc)?.[0]); + if (page - 1 == 0) { + page = pageMax; + } else { + page--; + } + + // Retrieve user + const userId = /(?!<@)\d+(?=>)/gm.exec(embed_desc)?.[0] as string; + const user = client.users.cache.get(userId) as User; // Fetch list - client.db; + const list = await embedListReminders(client, user, interaction.guildId, page, interaction.locale); const row = new ActionRowBuilder() .addComponents( new ButtonBuilder() .setCustomId('reminderList-prec') - .setLabel('Prec') - .setStyle(ButtonStyle.Primary) - .setDisabled(page === 1 ? true : false)) + .setLabel(loc.get('c_reminder12')) + .setStyle(ButtonStyle.Primary)) .addComponents( new ButtonBuilder() .setCustomId('reminderList-next') - .setLabel('Next') + .setLabel(loc.get('c_reminder13')) .setStyle(ButtonStyle.Primary), ); - - const list = new EmbedBuilder() - .setColor(Colors.DarkGrey) - .setTitle('List title') - .setDescription('List desc'); - return { - content: 'prec', embeds: [list], components: [row], }; diff --git a/src/commands/misc/reminder.ts b/src/commands/misc/reminder.ts index e6de2ad..b6f2313 100644 --- a/src/commands/misc/reminder.ts +++ b/src/commands/misc/reminder.ts @@ -229,8 +229,7 @@ export default { new ButtonBuilder() .setCustomId(idPrec) .setLabel(loc.get('c_reminder12')) - .setStyle(ButtonStyle.Primary) - .setDisabled(true)) + .setStyle(ButtonStyle.Primary)) .addComponents( new ButtonBuilder() .setCustomId(idNext) diff --git a/src/locales/fr.json b/src/locales/fr.json index 59f925a..b24a301 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -41,7 +41,7 @@ "c_reminder3": "Rappel non trouvé, pas sur le bon serveur ou qui ne vous appartiens pas.", "c_reminder4": "Utilisateur inconnu.", "c_reminder5": "Rappels de", - "c_reminder6": "Page ", + "c_reminder6": "Page", "c_reminder7": "Pas de message", "c_reminder8": "Expire dans", "c_reminder9": "Fais le",