Advertisement
func_kenobi

nodejs ds bot play function

May 5th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 5.02 KB | Source Code | 0 0
  1. const { SlashCommandBuilder } = require("@discordjs/builders");
  2. const { EmbedBuilder } = require("discord.js");
  3. const { QueryType } = require("discord-player");
  4.  
  5. module.exports = {
  6.     data: new SlashCommandBuilder()
  7.         .setName("play")
  8.         .setDescription("play a song from YouTube.")
  9.         .addSubcommand(subcommand =>
  10.             subcommand
  11.                 .setName("search")
  12.                 .setDescription("Searches for a song and plays it")
  13.                 .addStringOption(option =>
  14.                     option.setName("searchterms").setDescription("search keywords").setRequired(true)
  15.                 )
  16.         )
  17.         .addSubcommand(subcommand =>
  18.             subcommand
  19.                 .setName("playlist")
  20.                 .setDescription("Plays a playlist from YT")
  21.                 .addStringOption(option => option.setName("url").setDescription("the playlist's url").setRequired(true))
  22.         )
  23.         .addSubcommand(subcommand =>
  24.             subcommand
  25.                 .setName("song")
  26.                 .setDescription("Plays a single song from YT")
  27.                 .addStringOption(option => option.setName("url").setDescription("the song's url").setRequired(true))
  28.         ),
  29.     execute: async ({ client, interaction }) => {
  30.         // Defer the reply to ensure the interaction is acknowledged
  31.         await interaction.deferReply();
  32.  
  33.         // Make sure the user is inside a voice channel
  34.         if (!interaction.member.voice.channel) {
  35.             return interaction.followUp("You need to be in a Voice Channel to play a song.");
  36.         }
  37.  
  38.         // Create a play queue for the server
  39.         const queue = await client.player.nodes.create(interaction.guild);
  40.  
  41.         // Wait until you are connected to the channel
  42.         if (!queue.connection) await queue.connect(interaction.member.voice.channel);
  43.  
  44.         let embed = new EmbedBuilder();
  45.  
  46.         if (interaction.options.getSubcommand() === "song") {
  47.             let url = interaction.options.getString("url");
  48.  
  49.             // Search for the song using the discord-player
  50.             const result = await client.player.search(url, {
  51.                 requestedBy: interaction.user,
  52.                 searchEngine: QueryType.YOUTUBE_VIDEO
  53.             });
  54.  
  55.             // Finish if no tracks were found
  56.             if (result.tracks.length === 0) {
  57.                 return interaction.followUp("No results");
  58.             }
  59.  
  60.             // Add the track to the queue
  61.             const song = result.tracks[0];
  62.             await queue.addTrack(song);
  63.             embed
  64.                 .setDescription(`**${song.title}** has been added to the Queue`)
  65.                 .setThumbnail(song.thumbnail)
  66.                 .setFooter({ text: `Duration: ${song.duration}` });
  67.         }
  68.         else if (interaction.options.getSubcommand() === "playlist") {
  69.             let url = interaction.options.getString("url");
  70.             const result = await client.player.search(url, {
  71.                 requestedBy: interaction.user,
  72.                 searchEngine: QueryType.YOUTUBE_PLAYLIST
  73.             });
  74.  
  75.             if (result.tracks.length === 0) {
  76.                 return interaction.followUp(`No playlists found with ${url}`);
  77.             }
  78.  
  79.             // Add the tracks to the queue
  80.             const playlist = result.playlist;
  81.             await queue.addTracks(result.tracks);
  82.             embed
  83.                 .setDescription(`**${result.tracks.length} songs from ${playlist.title}** have been added to the Queue`)
  84.                 .setThumbnail(playlist.thumbnail);
  85.         }
  86.         else if (interaction.options.getSubcommand() === "search") {
  87.             let searchTerms = interaction.options.getString("searchterms");
  88.             const result = await client.player.search(searchTerms, {
  89.                 requestedBy: interaction.user,
  90.                 searchEngine: QueryType.AUTO
  91.             });
  92.  
  93.             // Finish if no tracks were found
  94.             if (result.tracks.length === 0) {
  95.                 return interaction.followUp("No results");
  96.             }
  97.  
  98.             // Add the track to the queue
  99.             const song = result.tracks[0];
  100.             await queue.addTrack(song);
  101.             embed
  102.                 .setDescription(`**${song.title}** has been added to the Queue`)
  103.                 .setThumbnail(song.thumbnail)
  104.                 .setFooter({ text: `Duration: ${song.duration}` });
  105.         }
  106.  
  107.         // Play the song
  108.         if (!queue.playing && queue.tracks.length > 0) {
  109.             // Start playing the first track in the queue
  110.             await queue.play(queue.tracks[0]);
  111.                    
  112.         } else if (queue.tracks.length === 0) {
  113.             // The queue is empty, handle this scenario appropriately
  114.             console.log('The queue is empty.');
  115.             return interaction.followUp('There are no songs in the queue to play.');
  116.         }
  117.  
  118.         // Respond with the embed containing information about the player
  119.         await interaction.editReply({
  120.             embeds: [embed]
  121.         });
  122.     },
  123. };
  124.  
Tags: nodejs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement