JaBa/commands/Music/play.js

237 lines
6.7 KiB
JavaScript
Raw Normal View History

const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, PermissionsBitField } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand");
class Play extends BaseCommand {
/**
*
* @param {import("../base/JaBa")} client
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("play")
.setDescription(client.translate("music/play:DESCRIPTION"))
.setDMPermission(false)
.addStringOption(option => option.setName("query")
.setDescription(client.translate("music/play:QUERY"))
.setRequired(true)),
aliases: [],
dirname: __dirname,
ownerOnly: false,
});
}
/**
*
* @param {import("../../base/JaBa")} client
*/
async onLoad() {
//...
}
/**
*
* @param {import("../../base/JaBa")} 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();
2022-08-29 21:31:36 +05:00
const voice = interaction.member.voice.channel;
if (!voice) return interaction.error("music/play:NO_VOICE_CHANNEL", null, { edit: true });
const query = interaction.options.getString("query");
const perms = voice.permissionsFor(client.user);
if (!perms.has(PermissionsBitField.Flags.Connect) || !perms.has(PermissionsBitField.Flags.Speak)) return interaction.error("music/play:VOICE_CHANNEL_CONNECT", null, { edit: true });
2022-08-29 21:31:36 +05:00
try {
// eslint-disable-next-line no-var
var searchResult = await client.player.search(query, {
requestedBy: interaction.user,
});
if (!searchResult.tracks[0] || !searchResult)
return interaction.error("music/play:NO_RESULT", { query, error: "Скорее всего видео заблокировано по региону" }, { edit: true });
2022-08-29 21:31:36 +05:00
} catch (error) {
console.log(error);
2022-08-29 21:31:36 +05:00
return interaction.editReply({
content: interaction.translate("music/play:NO_RESULT", {
query,
error,
}),
2022-08-29 21:31:36 +05:00
});
}
const queue = await client.player.getQueue(interaction.guildId) || client.player.createQueue(interaction.guild, {
metadata: { channel: interaction.channel },
autoSelfDeaf: true,
leaveOnEnd: true,
leaveOnStop: true,
bufferingTimeout: 1000,
});
if (searchResult.searched) {
const row1 = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId("1")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("1⃣"),
new ButtonBuilder()
.setCustomId("2")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("2⃣"),
new ButtonBuilder()
.setCustomId("3")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("3⃣"),
new ButtonBuilder()
.setCustomId("4")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("4⃣"),
new ButtonBuilder()
.setCustomId("5")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("5⃣"),
);
const row2 = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId("6")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("6⃣"),
new ButtonBuilder()
.setCustomId("7")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("7⃣"),
new ButtonBuilder()
.setCustomId("8")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("8⃣"),
new ButtonBuilder()
.setCustomId("9")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("9⃣"),
new ButtonBuilder()
.setCustomId("10")
2022-09-02 19:37:25 +05:00
.setStyle(ButtonStyle.Secondary)
.setEmoji("🔟"),
);
const row3 = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId("search_cancel")
.setStyle(ButtonStyle.Secondary)
.setEmoji("❌"),
);
const rows = [row1, row2, row3];
const embed = new EmbedBuilder()
.setTitle(interaction.translate("music/play:RESULTS_TITLE", {
query,
}))
.setColor(client.config.embed.color)
.setDescription(searchResult.tracks.map(track => {
let views;
if (track.raw.live) views = "🔴 LIVE";
else views = new Intl.NumberFormat(interaction.client.languages.find(language => language.name === interaction.guild.data.language).moment, {
notation: "compact", compactDisplay: "short",
}).format(track.views);
2022-08-28 16:56:35 +05:00
return `${searchResult.tracks.indexOf(track) + 1}. [${track.title}](${track.url})\n> ${interaction.translate("common:VIEWS")}: **${views}**\n`;
}).join("\n"))
.setTimestamp();
await interaction.editReply({
embeds: [embed],
2022-12-16 23:10:59 +05:00
components: rows,
});
const filter = i => i.user.id === interaction.user.id;
const collector = interaction.channel.createMessageComponentCollector({ filter, idle: (30 * 1000) });
collector.on("collect", async i => {
if (i.isButton()) {
if (i.customId >= 1 && i.customId <= 10) {
i.deferUpdate();
const selected = searchResult.tracks[i.customId - 1];
queue.addTrack(selected);
try {
if (!queue.connection) await queue.connect(interaction.member.voice.channel);
if (!queue.playing) await queue.play();
interaction.editReply({
content: interaction.translate("music/play:ADDED_QUEUE", {
songName: selected.title,
}),
2022-08-29 21:31:36 +05:00
components: [],
embeds: [],
});
collector.stop();
return;
2022-08-29 21:31:36 +05:00
} catch (error) {
client.player.deleteQueue(interaction.guildId);
2022-08-29 21:31:36 +05:00
console.log(error);
return interaction.editReply({
content: interaction.translate("music/play:ERR_OCCURRED", {
error,
}),
});
}
} else if (i.customId === "search_cancel") {
i.deferUpdate();
2022-09-09 18:27:03 +05:00
interaction.editReply({
content: interaction.translate("misc:SELECT_CANCELED"),
embeds: [],
components: [],
});
2022-09-09 18:27:03 +05:00
collector.stop();
2022-09-09 18:27:03 +05:00
return;
}
}
});
collector.on("end", async (_, reason) => {
if (reason === "idle") {
rows.forEach(row => {
row.components.forEach(component => {
component.setDisabled(true);
});
});
return interaction.editReply({
2022-12-16 23:10:59 +05:00
components: rows,
});
}
});
return;
}
searchResult.playlist ? queue.addTracks(searchResult.tracks) : queue.addTrack(searchResult.tracks[0]);
try {
if (!queue.connection) await queue.connect(interaction.member.voice.channel);
if (!queue.playing) await queue.play();
interaction.editReply({
content: interaction.translate("music/play:ADDED_QUEUE", {
songName: searchResult.playlist ? searchResult.playlist.title : searchResult.tracks[0].title,
}),
});
2022-08-29 21:31:36 +05:00
} catch (error) {
client.player.deleteQueue(interaction.guildId);
2022-08-29 21:31:36 +05:00
console.log(error);
2022-08-06 22:35:01 +05:00
return interaction.editReply({
content: interaction.translate("music/play:ERR_OCCURRED", {
error,
}),
2022-08-06 22:35:01 +05:00
});
}
}
}
module.exports = Play;