discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.35k stars 3.97k forks source link

StreamDispatcher seems to want to occasionally immediately finish when reading a ytdl stream #5014

Closed superischroma closed 3 years ago

superischroma commented 3 years ago

Please describe the problem you are having in as much detail as possible: with some songs that i play through the StreamDispatcher (seems to be ones that have silences at the beginning), the dispatcher will almost immediately finish and throw an error. from what i've seen, these are the two errors that i'm receiving from calling the "debug" and "error" events of the dispatcher this: Error: ffmpeg stream: write EPIPE and this: Error [ERR_STREAM_DESTROYED]: ffmpeg stream: Cannot call write after a stream was destroyed

Include a reproducible code sample here, if possible:

/**
* @param {Discord.VoiceConnection} connection 
* @param {String} url 
*/
async function run(connection, url) // assuming these two variables aren't null
{
    const video = ytdl(url, { highWaterMark: 5242880 });
    video.on("error", (err) => console.log(err));
    let dispatcher = connection.play(video);
    dispatcher.on("finish", () => console.log("finished"));
    break;
}
// recommended url: https://www.youtube.com/watch?v=Auk1oVI2Icw
// (this is one i've been using to test this issue)

original code from my project plus some comments from guiding:

async play() // this is my function for playing music on the bot
{
    if (this.queue.length === 0) return;
    if (!this.guild.me.voice || !this.guild.me.voice.channel) return;
    let connection = this.guild.me.voice.connection;
    if (!connection) return;
    if (connection.dispatcher) return;
    let track = this.queue[0];
    let channel = this.guild.channels.cache.get(track.channelID);
    switch (track.type)
    {
        //case "soundcloud" omitted
        case "youtube":
        {
            const video = ytdl(track.url, { highWaterMark: 5242880 });
            video.on("error", (err) => console.log(err));
            channel.send(`playing: **${track.name}** // requester: **${track.requester.tag}**`);
            let dispatcher = connection.play(video); // dispatcher will begin
            dispatcher.setVolume(this.volume);
            dispatcher.on("finish", () => this.finish()); // almost immediately the following will be called
            // fyi: there isn't anything special in the function called, just some code to shift the queue
            break;
        }
    }
}

Further details:

Relevant client options:

if this doesn't actually have to do with discord.js, i apologize for the inconvenience.

superischroma commented 3 years ago

i just realized this is likely an ffmpeg issue, so i think i'll take this issue elsewhere.