JaBa/commands/Music/play.js

125 lines
3.5 KiB
JavaScript
Raw Normal View History

const { SlashCommandBuilder, PermissionsBitField } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand");
class Play extends BaseCommand {
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("play")
.setDescription(client.translate("music/play:DESCRIPTION"))
.setDescriptionLocalizations({
2023-07-05 00:58:06 +05:00
uk: client.translate("music/play:DESCRIPTION", null, "uk-UA"),
ru: client.translate("music/play:DESCRIPTION", null, "ru-RU"),
})
.setDMPermission(false)
2023-07-05 00:58:06 +05:00
.addStringOption(option =>
option
.setName("query")
.setDescription(client.translate("music/play:QUERY"))
.setDescriptionLocalizations({
uk: client.translate("music/play:QUERY", null, "uk-UA"),
ru: client.translate("music/play:QUERY", null, "ru-RU"),
})
.setRequired(true)
.setAutocomplete(true),
),
aliases: [],
dirname: __dirname,
ownerOnly: false,
});
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
*/
async onLoad() {
//...
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} 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 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 {
2023-09-25 22:32:36 +05:00
const { queue } = await client.player.play(interaction.member.voice.channel, searchResult, {
nodeOptions: {
2024-02-02 20:49:55 +05:00
metadata: interaction,
},
selfDeaf: true,
leaveOnEnd: false,
leaveOnStop: true,
skipOnNoStream: true,
2023-06-26 17:25:17 +05:00
maxSize: 200,
maxHistorySize: 50,
});
2023-09-25 22:32:36 +05:00
interaction.editReply({
content: interaction.translate("music/play:ADDED_QUEUE", {
songName: searchResult.hasPlaylist() ? searchResult.playlist.title : searchResult.tracks[0].title,
2023-11-04 23:06:51 +05:00
}),
2023-09-25 22:32:36 +05:00
});
// TODO: Seeks currently playing D:
if (query.match(/&t=[[0-9]+/g) !== null) {
2023-09-25 22:32:36 +05:00
const time = query.match(/&t=[[0-9]+/g)[0].split("=")[1];
queue.node.seek(time * 1000);
}
}
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
* @param {import("discord.js").AutocompleteInteraction} interaction
* @returns
*/
async autocompleteRun(client, interaction) {
const query = interaction.options.getString("query");
2023-09-25 22:32:36 +05:00
if (query === "") return;
if (query.startsWith("http")) return interaction.respond([
2023-09-25 22:32:36 +05:00
{
name: "Current link",
value: query,
},
]);
const results = await client.player.search(query);
2024-01-15 23:19:59 +05:00
return results.tracks.length > 0 ? await interaction.respond(
results.tracks.slice(0, 10).map(track => ({
2023-08-06 18:56:09 +05:00
name: (`${track.author} - ${track.title}`.length >= 100) & (`${track.author} - ${track.title}`.slice(0, 80) + "...") || `${track.author} - ${track.title}`,
value: track.url,
2023-07-05 00:58:06 +05:00
})),
2024-01-15 23:19:59 +05:00
) : await interaction.respond([
{
name: "Nothing",
value: "Nothing",
},
]);
}
}
2023-07-05 00:58:06 +05:00
module.exports = Play;