Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { SlashCommandBuilder } = require("@discordjs/builders");
- const { EmbedBuilder } = require("discord.js");
- const { QueryType } = require("discord-player");
- module.exports = {
- data: new SlashCommandBuilder()
- .setName("play")
- .setDescription("play a song from YouTube.")
- .addSubcommand(subcommand =>
- subcommand
- .setName("search")
- .setDescription("Searches for a song and plays it")
- .addStringOption(option =>
- option.setName("searchterms").setDescription("search keywords").setRequired(true)
- )
- )
- .addSubcommand(subcommand =>
- subcommand
- .setName("playlist")
- .setDescription("Plays a playlist from YT")
- .addStringOption(option => option.setName("url").setDescription("the playlist's url").setRequired(true))
- )
- .addSubcommand(subcommand =>
- subcommand
- .setName("song")
- .setDescription("Plays a single song from YT")
- .addStringOption(option => option.setName("url").setDescription("the song's url").setRequired(true))
- ),
- execute: async ({ client, interaction }) => {
- // Defer the reply to ensure the interaction is acknowledged
- await interaction.deferReply();
- // Make sure the user is inside a voice channel
- if (!interaction.member.voice.channel) {
- return interaction.followUp("You need to be in a Voice Channel to play a song.");
- }
- // Create a play queue for the server
- const queue = await client.player.nodes.create(interaction.guild);
- // Wait until you are connected to the channel
- if (!queue.connection) await queue.connect(interaction.member.voice.channel);
- let embed = new EmbedBuilder();
- if (interaction.options.getSubcommand() === "song") {
- let url = interaction.options.getString("url");
- // Search for the song using the discord-player
- const result = await client.player.search(url, {
- requestedBy: interaction.user,
- searchEngine: QueryType.YOUTUBE_VIDEO
- });
- // Finish if no tracks were found
- if (result.tracks.length === 0) {
- return interaction.followUp("No results");
- }
- // Add the track to the queue
- const song = result.tracks[0];
- await queue.addTrack(song);
- embed
- .setDescription(`**${song.title}** has been added to the Queue`)
- .setThumbnail(song.thumbnail)
- .setFooter({ text: `Duration: ${song.duration}` });
- }
- else if (interaction.options.getSubcommand() === "playlist") {
- let url = interaction.options.getString("url");
- const result = await client.player.search(url, {
- requestedBy: interaction.user,
- searchEngine: QueryType.YOUTUBE_PLAYLIST
- });
- if (result.tracks.length === 0) {
- return interaction.followUp(`No playlists found with ${url}`);
- }
- // Add the tracks to the queue
- const playlist = result.playlist;
- await queue.addTracks(result.tracks);
- embed
- .setDescription(`**${result.tracks.length} songs from ${playlist.title}** have been added to the Queue`)
- .setThumbnail(playlist.thumbnail);
- }
- else if (interaction.options.getSubcommand() === "search") {
- let searchTerms = interaction.options.getString("searchterms");
- const result = await client.player.search(searchTerms, {
- requestedBy: interaction.user,
- searchEngine: QueryType.AUTO
- });
- // Finish if no tracks were found
- if (result.tracks.length === 0) {
- return interaction.followUp("No results");
- }
- // Add the track to the queue
- const song = result.tracks[0];
- await queue.addTrack(song);
- embed
- .setDescription(`**${song.title}** has been added to the Queue`)
- .setThumbnail(song.thumbnail)
- .setFooter({ text: `Duration: ${song.duration}` });
- }
- // Play the song
- if (!queue.playing && queue.tracks.length > 0) {
- // Start playing the first track in the queue
- await queue.play(queue.tracks[0]);
- } else if (queue.tracks.length === 0) {
- // The queue is empty, handle this scenario appropriately
- console.log('The queue is empty.');
- return interaction.followUp('There are no songs in the queue to play.');
- }
- // Respond with the embed containing information about the player
- await interaction.editReply({
- embeds: [embed]
- });
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement