JaBa/commands/Music/queue.js

206 lines
7.5 KiB
JavaScript
Raw Normal View History

const { SlashCommandBuilder, ActionRowBuilder, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand");
class Queue extends BaseCommand {
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("queue")
.setDescription(client.translate("music/queue:DESCRIPTION"))
.setDescriptionLocalizations({
2023-07-05 00:58:06 +05:00
uk: client.translate("music/queue:DESCRIPTION", null, "uk-UA"),
ru: client.translate("music/queue:DESCRIPTION", null, "ru-RU"),
})
.setDMPermission(false),
dirname: __dirname,
ownerOnly: false,
});
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
*/
async onLoad(client) {
client.on("interactionCreate", async interaction => {
if (!interaction.isButton()) return;
if (interaction.customId.startsWith("queue_")) {
2024-04-30 11:40:52 +05:00
const locale = (await client.findOrCreateGuild(interaction.guildId)).language;
2024-02-09 23:26:57 +05:00
2024-04-29 15:57:02 +05:00
const player = client.lavalink.getPlayer(interaction.guildId);
if (!player) return interaction.error("music/play:NOT_PLAYING");
2024-04-30 11:40:52 +05:00
const { embeds, size } = generateQueueEmbeds(interaction, player, locale);
let currentPage = Number(interaction.message.content.match(/\d+/g)[0]) - 1 ?? 0;
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder().setCustomId("queue_prev_page").setStyle(ButtonStyle.Primary).setEmoji("⬅️"),
new ButtonBuilder().setCustomId("queue_next_page").setStyle(ButtonStyle.Primary).setEmoji("➡️"),
new ButtonBuilder().setCustomId("queue_jump_page").setStyle(ButtonStyle.Secondary).setEmoji("↗️"),
2024-01-08 01:54:03 +05:00
new ButtonBuilder().setCustomId("queue_stop").setStyle(ButtonStyle.Danger).setEmoji("❌"),
);
if (interaction.customId === "queue_prev_page") {
await interaction.deferUpdate();
if (currentPage !== 0) {
--currentPage;
interaction.editReply({
2024-04-30 11:40:52 +05:00
content: `${interaction.translate("common:PAGE", null, locale)}: **${currentPage + 1}**/**${size}**`,
embeds: [embeds[currentPage]],
components: [row],
});
}
} else if (interaction.customId === "queue_next_page") {
await interaction.deferUpdate();
if (currentPage < size - 1) {
currentPage++;
interaction.editReply({
2024-04-30 11:40:52 +05:00
content: `${interaction.translate("common:PAGE", null, locale)}: **${currentPage + 1}**/**${size}**`,
embeds: [embeds[currentPage]],
components: [row],
});
}
} else if (interaction.customId === "queue_jump_page") {
await interaction.deferUpdate();
const selectMenu = new StringSelectMenuBuilder()
.setCustomId("queue_select")
2024-04-30 11:40:52 +05:00
.setPlaceholder(interaction.translate("common:NOTHING_SELECTED", null, locale));
for (let i = 0; i < size; i++) {
selectMenu.addOptions(
new StringSelectMenuOptionBuilder()
.setLabel(`${i + 1}`)
.setValue(`${i}`),
);
}
const selectRow = new ActionRowBuilder().addComponents(selectMenu),
msg = await interaction.followUp({
components: [selectRow],
ephemeral: true,
});
const filter = i => i.user.id === interaction.user.id,
collected = await msg.awaitMessageComponent({ filter, time: 10 * 1000 }),
page = Number(collected.values[0]);
await collected.deferUpdate();
return interaction.editReply({
2024-04-30 11:40:52 +05:00
content: `${interaction.translate("common:PAGE", null, locale)}: **${page + 1}**/**${size}**`,
embeds: [embeds[page]],
components: [row],
});
} else if (interaction.customId === "queue_stop") {
await interaction.deferUpdate();
row.components.forEach(component => {
component.setDisabled(true);
});
return interaction.editReply({
components: [row],
});
}
}
});
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
* @param {import("discord.js").ChatInputCommandInteraction} interaction
*/
async execute(client, interaction) {
2024-04-29 15:57:02 +05:00
const player = client.lavalink.getPlayer(interaction.guildId);
if (!player) return interaction.error("music/play:NOT_PLAYING");
2024-04-29 15:57:02 +05:00
const { embeds, size } = generateQueueEmbeds(interaction, player),
row = new ActionRowBuilder().addComponents(
new ButtonBuilder().setCustomId("queue_prev_page").setStyle(ButtonStyle.Primary).setEmoji("⬅️"),
new ButtonBuilder().setCustomId("queue_next_page").setStyle(ButtonStyle.Primary).setEmoji("➡️"),
new ButtonBuilder().setCustomId("queue_jump_page").setStyle(ButtonStyle.Secondary).setEmoji("↗️"),
2024-01-08 01:54:03 +05:00
new ButtonBuilder().setCustomId("queue_stop").setStyle(ButtonStyle.Danger).setEmoji("❌"),
);
if (interaction.customId) return await interaction.followUp({
content: `${interaction.translate("common:PAGE")}: **1**/**${size}**`,
embeds: [embeds[0]],
components: [row],
});
await interaction.reply({
content: `${interaction.translate("common:PAGE")}: **1**/**${size}**`,
embeds: [embeds[0]],
components: [row],
});
}
}
/**
*
* @param {import("discord.js").ChatInputCommandInteraction} interaction
2024-04-29 15:57:02 +05:00
* @param {import("lavalink-client").Player} player
2024-04-30 11:40:52 +05:00
* @param {string} locale
* @returns
*/
2024-04-30 11:40:52 +05:00
function generateQueueEmbeds(interaction, player, locale) {
2023-10-21 14:41:32 +05:00
const embeds = [],
2024-04-29 15:57:02 +05:00
currentTrack = player.queue.current,
translated = {
2024-04-29 15:57:02 +05:00
// "3": interaction.translate("music/loop:AUTOPLAY"),
2024-04-30 11:40:52 +05:00
"queue": interaction.translate("music/loop:QUEUE", null, locale),
"track": interaction.translate("music/loop:TRACK", null, locale),
"off": interaction.translate("common:DISABLED", null, locale),
};
2023-10-21 14:41:32 +05:00
let k = 10;
2024-04-29 15:57:02 +05:00
if (!player.queue.tracks.length) {
const embed = interaction.client.embed({
2024-04-30 11:40:52 +05:00
title: interaction.translate("music/nowplaying:CURRENTLY_PLAYING", null, locale),
2024-04-29 15:57:02 +05:00
thumbnail: currentTrack.info.artworkUrl || null,
2024-04-30 11:40:52 +05:00
description: `${interaction.translate("music/nowplaying:REPEAT", null, locale)}: \`${translated[player.repeatMode]}\`\n${
currentTrack.info.uri.startsWith("./clips", null, locale) ? `${currentTrack.info.title} (clips)` : `[${currentTrack.info.title}](${currentTrack.info.uri})`
}\n> ${interaction.translate("music/queue:ADDED", null, locale)} ${currentTrack.requester.toString()}\n\n**${interaction.translate("music/queue:NEXT", null, locale)}**\n${interaction.translate("music/queue:NO_QUEUE", null, locale)}`,
});
embeds.push(embed);
return { embeds: embeds, size: embeds.length };
}
2024-04-29 15:57:02 +05:00
for (let i = 0; i < player.queue.tracks.length; i += 10) {
const current = player.queue.tracks.slice(i, k);
let j = i;
k += 10;
2024-04-30 11:40:52 +05:00
const info = current.map(track => `${++j}. ${track.info.uri.startsWith("./clips") ? `${track.info.title} (clips)` : `[${track.info.title}](${track.info.uri})`}\n> ${interaction.translate("music/queue:ADDED", null, locale)} ${track.requester.toString()}`).join("\n");
const embed = interaction.client.embed({
2024-04-30 11:40:52 +05:00
title: interaction.translate("music/nowplaying:CURRENTLY_PLAYING", null, locale),
2024-04-29 15:57:02 +05:00
thumbnail: currentTrack.info.artworkUrl || null,
2024-04-30 11:40:52 +05:00
description: `${interaction.translate("music/nowplaying:REPEAT", null, locale)}: \`${translated[player.repeatMode]}\`\n${
2024-04-29 15:57:02 +05:00
currentTrack.info.uri.startsWith("./clips") ? `${currentTrack.info.title} (clips)` : `[${currentTrack.info.title}](${currentTrack.info.uri})`
2024-04-30 11:40:52 +05:00
}\n * ${interaction.translate("music/queue:ADDED", null, locale)} ${currentTrack.requester.toString()}\n\n**${interaction.translate("music/queue:NEXT", null, locale)}**\n${info}`,
});
embeds.push(embed);
}
return { embeds: embeds, size: embeds.length };
}
2023-07-05 00:58:06 +05:00
module.exports = Queue;