Переписана servers

Не вижу смысла делать это как новую версию т.к. команда только для меня
This commit is contained in:
JonnyBro 2022-08-30 13:04:53 +05:00
parent 4a9ea0a220
commit 01d036c3d1
4 changed files with 146 additions and 109 deletions

View file

@ -36,7 +36,7 @@ class Queue extends BaseCommand {
if (!queue) return interaction.error("music/play:NOT_PLAYING"); if (!queue) return interaction.error("music/play:NOT_PLAYING");
let currentPage = 0; let currentPage = 0;
let embeds = generateQueueEmbed(interaction, queue); let embeds = generateQueueEmbeds(interaction, queue);
const row = new ActionRowBuilder() const row = new ActionRowBuilder()
.addComponents( .addComponents(
@ -63,10 +63,7 @@ class Queue extends BaseCommand {
); );
await interaction.reply({ await interaction.reply({
content: interaction.translate("music/queue:PAGE", { content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`,
current: currentPage + 1,
length: embeds.length
}),
embeds: [embeds[currentPage]], embeds: [embeds[currentPage]],
components: [row] components: [row]
}); });
@ -78,37 +75,31 @@ class Queue extends BaseCommand {
if (i.isButton()) { if (i.isButton()) {
if (i.customId === "queue_prev_page") { if (i.customId === "queue_prev_page") {
i.deferUpdate(); i.deferUpdate();
if (embeds != generateQueueEmbed(interaction, queue)) embeds = generateQueueEmbed(interaction, queue); if (embeds != generateQueueEmbeds(interaction, queue)) embeds = generateQueueEmbeds(interaction, queue);
if (currentPage !== 0) { if (currentPage !== 0) {
--currentPage; --currentPage;
interaction.editReply({ interaction.editReply({
content: interaction.translate("music/queue:PAGE", { content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`,
current: currentPage + 1,
length: embeds.length
}),
embeds: [embeds[currentPage]], embeds: [embeds[currentPage]],
components: [row] components: [row]
}); });
} }
} else if (i.customId === "queue_next_page") { } else if (i.customId === "queue_next_page") {
i.deferUpdate(); i.deferUpdate();
if (embeds != generateQueueEmbed(interaction, queue)) embeds = generateQueueEmbed(interaction, queue); if (embeds != generateQueueEmbeds(interaction, queue)) embeds = generateQueueEmbeds(interaction, queue);
if (currentPage < embeds.length - 1) { if (currentPage < embeds.length - 1) {
currentPage++; currentPage++;
interaction.editReply({ interaction.editReply({
content: interaction.translate("music/queue:PAGE", { content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`,
current: currentPage + 1,
length: embeds.length
}),
embeds: [embeds[currentPage]], embeds: [embeds[currentPage]],
components: [row] components: [row]
}); });
} }
} else if (i.customId === "queue_jump_page") { } else if (i.customId === "queue_jump_page") {
i.deferUpdate(); i.deferUpdate();
if (embeds != generateQueueEmbed(interaction, queue)) embeds = generateQueueEmbed(interaction, queue); if (embeds != generateQueueEmbeds(interaction, queue)) embeds = generateQueueEmbeds(interaction, queue);
const msg = await interaction.followUp({ const msg = await interaction.followUp({
content: interaction.translate("music/queue:PAGE_TO_JUMP", { content: interaction.translate("music/queue:PAGE_TO_JUMP", {
@ -121,14 +112,11 @@ class Queue extends BaseCommand {
return res.author.id === interaction.user.id && !isNaN(res.content); return res.author.id === interaction.user.id && !isNaN(res.content);
}; };
interaction.channel.awaitMessages({ filter, max: 1, time: (30 * 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;
interaction.editReply({ interaction.editReply({
content: interaction.translate("music/queue:PAGE", { content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`,
current: currentPage + 1,
length: embeds.length
}),
embeds: [embeds[currentPage]], embeds: [embeds[currentPage]],
components: [row] components: [row]
}); });
@ -166,7 +154,7 @@ class Queue extends BaseCommand {
* @param {import("discord-player").Queue} queue * @param {import("discord-player").Queue} queue
* @returns * @returns
*/ */
function generateQueueEmbed(interaction, queue) { function generateQueueEmbeds(interaction, queue) {
const embeds = []; const embeds = [];
const currentTrack = queue.current; const currentTrack = queue.current;
let k = 10; let k = 10;

View file

@ -1,4 +1,4 @@
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand"); const BaseCommand = require("../../base/BaseCommand");
class Servers extends BaseCommand { class Servers extends BaseCommand {
@ -31,102 +31,151 @@ class Servers extends BaseCommand {
* @param {Object} data * @param {Object} data
*/ */
async execute(client, interaction) { async execute(client, interaction) {
let i0 = 0, await interaction.deferReply({ ephemeral: true });
i1 = 10,
page = 1;
let description = `${interaction.translate("common:SERVERS")}: ${client.guilds.cache.size}\n\n` + let currentPage = 0;
client.guilds.cache const embeds = generateServersEmbeds(interaction, client.guilds.cache);
.sort((a, b) => b.memberCount - a.memberCount)
.map((r) => r) const row = new ActionRowBuilder()
.map((r, i) => `**${i + 1}** - ${r.name} | ${r.memberCount} ${client.getNoun(r.memberCount, interaction.translate("misc:NOUNS:MEMBERS:1"), interaction.translate("misc:NOUNS:MEMBERS:2"), interaction.translate("misc:NOUNS:MEMBERS:5"))}`) .addComponents(
.slice(0, 10) new ButtonBuilder()
.join("\n"); .setCustomId("servers_prev_page")
.setStyle(ButtonStyle.Primary)
.setEmoji("⬅️"),
new ButtonBuilder()
.setCustomId("servers_next_page")
.setStyle(ButtonStyle.Primary)
.setEmoji("➡️"),
new ButtonBuilder()
.setCustomId("servers_jump_page")
.setStyle(ButtonStyle.Secondary)
.setEmoji("↗️"),
new ButtonBuilder()
.setCustomId("servers_stop")
.setStyle(ButtonStyle.Danger)
.setEmoji("⏹️"),
);
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;
const collector = 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({
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({
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`,
embeds: [embeds[currentPage]],
components: [row]
});
}
} else if (i.customId === "servers_jump_page") {
i.deferUpdate();
const msg = await interaction.followUp({
content: interaction.translate("music/queue:PAGE_TO_JUMP", {
length: embeds.length
}),
fetchReply: true
});
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;
interaction.editReply({
content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`,
embeds: [embeds[currentPage]],
components: [row]
});
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;
}
});
} 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 {*} servers
* @returns
*/
function generateServersEmbeds(interaction, servers) {
const embeds = [];
let k = 10;
for (let i = 0; i < servers.size; i += 10) {
const current = servers.map(g => g).slice(i, k).sort((a, b) => b.memberCount - a.memberCount);
let j = i;
k += 10;
const info = current.map(server => `${++j}. ${server.name} | ${server.memberCount} ${interaction.client.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() const embed = new EmbedBuilder()
.setAuthor({ .setColor(interaction.client.config.embed.color)
name: interaction.user.tag,
iconURL: interaction.member.displayAvatarURL({
extension: "png",
size: 512
})
})
.setColor(client.config.embed.color)
.setFooter({ .setFooter({
text: client.user.username text: interaction.client.config.embed.footer
}) })
.setTitle(`${interaction.translate("common:PAGE")}: ${page}/${client.guilds.cache.size}`) .setTitle(interaction.translate("owner/servers:SERVERS_LIST"))
.setDescription(description); .setDescription(info)
.setTimestamp();
await interaction.reply({ embeds.push(embed);
embeds: [embed],
});
const msg = await interaction.fetchReply();
await msg.react("⬅");
await msg.react("➡");
await msg.react("❌");
const collector = msg.createReactionCollector({
time: (2 * 1000)
});
collector.on("collect", async reaction => {
if (reaction._emoji.name === "⬅") {
i0 = i0 - 10;
i1 = i1 - 10;
page = page - 1;
if (i0 < 0) return msg.delete();
if (!i0 || !i1) return msg.delete();
description = `${interaction.translate("common:SERVERS")}: ${client.guilds.cache.size}\n\n` +
client.guilds.cache
.sort((a, b) => b.memberCount - a.memberCount)
.map(r => r)
.map((r, i) => `**${i + 1}** - ${r.name} | ${r.memberCount} ${client.getNoun(r.memberCount, interaction.translate("misc:NOUNS:MEMBERS:1"), interaction.translate("misc:NOUNS:MEMBERS:2"), interaction.translate("misc:NOUNS:MEMBERS:5"))}`)
.slice(i0, i1)
.join("\n");
embed
.setTitle(`${interaction.translate("common:PAGE")}: ${page}/${client.guilds.cache.size}`)
.setDescription(description);
msg.edit({
embeds: [embed]
});
} }
if (reaction._emoji.name === "➡") { return embeds;
i0 = i0 + 10;
i1 = i1 + 10;
page = page + 1;
if (i1 > client.guilds.cache.size + 10) return msg.delete();
if (!i0 || !i1) return msg.delete();
description = `${interaction.translate("common:SERVERS")}: ${client.guilds.cache.size}\n\n` +
client.guilds.cache
.sort((a, b) => b.memberCount - a.memberCount)
.map((r) => r)
.map((r, i) => `**${i + 1}** - ${r.name} | ${r.memberCount} ${client.getNoun(r.memberCount, interaction.translate("misc:NOUNS:MEMBERS:1"), interaction.translate("misc:NOUNS:MEMBERS:2"), interaction.translate("misc:NOUNS:MEMBERS:5"))}`)
.slice(i0, i1)
.join("\n");
embed.setTitle(`${interaction.translate("common:PAGE")}: ${page}/${Math.round(client.guilds.cache.size / 10)}`)
.setDescription(description);
msg.edit({
embeds: [embed]
});
} }
if (reaction._emoji.name === "❌") return msg.delete(); // `${interaction.translate("common:SERVERS")}: ${interaction.client.guilds.cache.size}\n\n` +
// interaction.client.guilds.cache
await reaction.users.remove(interaction.member.id); // .sort((a, b) => b.memberCount - a.memberCount)
}); // .map(g => g)
} // .map((g, i) => `${i + 1}. ${g.name} | ${g.memberCount} ${interaction.client.getNoun(g.memberCount, interaction.translate("misc:NOUNS:MEMBERS:1"), interaction.translate("misc:NOUNS:MEMBERS:2"), interaction.translate("misc:NOUNS:MEMBERS:5"))}`)
} // .slice(i, k)
// .join("\n")
module.exports = Servers; module.exports = Servers;

View file

@ -3,7 +3,6 @@
"USAGE": "", "USAGE": "",
"EXAMPLES": "queue", "EXAMPLES": "queue",
"QUEUE": "Очередь", "QUEUE": "Очередь",
"PAGE": "Страница **{{current}}**/**{{length}}**",
"NO_QUEUE": "Пусто", "NO_QUEUE": "Пусто",
"DURATION": "Длительность очереди", "DURATION": "Длительность очереди",
"ADDED": "Добавил", "ADDED": "Добавил",

View file

@ -1,5 +1,6 @@
{ {
"DESCRIPTION": "Показать список серверов бота", "DESCRIPTION": "Показать список серверов бота",
"USAGE": "", "USAGE": "",
"EXAMPLES": "servers" "EXAMPLES": "servers",
"SERVERS_LIST": "Список серверов"
} }