ImK
ImK

Reputation: 1

Discord js music player bot suddenly leaves

So I made a music bot for discord js that uses discord player package. The command runs perfectly but in the middle song suddenly stops I tried searching everywhere nothing works and i think its an issue for ffmpeg but im not so sure

bot.player = new Player(bot, {
    ytdlOptions: {
        quality: 'lowestaudio',
        highWaterMark: 1 >> 25
    },
})

const player = bot.player;

//play command 


bot.on('interactionCreate', async interaction =>{
    if(interaction.commandName === 'play') {

        const song = interaction.options.get('song').value;
        const res = await player.search(song, {
            searchEngine: QueryType.SPOTIFY_SONG
        });


        if (!res || !res.tracks.length) return interaction.reply({ content: `No results found ${interaction.member}... try again ? ❌`, ephemeral: true });

        const queue = await player.createQueue(interaction.guild)

        if (!queue.connection) await queue.connect(interaction.member.voice.channel);


        await interaction.channel.send({ content:`Loading your ${res.playlist ? 'playlist' : 'track'}... 🎧`});

        const track = await res.tracks[0]

        console.log(track)
        if(!track)  return interaction.channel.send({content: 'No song found'});

        queue.addTrack(track)

        queue.play();

    }
})

After leaving the channel, other commands and bot work fine

Upvotes: 0

Views: 200

Answers (1)

trustytrojan
trustytrojan

Reputation: 83

What you're experiencing isn't an issue with ffmpeg. It seems like you're using ytdl-core to stream audio. ytdl-core wasn't designed with the intent of keeping HTTP connections open for long periods of time. This happens when streaming media to something that consumes the data at a slower rate than the rate it is being fed the data.

Another npm package known as play-dl solves this issue (and adds support for other streaming services which is a real cherry on top). Check out it's example Discord bot code here to see how it's used.

In my old Discord bot projects, I used play-dl after experiencing the same issue with ytdl-core, and things went smoothly. Leave a comment if you need further help.

Upvotes: 0

Related Questions