Advertisement
ProgNeo

Untitled

Feb 11th, 2023
1,564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js');
  2. const client = new Discord.Client();
  3. const ytdl = require('ytdl-core');
  4. const ffmpeg = require('fluent-ffmpeg');
  5. const request = require('request');
  6.  
  7. client.on('ready', () => {
  8.   console.log(`Logged in as ${client.user.tag}!`);
  9. });
  10.  
  11. client.on('message', msg => {
  12.   if (msg.content.startsWith('!play')) {
  13.     const args = msg.content.split(' ');
  14.  
  15.     // Get the search query
  16.     const query = args.slice(1).join(' ');
  17.  
  18.     // Use the SoundCloud API to search for a track
  19.     request(`https://api.soundcloud.com/tracks?client_id=YOUR_SOUNDCLOUD_CLIENT_ID&q=${query}`, (error, response, body) => {
  20.       if (error) return console.error(error);
  21.  
  22.       const tracks = JSON.parse(body);
  23.  
  24.       if (!tracks.length) return msg.reply(`No results found for "${query}"`);
  25.  
  26.       // Get the first track in the search results
  27.       const track = tracks[0];
  28.  
  29.       // Get the voice channel of the user who sent the message
  30.       const voiceChannel = msg.member.voice.channel;
  31.  
  32.       if (!voiceChannel) return msg.reply("You need to be in a voice channel to play music!");
  33.  
  34.       // Join the voice channel
  35.       voiceChannel.join().then(connection => {
  36.         const stream = ytdl(track.stream_url + `?client_id=YOUR_SOUNDCLOUD_CLIENT_ID`, { filter: 'audioonly' });
  37.         const dispatcher = connection.play(stream);
  38.  
  39.         // Convert the stream to PCM data with ffmpeg
  40.         ffmpeg(stream)
  41.           .audioCodec('pcm_s16le')
  42.           .format('s16le')
  43.           .pipe(dispatcher);
  44.       });
  45.     });
  46.   }
  47. });
  48.  
  49. client.login('YOUR_DISCORD_BOT_TOKEN');
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement