From 8050abbabc2ba36b297434430116cfe682d29629 Mon Sep 17 00:00:00 2001 From: "Jonny_Bro (Nikita)" Date: Wed, 14 Aug 2024 18:52:46 +0500 Subject: [PATCH] add pagination to delete buttons in reminds --- commands/General/reminds.js | 63 ++++++++++++++++++++++--------------- events/CommandHandler.js | 3 +- index.js | 6 ++-- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/commands/General/reminds.js b/commands/General/reminds.js index c3701e25..633c231b 100644 --- a/commands/General/reminds.js +++ b/commands/General/reminds.js @@ -46,22 +46,14 @@ class Reminds extends BaseCommand { let currentPage = Number(interaction.message.content.match(/\d+/g)[0]) - 1 ?? 0; - const row2 = new ActionRowBuilder(); - - for (const key in reminds) { - row2.addComponents( - new ButtonBuilder() - .setCustomId(`reminds_delete_${key}`) - .setLabel(interaction.translate("general/reminds:DELETE", { pos: parseInt(key) + 1 })) - .setStyle(ButtonStyle.Danger), - ); - } - if (interaction.customId === "reminds_prev_page") { await interaction.deferUpdate(); if (currentPage !== 0) { --currentPage; + + const row2 = new ActionRowBuilder().addComponents(embeds[currentPage].data._buttons); + interaction.editReply({ content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`, embeds: [embeds[currentPage]], @@ -73,6 +65,9 @@ class Reminds extends BaseCommand { if (currentPage < embeds.length - 1) { currentPage++; + + const row2 = new ActionRowBuilder().addComponents(embeds[currentPage].data._buttons); + interaction.editReply({ content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`, embeds: [embeds[currentPage]], @@ -96,6 +91,9 @@ class Reminds extends BaseCommand { interaction.channel.awaitMessages({ filter, max: 1, time: 10 * 1000 }).then(collected => { if (embeds[collected.first().content - 1]) { currentPage = collected.first().content - 1; + + const row2 = new ActionRowBuilder().addComponents(embeds[currentPage].data._buttons); + interaction.editReply({ content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`, embeds: [embeds[currentPage]], @@ -118,20 +116,32 @@ class Reminds extends BaseCommand { }); return interaction.editReply({ - components: [row, row2], + components: [row], }); } else if (interaction.customId.startsWith("reminds_delete_")) { await interaction.deferUpdate(); const id = parseInt(interaction.customId.split("_")[2]); - const remindToDelete = reminds[id]; + const remindToDelete = reminds[id - 1]; interaction.data.user.reminds = reminds.filter(r => r.sendAt !== remindToDelete.sendAt); await interaction.data.user.save(); + const embeds = generateRemindsEmbeds(interaction, interaction.data.user.reminds); + + embeds.length <= 5 ? currentPage = 0 : currentPage; + + const row2 = new ActionRowBuilder().addComponents(embeds[currentPage].data._buttons); + + await interaction.editReply({ + content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`, + embeds: [embeds[currentPage]], + components: [row, row2], + }); + return interaction.followUp({ - content: `${client.customEmojis.success} | ${interaction.translate("general/reminds:DELETED", { pos: id + 1 })}`, + content: `${client.customEmojis.success} | ${interaction.translate("general/reminds:DELETED", { pos: id })}`, ephemeral: true, }); } @@ -158,21 +168,12 @@ class Reminds extends BaseCommand { new ButtonBuilder().setCustomId("reminds_stop").setStyle(ButtonStyle.Danger).setEmoji("❌"), ); - const row2 = new ActionRowBuilder(); - - for (const key in reminds) { - row2.addComponents( - new ButtonBuilder() - .setCustomId(`reminds_delete_${key}`) - .setLabel(interaction.translate("general/reminds:DELETE", { pos: parseInt(key) + 1 })) - .setStyle(ButtonStyle.Danger), - ); - } + const buttonsRow = new ActionRowBuilder().addComponents(embeds[0].data._buttons); await interaction.editReply({ content: `${interaction.translate("common:PAGE")}: **1**/**${embeds.length}**`, embeds: [embeds[0]], - components: [row, row2], + components: [row, buttonsRow], ephemeral: true, }); } @@ -203,9 +204,21 @@ function generateRemindsEmbeds(interaction, reminds) { description: info, }); + embed.data._buttons = []; + + for (const key in current) { + embed.data._buttons.push( + new ButtonBuilder() + .setCustomId(`reminds_delete_${parseInt(key) + i + 1}`) + .setLabel(interaction.translate("general/reminds:DELETE", { pos: parseInt(key) + i + 1 })) + .setStyle(ButtonStyle.Danger), + ); + } + embeds.push(embed); } + return embeds; } diff --git a/events/CommandHandler.js b/events/CommandHandler.js index fa6384b4..5f611f20 100644 --- a/events/CommandHandler.js +++ b/events/CommandHandler.js @@ -33,7 +33,8 @@ class CommandHandler extends BaseEvent { if (interaction.type !== InteractionType.ApplicationCommand && !interaction.isCommand()) return; - if (command?.dirname.includes("IAT") && interaction.guildId !== "1039187019957555252") return interaction.reply({ content: "IAT Only", ephemeral: true }); + if (command?.dirname.includes("IAT") && interaction.guildId !== "1039187019957555252") return interaction.reply({ content: "IAT only", ephemeral: true }); + if (command?.dirname.includes("beatrun") && interaction.guildId !== "1113394230002454560") return interaction.reply({ content: "Beatrun.ru only", ephemeral: true }); if (command.ownerOnly && interaction.user.id !== client.config.owner.id) return interaction.error("misc:OWNER_ONLY", null, { ephemeral: true }); if (!interaction.data.user.achievements.firstCommand.achieved) { diff --git a/index.js b/index.js index 28f23b00..a38734d5 100644 --- a/index.js +++ b/index.js @@ -19,10 +19,10 @@ const client = new Client({ })(); client - .on("disconnect", () => client.logger.warn("Bot is disconnecting...")) + .on("disconnect", () => client.logger.warn("Bot disconnected.")) .on("reconnecting", () => client.logger.warn("Bot reconnecting...")) - .on("warn", warn => console.log(warn)) - .on("error", e => console.log(e)); + .on("warn", console.log) + .on("error", console.log); process .on("unhandledRejection", e => console.log(e))