1

问题:对于我通过 discord.js 模块播放的一些歌曲StreamDispatcher(似乎是开头有静音的歌曲),调度程序几乎会立即完成并抛出错误。我使用 ffmpeg-static 模块。

从我所见,这是我从调用调度程序的“调试”和“错误”事件中收到的两个错误

Error: ffmpeg stream: write EPIPE

Error [ERR_STREAM_DESTROYED]: ffmpeg stream: Cannot call write after a stream was destroyed

可重现的代码示例(需要 ytdl-core):

/**
* @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)

我项目的原始代码加上指导的一些评论:

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;
        }
    }
}

细节:

  • discord.js 版本:12.4.1
  • node.js 版本:v12.18.3
  • 操作系统:Windows 10
4

0 回答 0