I am currently creating a discord bot that will download a YouTube video into video.mp4 and play that video using the established connection, however, I keep getting this unfamiliar error that I am not sure how to interpret. The important section of my code is as follows:
console.log(video); //YouTube video id ex: 6Fw4B5ALC1o
const connection2 = joinVoiceChannel({
channelId: msg.member.voice.channel.id,
guildId: msg.guild.id,
adapterCreator: msg.channel.guild.voiceAdapterCreator
});
ytdl('https://www.youtube.com/watch?v=' + video).pipe(fs.createWriteStream('video.mp4'))
console.log("video downloaded");
const player = createAudioPlayer('video.mp4');
console.log("player created");
player.play();
The error:
/Users/sam/Desktop/DCordBot/node_modules/@discordjs/voice/dist/index.js:944
if (resource.ended) {
^
TypeError: Cannot read properties of undefined (reading 'ended')
at AudioPlayer.play (/Users/sam/Desktop/DCordBot/node_modules/@discordjs/voice/dist/index.js:944:18)
at Client.<anonymous> (/Users/sam/Desktop/DCordBot/main.js:179:16)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
The size of video.mp4 is consistently 0 bytes, which confuses me, because if I insert the link "http://www....." + "videoID" into the search bar it will bring up the YouTube video that I want. I am not sure why, but it's not downloading the video properly, does anyone have any suggestions or insight as to what the problem might be? Thanks.