JaBa/commands/Owner/servers.js

144 lines
4 KiB
JavaScript
Raw Normal View History

const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand");
class Servers extends BaseCommand {
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("servers")
.setDescription(client.translate("owner/servers:DESCRIPTION"))
.setDescriptionLocalizations({
2023-07-05 00:58:06 +05:00
uk: client.translate("owner/servers:DESCRIPTION", null, "uk-UA"),
ru: client.translate("owner/servers:DESCRIPTION", null, "ru-RU"),
})
.setDMPermission(true),
aliases: [],
dirname: __dirname,
ownerOnly: true,
});
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
*/
async onLoad() {
//...
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
* @param {import("discord.js").ChatInputCommandInteraction} interaction
2022-08-09 23:48:33 +05:00
* @param {Object} data
*/
async execute(client, interaction) {
await interaction.deferReply({ ephemeral: true });
let currentPage = 0;
const embeds = generateServersEmbeds(interaction, client.guilds.cache);
2023-07-05 00:58:06 +05:00
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder().setCustomId("servers_prev_page").setStyle(ButtonStyle.Primary).setEmoji("⬅️"),
new ButtonBuilder().setCustomId("servers_next_page").setStyle(ButtonStyle.Primary).setEmoji("➡️"),
2024-01-08 01:54:03 +05:00
new ButtonBuilder().setCustomId("servers_stop").setStyle(ButtonStyle.Danger).setEmoji("❌"),
2023-07-05 00:58:06 +05:00
);
await interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`,
embeds: [embeds[currentPage]],
components: [row],
});
const filter = i => i.user.id === interaction.user.id;
2023-07-05 00:58:06 +05:00
const collector = interaction.guild === null ? (await interaction.user.createDM()).createMessageComponentCollector({ filter, idle: 20 * 1000 }) : interaction.channel.createMessageComponentCollector({ filter, idle: 20 * 1000 });
collector.on("collect", async i => {
if (i.isButton()) {
if (i.customId === "servers_prev_page") {
i.deferUpdate();
if (currentPage !== 0) {
--currentPage;
interaction.editReply({
2022-08-30 14:26:56 +05:00
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`,
embeds: [embeds[currentPage]],
components: [row],
});
}
} else if (i.customId === "servers_next_page") {
i.deferUpdate();
if (currentPage < embeds.length - 1) {
currentPage++;
interaction.editReply({
2022-08-30 14:26:56 +05:00
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`,
embeds: [embeds[currentPage]],
components: [row],
});
}
} else if (i.customId === "servers_stop") {
i.deferUpdate();
collector.stop();
}
}
});
collector.on("end", () => {
row.components.forEach(component => {
component.setDisabled(true);
});
return interaction.editReply({
components: [row],
});
});
}
}
/**
*
* @param {import("discord.js").ChatInputCommandInteraction} interaction
* @param {Array} servers
* @returns
*/
function generateServersEmbeds(interaction, servers) {
const embeds = [];
let k = 10;
for (let i = 0; i < servers.size; i += 10) {
2023-07-05 00:58:06 +05:00
const current = servers
.sort((a, b) => b.memberCount - a.memberCount)
.map(g => g)
.slice(i, k);
let j = i;
k += 10;
2023-07-05 00:58:06 +05:00
const info = current
.map(
server =>
`${++j}. ${server.name} | ${server.memberCount} ${interaction.client.functions.getNoun(
server.memberCount,
interaction.translate("misc:NOUNS:MEMBERS:1"),
interaction.translate("misc:NOUNS:MEMBERS:2"),
interaction.translate("misc:NOUNS:MEMBERS:5"),
)}`,
)
.join("\n");
const embed = new EmbedBuilder()
.setTitle(interaction.translate("owner/servers:SERVERS_LIST"))
.setDescription(info)
2023-08-24 19:00:24 +05:00
.setColor(interaction.client.config.embed.color)
.setFooter(interaction.client.config.embed.footer)
.setTimestamp();
embeds.push(embed);
}
return embeds;
}
2023-07-05 00:58:06 +05:00
module.exports = Servers;