Skip to content

If bot is connected to a voice channel for too long, it stops playing audio. #4021

Closed
@Dorfnox

Description

@Dorfnox

Hi -

I have a discord bot that plays music in a voice channel.

I leave it connected overnight. Around 3am~ish, a user enters the voice channel and begins playing music just fine. They log out at 5am.

At 9am I wake up and join the voice channel, and music doesn't play. There are no errors thrown in the console.

It's as if everything works perfectly fine, except music doesn't stream through.
Some of my code:

    _playRecursively() {
        const wr = new WaffleResponse();
        const { info, msg } = this.musicQueue.peek();
        const { title, videoId } = info.player_response.videoDetails;
        const ytLink = `https://www.youtube.com/watch?v=${videoId}`;

        const connection = this._getVoiceConnection();
        if (!connection) return ;

        const readableStream = ytdl.downloadFromInfo(info, { quality: 'highestaudio', highWaterMark: 1 << highWaterMarkBitShift }); /* ~4mbs */

        readableStream.on('error', err => {
            wr.setResponse(`'${title}' encountered an error while streaming. skipping.`).setError(err).reply(msg);
            const dispatcher = this._getDispatcher();
            if (dispatcher) dispatcher.end();
        });

        const dispatcher = connection.play(readableStream, { highWaterMark: 1 })
        dispatcher.on('start', () => {
                const embeddedMessage = this._getEmbeddedQueueMessage(false);
                wr.setEmbeddedResponse(embeddedMessage).reply(msg);
                this.client.user.setPresence({ activity: { name: `${title} 🎧`, type: 'PLAYING', url: ytLink }});
            })
            .on('finish', () => {
                wr.setResponse(`**${title}** has finished playing`).setIsSendable(false).reply(msg);
                this.client.user.setPresence({ activity: { name: '', type: '' }});
                getSafe(() => dispatcher.destroy());
                this.musicQueue.dequeue();
                if (!this.musicQueue.isEmpty()) {
                    this._playRecursively();
                }
            })
            .on('error', err => {
                wr.setResponse(`'${title}' encountered an error while streaming. skipping.`).setError(err).reply(msg);
                this.client.user.setPresence({ activity: { name: '', type: '' }});
                getSafe(() => dispatcher.destroy());
                this.musicQueue.dequeue();
                if (!this.musicQueue.isEmpty()) {
                    this._playRecursively();
                }
            });
        dispatcher.setVolumeLogarithmic(0.5);
    }
  • discord.js version: latest
  • Node.js version: latest
  • Operating system: MaxOS Catalina
  • Priority this issue should have – please be realistic and elaborate if possible: low

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions