JaBa/commands/General/reminds.js

234 lines
7.8 KiB
JavaScript
Raw Normal View History

2024-09-19 23:58:06 +05:00
const { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, InteractionContextType, ApplicationIntegrationType } = require("discord.js");
2024-03-30 12:06:19 +05:00
const BaseCommand = require("../../base/BaseCommand");
class Reminds extends BaseCommand {
/**
*
* @param {import("../base/Client")} client
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("reminds")
.setDescription(client.translate("general/reminds:DESCRIPTION"))
.setDescriptionLocalizations({
uk: client.translate("general/reminds:DESCRIPTION", null, "uk-UA"),
ru: client.translate("general/reminds:DESCRIPTION", null, "ru-RU"),
})
2024-09-19 23:58:06 +05:00
.setIntegrationTypes([ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall])
2024-11-10 17:28:24 +05:00
.setContexts([InteractionContextType.BotDM, InteractionContextType.PrivateChannel, InteractionContextType.Guild]),
2024-03-30 12:06:19 +05:00
dirname: __dirname,
ownerOnly: false,
});
}
/**
*
* @param {import("../../base/Client")} client
*/
async onLoad(client) {
client.on("interactionCreate", async interaction => {
if (!interaction.isButton()) return;
2024-08-13 18:52:55 +05:00
if (!interaction.customId.startsWith("reminds_")) return;
2024-03-30 12:06:19 +05:00
2024-08-13 18:52:55 +05:00
interaction.data = [];
interaction.data.guild = await client.getGuildData(interaction.guildId);
interaction.data.user = await client.getUserData(interaction.user.id);
2024-03-30 12:06:19 +05:00
2024-08-13 18:52:55 +05:00
const reminds = interaction.data.user.reminds,
embeds = generateRemindsEmbeds(interaction, reminds);
2024-03-30 12:06:19 +05:00
2024-08-13 18:52:55 +05:00
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder().setCustomId("reminds_prev_page").setStyle(ButtonStyle.Primary).setEmoji("⬅️"),
new ButtonBuilder().setCustomId("reminds_next_page").setStyle(ButtonStyle.Primary).setEmoji("➡️"),
new ButtonBuilder().setCustomId("reminds_jump_page").setStyle(ButtonStyle.Secondary).setEmoji("↗️"),
new ButtonBuilder().setCustomId("reminds_stop").setStyle(ButtonStyle.Danger).setEmoji("❌"),
);
2024-03-30 12:06:19 +05:00
2024-08-13 18:52:55 +05:00
let currentPage = Number(interaction.message.content.match(/\d+/g)[0]) - 1 ?? 0;
2024-03-30 12:06:19 +05:00
2024-08-13 18:52:55 +05:00
if (interaction.customId === "reminds_prev_page") {
await interaction.deferUpdate();
2024-03-30 12:06:19 +05:00
2024-08-13 18:52:55 +05:00
if (currentPage !== 0) {
--currentPage;
const row2 = new ActionRowBuilder().addComponents(embeds[currentPage].data._buttons);
2024-08-13 18:52:55 +05:00
interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`,
embeds: [embeds[currentPage]],
components: [row, row2],
});
}
} else if (interaction.customId === "reminds_next_page") {
await interaction.deferUpdate();
if (currentPage < embeds.length - 1) {
currentPage++;
const row2 = new ActionRowBuilder().addComponents(embeds[currentPage].data._buttons);
2024-08-13 18:52:55 +05:00
interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`,
embeds: [embeds[currentPage]],
components: [row, row2],
});
}
} else if (interaction.customId === "reminds_jump_page") {
await interaction.deferUpdate();
const msg = await interaction.followUp({
content: interaction.translate("misc:JUMP_TO_PAGE", {
length: embeds.length,
}),
fetchReply: true,
2024-11-10 17:28:24 +05:00
ephemeral: true,
2024-08-13 18:52:55 +05:00
});
const filter = res => {
return res.author.id === interaction.user.id && !isNaN(res.content);
};
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);
2024-03-30 12:06:19 +05:00
interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`,
embeds: [embeds[currentPage]],
2024-08-13 18:52:55 +05:00
components: [row, row2],
2024-03-30 12:06:19 +05:00
});
2024-08-13 18:52:55 +05:00
if (collected.first().deletable) collected.first().delete();
if (msg.deletable) msg.delete();
} else {
if (collected.first().deletable) collected.first().delete();
if (msg.deletable) msg.delete();
return;
2024-03-30 12:06:19 +05:00
}
2024-08-13 18:52:55 +05:00
});
} else if (interaction.customId === "reminds_stop") {
await interaction.deferUpdate();
2024-03-30 12:06:19 +05:00
2024-08-13 18:52:55 +05:00
row.components.forEach(component => {
component.setDisabled(true);
});
2024-03-30 12:06:19 +05:00
2024-08-13 18:52:55 +05:00
return interaction.editReply({
components: [row],
2024-08-13 18:52:55 +05:00
});
} else if (interaction.customId.startsWith("reminds_delete_")) {
await interaction.deferUpdate();
2024-03-30 12:06:19 +05:00
2024-08-13 18:52:55 +05:00
const id = parseInt(interaction.customId.split("_")[2]);
const remindToDelete = reminds[id - 1];
2024-08-13 18:52:55 +05:00
interaction.data.user.reminds = reminds.filter(r => r.sendAt !== remindToDelete.sendAt);
await interaction.data.user.save();
const embeds = generateRemindsEmbeds(interaction, interaction.data.user.reminds);
2024-08-18 13:12:49 +05:00
if (embeds.length === 0) return interaction.editReply({
content: interaction.translate("general/reminds:NO_REMINDS"),
embeds: [],
components: [],
});
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],
});
2024-08-13 18:52:55 +05:00
return interaction.followUp({
content: `${client.customEmojis.success} | ${interaction.translate("general/reminds:DELETED", { pos: id })}`,
2024-08-13 18:52:55 +05:00
ephemeral: true,
});
2024-03-30 12:06:19 +05:00
}
});
}
/**
*
* @param {import("../../base/Client")} client
* @param {import("discord.js").ChatInputCommandInteraction} interaction
*/
async execute(client, interaction) {
2024-11-10 17:28:24 +05:00
await interaction.deferReply({ ephemeral: true });
2024-03-30 12:06:19 +05:00
const reminds = interaction.data.user.reminds;
if (reminds.length === 0) return interaction.error("general/reminds:NO_REMINDS", null, { edit: true });
const embeds = generateRemindsEmbeds(interaction, reminds);
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder().setCustomId("reminds_prev_page").setStyle(ButtonStyle.Primary).setEmoji("⬅️"),
new ButtonBuilder().setCustomId("reminds_next_page").setStyle(ButtonStyle.Primary).setEmoji("➡️"),
new ButtonBuilder().setCustomId("reminds_jump_page").setStyle(ButtonStyle.Secondary).setEmoji("↗️"),
new ButtonBuilder().setCustomId("reminds_stop").setStyle(ButtonStyle.Danger).setEmoji("❌"),
);
const buttonsRow = new ActionRowBuilder().addComponents(embeds[0].data._buttons);
2024-08-13 18:52:55 +05:00
2024-03-30 12:06:19 +05:00
await interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **1**/**${embeds.length}**`,
embeds: [embeds[0]],
components: [row, buttonsRow],
ephemeral: true,
2024-03-30 12:06:19 +05:00
});
}
}
/**
*
* @param {import("discord.js").ChatInputCommandInteraction} interaction
* @param {Array} reminds
* @returns
*/
function generateRemindsEmbeds(interaction, reminds) {
const embeds = [];
let k = 5;
for (let i = 0; i < reminds.length; i += 5) {
const current = reminds
.sort((a, b) => a.createdAt - b.createdAt)
.map(g => g)
.slice(i, k);
let j = i;
k += 5;
const info = current.map(r => `${++j}. ${interaction.client.translate("general/remindme:EMBED_CREATED")}: <t:${r.createdAt}:f>\n${interaction.client.translate("general/remindme:EMBED_TIME")}: <t:${r.sendAt}:f>\n${interaction.client.translate("common:MESSAGE")}: \`${r.message}\``).join("\n");
const embed = interaction.client.embed({
title: interaction.translate("general/reminds:REMINDS_LIST"),
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),
);
}
2024-03-30 12:06:19 +05:00
embeds.push(embed);
}
2024-03-30 12:06:19 +05:00
return embeds;
}
module.exports = Reminds;