How To Make Discord Bot Play YouTube URL

I’m new to this and I wonder if there’s a way I can make my bot play specific YouTube URL so when I type s1 the bot join the room and play that URL

 if (message.content == "s1") {
     if (!message.member.voice.channel) return message.reply("You have to be in a VoiceChannel");
     message.member.voice.channel.join().then(VoiceConnection => {
         VoiceConnection.play("https://youtu.be/~~~~").on("finish", () => 
         VoiceConnection.disconnect());
         message.reply("done");
     }).catch(e => console.log(e))
 };

Answer

You need to use a package called ytdl to do this: https://www.npmjs.com/package/ytdl-core

in terminal you do npm install [email protected]

then add const ytdl = require('ytdl-core'); to the top of your js file.

then you need to change

VoiceConnection.play("https://youtu.be/~~~~").on("finish", () =>

to

VoiceConnection.play(ytdl("https://youtu.be/~~~~")).on("finish", () =>

Leave a Reply

Your email address will not be published. Required fields are marked *