I am making a discord music bot using the ytdl-core library. I also use node-opus and ffmpeg. So, the dependencies are not an issue.
The bot loads the song fine, its the playback thats having problem. Here is the hastebin for my code: https://hastebin.com/dokoguqoqu.js
It successfully outputs Now Playing which means there are no errors. However right after the bot joins the voice channel it never plays the song instead a second after it joins it executes the dispatcher.on(end) command and outputs Finished Playing.
The audio from the youtube video is loaded but never played.
Your use of ytdl.getInfo is not needed since ytdl() fires an event with the same data - second you should start the ytdl stream by itself and listen for errors it throws - then use it to create the dispatcher
Not able to replicate :/
Does this happen for any song for you?
@fent yes. I've tried different songs from different content creators. Still the same issue.
There might be an error with the ytdl stream. Try something like
let stream = ytdl(url, { filter: 'audioonly' });
stream.on('error', console.error);
let dispatcher = await connection.playStream(stream)
and see if there is an error.
Normally, uncaught errors will be shown in the console. But the connection.playStream() function seems to swallow any error emitted by the stream.
@fent Nope, still doesn't work. No error occurring just never plays the song.
I modified my code like you told me to
let connection = await voiceChannel.join();
let stream = ytdl(args[0], {filter: 'audioonly'})
.on("error", e => {
console.error(error);
message.channel.send("Error Occurred during playback. Try again later.");
})
let dispatcher = await connection.playStream(stream);
message.channel.send(`Now Playing: ${info.title}`);
dispatcher.on("end", e => {
message.channel.send(`Finished Playing: ${info.title}`);
voiceChannel.leave();
})
Still causes the bot to join and execute the dispatch end command which makes it leave.
There's a reference error in the error listener, variables e and error don't match.
@fent that was just a typo in the comment.
.on("error", e => {
console.error(e);
message.channel.send(....);
}
Still same issue. Joins and Leaves.
Hmm, try only running the call to ytdl without connecting to discord. To isolate the issue.
it doesn't work if i use it with discord.
As noted in #332 , the issue might be with using ytdl.getInfo() immediately followed by ytdl() on the same video. If you're getting the video info beforehand, you can use `ytdl.downloadFromInfo().
Could you try it and let me know if that works?
let stream = ytdl.downloadFromInfo(info, {filter: 'audioonly'})
@fent That fixed the playback problem. Thank You!
Most helpful comment
As noted in #332 , the issue might be with using
ytdl.getInfo()immediately followed byytdl()on the same video. If you're getting the video info beforehand, you can use `ytdl.downloadFromInfo().Could you try it and let me know if that works?