Advertisement
Wonkiest29

Untitled

Feb 22nd, 2025 (edited)
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ```js
  2. // index.js
  3. // ...
  4. import { Player } from 'discord-player';
  5. import pkg from '@discord-player/extractor';  // I
  6. const { YouTubeExtractor, SpotifyExtractor, SoundcloudExtractor, AppleMusicExtractor } = pkg;  // Extract relevant extractors
  7.  
  8. // ...
  9. const player = new Player(client)
  10. player.extractors.register(YouTubeExtractor);
  11. player.extractors.register(SpotifyExtractor);
  12. player.extractors.register(SoundcloudExtractor);
  13. player.extractors.register(AppleMusicExtractor);
  14.  
  15. global.player = player
  16. // ...
  17. ```
  18.  
  19. ```js
  20. // play.js
  21.  
  22. import { SlashCommandBuilder } from 'discord.js';
  23.  
  24. export default {
  25.     data: new SlashCommandBuilder()
  26.         .setName('play')
  27.         .setDescription('Play music from a YouTube URL or search term')
  28.         .addStringOption(option =>
  29.             option.setName('query')
  30.                 .setDescription('The YouTube URL or search term to play')
  31.                 .setRequired(true)
  32.         ),
  33.  
  34.     async execute(interaction) {
  35.         const query = interaction.options.getString('query');
  36.         const channel = interaction.member.voice.channel;
  37.  
  38.         if (!channel) {
  39.             return interaction.reply('You need to join a voice channel first!');
  40.         }
  41.  
  42.         // Ensure the query is a valid string
  43.         if (typeof query !== 'string' || !query.trim()) {
  44.             return interaction.reply('Please provide a valid search query or URL.');
  45.         }
  46.  
  47.         try {
  48.             // Search for the track using the provided query
  49.             const result = await player.search(query, {
  50.                 requestedBy: interaction.user,
  51.                 searchEngine: 'ytsearch'
  52.             });
  53.  
  54.             // If no results are found, inform the user
  55.             if (result.tracks.length === 0) {
  56.                 return interaction.reply('No results found for the given query!');
  57.             }
  58.  
  59.             // Create a queue for the voice channel
  60.             const queue = player.nodes.create(interaction.guild, {
  61.                 metadata: {
  62.                     channel: interaction.channel,
  63.                 },
  64.             });
  65.  
  66.             // Connect to the voice channel if not already connected
  67.             if (!queue.connection) {
  68.                 await queue.connect(channel);
  69.             }
  70.  
  71.             // Add the first track to the queue
  72.             queue.addTrack(result.tracks[0]);
  73.  
  74.             // Start playing the track if the bot isn't already playing
  75.             if (!queue.isPlaying()) {
  76.                 await queue.play();
  77.             }
  78.  
  79.             // Inform the user about the track being played
  80.             return interaction.reply(`Now playing **${result.tracks[0].title}**`);
  81.         } catch (error) {
  82.             console.error('Error during play command execution:', error);
  83.             return interaction.reply('An error occurred while trying to play the track.');
  84.         }
  85.     }
  86. };
  87. ```
  88.  
  89.  
  90.  
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement