Search code examples
node.jsdiscorddiscord.jsytdl

Discord.js ytdl Error: input stream: Status code: 416


I have a music Discord bot using ytdl and occasionally, the song would randomly stop and this error would be outputted:

Error: input stream: Status code: 416

Here is my code for my playSong function

static playSong(queue, message) {
    const classThis = this;
    queue[0].voiceChannel
    .join()
    .then(function(connection) {
        const dispatcher = connection.play(ytdl(queue[0].url, {quality: 'highestaudio'}))
        .on('start', function() {
            message.guild.musicData.songDispatcher = dispatcher;
            dispatcher.setVolume(message.guild.musicData.volume);
            const videoEmbed = new Discord.MessageEmbed()
                .setThumbnail(queue[0].thumbnail)
                .setColor(embedSettings.color)
                .addField('Now Playing', `${queue[0].title} (${queue[0].url})`)
                .addField('Duration', queue[0].duration)
                .setFooter(embedSettings.footer, embedSettings.footer_url);
            if (queue[1]) videoEmbed.addField('Next Song:', queue[1].title);
            message.say(videoEmbed);
            message.guild.musicData.nowPlaying = queue[0];
            return queue.shift();
        })
        .on('finish', function() {
            if (queue.length >= 1) {
                return classThis.playSong(queue, message);
            } else {
                message.guild.musicData.isPlaying = false;
                message.guild.musicData.nowPlaying = null;
                message.guild.musicData.songDispatcher = null;
                if (message.guild.me.voice.channel) {
                    return message.guild.me.voice.channel.leave();
                }
            }
        })
}

When I moved the bot over to a Discord Bot hosting service, the error occurred less frequently but still occurred. Is this a problem with my code, or with the connection with YouTube API?


Solution

  • I fixed this problem by using:

    const dispatcher = connection.play(ytdl(queue[0].url, {
        quality: 'highestaudio',
        highWaterMark: 1 << 25
    }))
    

    so, for you:

    static playSong(queue, message) {
        const classThis = this;
        queue[0].voiceChannel
        .join()
        .then(function(connection) {
            const dispatcher = connection.play(ytdl(queue[0].url, {quality: 'highestaudio', highWaterMark: 1 << 25 }))
            .on('start', function() {
                message.guild.musicData.songDispatcher = dispatcher;
                dispatcher.setVolume(message.guild.musicData.volume);
                const videoEmbed = new Discord.MessageEmbed()
                    .setThumbnail(queue[0].thumbnail)
                    .setColor(embedSettings.color)
                    .addField('Now Playing', `${queue[0].title} (${queue[0].url})`)
                    .addField('Duration', queue[0].duration)
                    .setFooter(embedSettings.footer, embedSettings.footer_url);
                if (queue[1]) videoEmbed.addField('Next Song:', queue[1].title);
                message.say(videoEmbed);
                message.guild.musicData.nowPlaying = queue[0];
                return queue.shift();
            })
            .on('finish', function() {
                if (queue.length >= 1) {
                    return classThis.playSong(queue, message);
                } else {
                    message.guild.musicData.isPlaying = false;
                    message.guild.musicData.nowPlaying = null;
                    message.guild.musicData.songDispatcher = null;
                    if (message.guild.me.voice.channel) {
                        return message.guild.me.voice.channel.leave();
                    }
                }
            })
    }