Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Dorfnox opened this issue Apr 4, 2020 · 5 comments
Closed

Comments

@Dorfnox
Copy link

Dorfnox commented Apr 4, 2020

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
@uhKevinMC
Copy link
Contributor

This could be caused by the stream collapsing between you and the 3rd party, and your bot not recognizing that it has stopped. Did your bots speaking status also change or?

@Dorfnox
Copy link
Author

Dorfnox commented Apr 5, 2020

Thanks -
The bot even shows up as green as if it's 'speaking' when I play a song.

Do you think I should regularly disconnect it from the voice channel if there has been no activity for a while? I'm wondering if it could be because leave it connected to the voice channel for days without disconnecting.

@uhKevinMC
Copy link
Contributor

uhKevinMC commented Apr 6, 2020

The bot even shows up as green as if it's 'speaking' when I play a song.

Do you think I should regularly disconnect it from the voice channel if there has been no activity for a while?

Usually, the StreamDispatcher shouldn't stop like that, even if its for long periods at a time. It might be a possibility of having too many dropped packets and the dispatcher just stopping. If you can, can you attempt this again and console.log() the reason for the dispatcher finishing/erroring?

This can ALSO be caused by the high water mark, but it should end the stream rather than finishing in the matter you described.

If none of those solutions are convenient for you, you can also try lavalink by Frederikam. Click for Repo

@Moebits
Copy link
Contributor

Moebits commented Apr 7, 2020

I sometimes had this problem when playing from a readableStream. The solution was to write the stream onto an audio file, and then play the audio file.

@amishshah
Copy link
Member

Hi there,

We're working on a new implementation of Discord's Voice API that has better playback quality and is more reliable than what we currently support in Discord.js v12 - check it out at https://github.com/discordjs/voice!

The new library solves many of the issues that users are facing, and as part of this, we're dropping built-in support for voice in our next major release. We have a PR (#5402) that adds native support for our new voice library - once this PR is merged, this issue will be closed.

You can still use our new voice library before that PR lands - just take a look at our music bot example to see how to get started upgrading your voice code. By using the boilerplate music player in the example, you can make it even easier to upgrade your code.

Note that the PR above only reduces some of the boilerplate code you'd otherwise have to write - you do not have to wait for the PR to be merged to start using the new voice library.


If you have any questions about this, feel free to:

  • Make an issue if you have found a bug in the new voice library
  • Use GitHub Discussions or join our Discord server (we have a new channel, #djs-new-voice, specifically for this!) to ask general questions about the library, give feedback on the library, and get support with upgrading to it

@kyranet kyranet closed this as completed Jun 9, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants