add pagination to delete buttons in reminds

This commit is contained in:
Jonny_Bro (Nikita) 2024-08-14 18:52:46 +05:00
parent 05f49331e0
commit 8050abbabc
Signed by: jonny_bro
GPG key ID: 3F1ECC04147E9BD8
3 changed files with 43 additions and 29 deletions

View file

@ -46,22 +46,14 @@ class Reminds extends BaseCommand {
let currentPage = Number(interaction.message.content.match(/\d+/g)[0]) - 1 ?? 0; 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") { if (interaction.customId === "reminds_prev_page") {
await interaction.deferUpdate(); await interaction.deferUpdate();
if (currentPage !== 0) { if (currentPage !== 0) {
--currentPage; --currentPage;
const row2 = new ActionRowBuilder().addComponents(embeds[currentPage].data._buttons);
interaction.editReply({ interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`, content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`,
embeds: [embeds[currentPage]], embeds: [embeds[currentPage]],
@ -73,6 +65,9 @@ class Reminds extends BaseCommand {
if (currentPage < embeds.length - 1) { if (currentPage < embeds.length - 1) {
currentPage++; currentPage++;
const row2 = new ActionRowBuilder().addComponents(embeds[currentPage].data._buttons);
interaction.editReply({ interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`, content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`,
embeds: [embeds[currentPage]], embeds: [embeds[currentPage]],
@ -96,6 +91,9 @@ class Reminds extends BaseCommand {
interaction.channel.awaitMessages({ filter, max: 1, time: 10 * 1000 }).then(collected => { interaction.channel.awaitMessages({ filter, max: 1, time: 10 * 1000 }).then(collected => {
if (embeds[collected.first().content - 1]) { if (embeds[collected.first().content - 1]) {
currentPage = collected.first().content - 1; currentPage = collected.first().content - 1;
const row2 = new ActionRowBuilder().addComponents(embeds[currentPage].data._buttons);
interaction.editReply({ interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`, content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`,
embeds: [embeds[currentPage]], embeds: [embeds[currentPage]],
@ -118,20 +116,32 @@ class Reminds extends BaseCommand {
}); });
return interaction.editReply({ return interaction.editReply({
components: [row, row2], components: [row],
}); });
} else if (interaction.customId.startsWith("reminds_delete_")) { } else if (interaction.customId.startsWith("reminds_delete_")) {
await interaction.deferUpdate(); await interaction.deferUpdate();
const id = parseInt(interaction.customId.split("_")[2]); 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); interaction.data.user.reminds = reminds.filter(r => r.sendAt !== remindToDelete.sendAt);
await interaction.data.user.save(); 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({ 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, ephemeral: true,
}); });
} }
@ -158,21 +168,12 @@ class Reminds extends BaseCommand {
new ButtonBuilder().setCustomId("reminds_stop").setStyle(ButtonStyle.Danger).setEmoji("❌"), new ButtonBuilder().setCustomId("reminds_stop").setStyle(ButtonStyle.Danger).setEmoji("❌"),
); );
const row2 = new ActionRowBuilder(); const buttonsRow = new ActionRowBuilder().addComponents(embeds[0].data._buttons);
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),
);
}
await interaction.editReply({ await interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **1**/**${embeds.length}**`, content: `${interaction.translate("common:PAGE")}: **1**/**${embeds.length}**`,
embeds: [embeds[0]], embeds: [embeds[0]],
components: [row, row2], components: [row, buttonsRow],
ephemeral: true, ephemeral: true,
}); });
} }
@ -203,9 +204,21 @@ function generateRemindsEmbeds(interaction, reminds) {
description: info, 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); embeds.push(embed);
} }
return embeds; return embeds;
} }

View file

@ -33,7 +33,8 @@ class CommandHandler extends BaseEvent {
if (interaction.type !== InteractionType.ApplicationCommand && !interaction.isCommand()) return; 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 (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) { if (!interaction.data.user.achievements.firstCommand.achieved) {

View file

@ -19,10 +19,10 @@ const client = new Client({
})(); })();
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("reconnecting", () => client.logger.warn("Bot reconnecting..."))
.on("warn", warn => console.log(warn)) .on("warn", console.log)
.on("error", e => console.log(e)); .on("error", console.log);
process process
.on("unhandledRejection", e => console.log(e)) .on("unhandledRejection", e => console.log(e))