JaBa/commands/Music/play.js
2023-03-26 18:31:19 +05:00

97 lines
No EOL
2.8 KiB
JavaScript

const { SlashCommandBuilder, 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"))
.setDescriptionLocalizations({ "uk": client.translate("music/play:DESCRIPTION", null, "uk-UA") })
.setDMPermission(false)
.addStringOption(option => option.setName("query")
.setDescription(client.translate("music/play:QUERY"))
.setDescriptionLocalizations({ "uk": client.translate("music/play:QUERY", null, "uk-UA") })
.setRequired(true)
.setAutocomplete(true)),
aliases: [],
dirname: __dirname,
ownerOnly: false,
});
}
/**
*
* @param {import("../../base/JaBa")} client
*/
async onLoad() {
//...
}
/**
*
* @param {import("../../base/JaBa")} client
* @param {import("discord.js").ChatInputCommandInteraction} interaction
* @param {Object} data
*/
async execute(client, interaction) {
await interaction.deferReply();
const query = interaction.options.getString("query"),
voice = interaction.member.voice.channel;
if (!voice) return interaction.error("music/play:NO_VOICE_CHANNEL", null, { edit: true });
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 });
const searchResult = await client.player.search(query, {
requestedBy: interaction.user,
});
if (!searchResult.hasTracks()) return interaction.error("music/play:NO_RESULT", { query }, { edit: true });
else {
client.player.play(interaction.member.voice.channel, searchResult, {
nodeOptions: {
metadata: {
channel: interaction.channel,
client,
requestedBy: interaction.user,
},
},
selfDeaf: true,
leaveOnEnd: false,
leaveOnStop: true,
skipOnNoStream: true,
bufferingTimeout: 1000,
});
}
interaction.editReply({
content: interaction.translate("music/play:ADDED_QUEUE", {
songName: searchResult.hasPlaylist() ? searchResult.playlist.title : searchResult.tracks[0].title,
}),
});
}
/**
*
* @param {import("../../base/JaBa")} client
* @param {import("discord.js").AutocompleteInteraction} interaction
* @returns
*/
async autocompleteRun(client, interaction) {
const query = interaction.options.getString("query"),
results = await client.player.search(query);
return interaction.respond(
results.tracks.slice(0, 10).map(track => ({
name: `${track.author} - ${track.title}`.slice(0, 90) + "...",
value: track.url,
}),
));
}
}
module.exports = Play;