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