From 01d036c3d14fba16460b9300b3a8acaf67b7df76 Mon Sep 17 00:00:00 2001 From: JonnyBro Date: Tue, 30 Aug 2022 13:04:53 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=BF=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0=20servers=20=D0=9D=D0=B5=20=D0=B2=D0=B8?= =?UTF-8?q?=D0=B6=D1=83=20=D1=81=D0=BC=D1=8B=D1=81=D0=BB=D0=B0=20=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20=D1=8D=D1=82=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=B0=D0=BA=20=D0=BD=D0=BE=D0=B2=D1=83=D1=8E=20=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D1=81=D0=B8=D1=8E=20=D1=82.=D0=BA.=20=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=B4=D0=B0=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE?= =?UTF-8?q?=20=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=B5=D0=BD=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/Music/queue.js | 32 ++--- commands/Owner/servers.js | 219 ++++++++++++++++++----------- languages/ru-RU/music/queue.json | 1 - languages/ru-RU/owner/servers.json | 3 +- 4 files changed, 146 insertions(+), 109 deletions(-) diff --git a/commands/Music/queue.js b/commands/Music/queue.js index 3ba13b9e..e84fea14 100644 --- a/commands/Music/queue.js +++ b/commands/Music/queue.js @@ -36,7 +36,7 @@ class Queue extends BaseCommand { if (!queue) return interaction.error("music/play:NOT_PLAYING"); let currentPage = 0; - let embeds = generateQueueEmbed(interaction, queue); + let embeds = generateQueueEmbeds(interaction, queue); const row = new ActionRowBuilder() .addComponents( @@ -63,10 +63,7 @@ class Queue extends BaseCommand { ); await interaction.reply({ - content: interaction.translate("music/queue:PAGE", { - current: currentPage + 1, - length: embeds.length - }), + content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`, embeds: [embeds[currentPage]], components: [row] }); @@ -78,37 +75,31 @@ class Queue extends BaseCommand { if (i.isButton()) { if (i.customId === "queue_prev_page") { i.deferUpdate(); - if (embeds != generateQueueEmbed(interaction, queue)) embeds = generateQueueEmbed(interaction, queue); + if (embeds != generateQueueEmbeds(interaction, queue)) embeds = generateQueueEmbeds(interaction, queue); if (currentPage !== 0) { --currentPage; interaction.editReply({ - content: interaction.translate("music/queue:PAGE", { - current: currentPage + 1, - length: embeds.length - }), + content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`, embeds: [embeds[currentPage]], components: [row] }); } } else if (i.customId === "queue_next_page") { 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) { currentPage++; interaction.editReply({ - content: interaction.translate("music/queue:PAGE", { - current: currentPage + 1, - length: embeds.length - }), + content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`, embeds: [embeds[currentPage]], components: [row] }); } } else if (i.customId === "queue_jump_page") { 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({ 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); }; - 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]) { currentPage = collected.first().content - 1; interaction.editReply({ - content: interaction.translate("music/queue:PAGE", { - current: currentPage + 1, - length: embeds.length - }), + content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/${embeds.length}`, embeds: [embeds[currentPage]], components: [row] }); @@ -166,7 +154,7 @@ class Queue extends BaseCommand { * @param {import("discord-player").Queue} queue * @returns */ -function generateQueueEmbed(interaction, queue) { +function generateQueueEmbeds(interaction, queue) { const embeds = []; const currentTrack = queue.current; let k = 10; diff --git a/commands/Owner/servers.js b/commands/Owner/servers.js index 9a05e13c..1b97daba 100644 --- a/commands/Owner/servers.js +++ b/commands/Owner/servers.js @@ -1,4 +1,4 @@ -const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); +const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js"); const BaseCommand = require("../../base/BaseCommand"); class Servers extends BaseCommand { @@ -31,102 +31,151 @@ class Servers extends BaseCommand { * @param {Object} data */ async execute(client, interaction) { - let i0 = 0, - i1 = 10, - page = 1; + await interaction.deferReply({ ephemeral: true }); - let 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(0, 10) - .join("\n"); + let currentPage = 0; + const embeds = generateServersEmbeds(interaction, client.guilds.cache); - const embed = new EmbedBuilder() - .setAuthor({ - name: interaction.user.tag, - iconURL: interaction.member.displayAvatarURL({ - extension: "png", - size: 512 - }) - }) - .setColor(client.config.embed.color) - .setFooter({ - text: client.user.username - }) - .setTitle(`${interaction.translate("common:PAGE")}: ${page}/${client.guilds.cache.size}`) - .setDescription(description); + 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("➡️"), + new ButtonBuilder() + .setCustomId("servers_jump_page") + .setStyle(ButtonStyle.Secondary) + .setEmoji("↗️"), + new ButtonBuilder() + .setCustomId("servers_stop") + .setStyle(ButtonStyle.Danger) + .setEmoji("⏹️"), + ); - await interaction.reply({ - embeds: [embed], + await interaction.editReply({ + content: `${interaction.translate("common:PAGE")}: **${currentPage + 1}**/**${embeds.length}**`, + embeds: [embeds[currentPage]], + components: [row] }); - const msg = await interaction.fetchReply(); + const filter = i => i.user.id === interaction.user.id; + const collector = interaction.channel.createMessageComponentCollector({ filter, idle: (20 * 1000) }); - await msg.react("⬅"); - await msg.react("➡"); - await msg.react("❌"); + collector.on("collect", async i => { + if (i.isButton()) { + if (i.customId === "servers_prev_page") { + i.deferUpdate(); - const collector = msg.createReactionCollector({ - time: (2 * 1000) + 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("collect", async reaction => { - if (reaction._emoji.name === "⬅") { - i0 = i0 - 10; - i1 = i1 - 10; - page = page - 1; + collector.on("end", () => { + row.components.forEach(component => { + component.setDisabled(true); + }); - 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 === "➡") { - 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(); - - await reaction.users.remove(interaction.member.id); + 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() + .setColor(interaction.client.config.embed.color) + .setFooter({ + text: interaction.client.config.embed.footer + }) + .setTitle(interaction.translate("owner/servers:SERVERS_LIST")) + .setDescription(info) + .setTimestamp(); + embeds.push(embed); + } + + return embeds; +} + +// `${interaction.translate("common:SERVERS")}: ${interaction.client.guilds.cache.size}\n\n` + +// interaction.client.guilds.cache +// .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; \ No newline at end of file diff --git a/languages/ru-RU/music/queue.json b/languages/ru-RU/music/queue.json index bddc0976..37b2eb80 100644 --- a/languages/ru-RU/music/queue.json +++ b/languages/ru-RU/music/queue.json @@ -3,7 +3,6 @@ "USAGE": "", "EXAMPLES": "queue", "QUEUE": "Очередь", - "PAGE": "Страница **{{current}}**/**{{length}}**", "NO_QUEUE": "Пусто", "DURATION": "Длительность очереди", "ADDED": "Добавил", diff --git a/languages/ru-RU/owner/servers.json b/languages/ru-RU/owner/servers.json index 0da6cacf..f8c1f70f 100644 --- a/languages/ru-RU/owner/servers.json +++ b/languages/ru-RU/owner/servers.json @@ -1,5 +1,6 @@ { "DESCRIPTION": "Показать список серверов бота", "USAGE": "", - "EXAMPLES": "servers" + "EXAMPLES": "servers", + "SERVERS_LIST": "Список серверов" } \ No newline at end of file