JaBa/commands/Music/play.js

115 lines
4 KiB
JavaScript
Raw Normal View History

2024-09-19 23:58:06 +05:00
const { SlashCommandBuilder, PermissionsBitField, InteractionContextType, ApplicationIntegrationType } = require("discord.js"),
2024-11-30 21:42:23 +05:00
{ QueryType } = require("discord-player"),
{ YoutubeiExtractor } = require("discord-player-youtubei");
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"),
})
.setIntegrationTypes([ApplicationIntegrationType.GuildInstall])
.setContexts([InteractionContextType.Guild])
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"),
})
2024-04-30 12:19:44 +05:00
.setRequired(true)
.setAutocomplete(true),
2023-07-05 00:58:06 +05:00
),
dirname: __dirname,
ownerOnly: false,
});
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
* @param {import("discord.js").ChatInputCommandInteraction} interaction
*/
async execute(client, interaction) {
await interaction.deferReply();
2022-08-29 21:31:36 +05:00
const query = interaction.options.getString("query"),
2024-04-30 12:19:44 +05:00
voice = interaction.member.voice.channel;
if (!voice) return interaction.error("music/play:NO_VOICE_CHANNEL", null, { edit: true });
2024-04-30 12:19:44 +05:00
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 });
2024-04-30 12:19:44 +05:00
const searchResult = await client.player.search(query, {
requestedBy: interaction.member,
});
if (!searchResult.hasTracks()) return interaction.error("music/play:NO_RESULT", { query }, { edit: true, ephemeral: true });
else {
2024-04-30 12:19:44 +05:00
await client.player.play(voice, searchResult, {
nodeOptions: {
metadata: interaction,
},
selfDeaf: true,
leaveOnEnd: false,
leaveOnStop: true,
skipOnNoStream: true,
maxSize: 100,
maxHistorySize: 50,
});
interaction.editReply({
content: interaction.translate("music/play:ADDED_QUEUE", {
songName: searchResult.hasPlaylist() ? searchResult.playlist.title : `${searchResult.tracks[0].title} - ${searchResult.tracks[0].author}`,
}),
});
}
}
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
* @param {import("discord.js").AutocompleteInteraction} interaction
* @returns
*/
2024-04-30 12:19:44 +05:00
async autocompleteRun(client, interaction) {
const query = interaction.options.getString("query");
if (query === "" || query === null) return interaction.respond([ { name: "No Query Provided", value: "" } ]);
if (query.startsWith("http")) return interaction.respond([ { name: "Current Link", value: query } ]);
2024-04-30 12:19:44 +05:00
2024-11-30 21:42:23 +05:00
const youtubeResults = await client.player.search(query, { searchEngine: `ext:${YoutubeiExtractor.identifier}` });
2024-07-21 21:58:02 +05:00
const spotifyResults = await client.player.search(query, { searchEngine: QueryType.SPOTIFY_SEARCH });
2024-04-30 12:19:44 +05:00
const tracks = [];
youtubeResults.tracks
.slice(0, 5)
.map(t => ({
name: `YouTube: ${`${t.title} - ${t.author} (${t.duration})`.length > 75 ? `${`${t.title} - ${t.author}`.substring(0, 75)}... (${t.duration})` : `${t.title} - ${t.author} (${t.duration})`}`,
value: t.url,
}))
.forEach(t => tracks.push({ name: t.name, value: t.value }));
2024-07-21 21:58:02 +05:00
spotifyResults.tracks
.slice(0, 5)
.map(t => ({
name: `Spotify: ${`${t.title} - ${t.author} (${t.duration})`.length > 75 ? `${`${t.title} - ${t.author}`.substring(0, 75)}... (${t.duration})` : `${t.title} - ${t.author} (${t.duration})`}`,
value: t.url,
}))
.forEach(t => tracks.push({ name: t.name, value: t.value }));
2024-04-30 12:19:44 +05:00
return interaction.respond(tracks);
}
}
2023-07-05 00:58:06 +05:00
module.exports = Play;