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

Joinig channel times out when joining too fast after leaving #4058

Closed
1 task
p0thi opened this issue Apr 8, 2020 · 1 comment
Closed
1 task

Joinig channel times out when joining too fast after leaving #4058

p0thi opened this issue Apr 8, 2020 · 1 comment

Comments

@p0thi
Copy link

p0thi commented Apr 8, 2020

Please describe the problem you are having in as much detail as possible:
I have a sound bot, that joins a channel, plays an audio file and leaves the channel again.
If the user asks the bot to join this channel too soon after it disconnected, the bot joins the channel, but the channel.join() in the code rejects with a VOICE_CONNECTION_TIMEOUT Error after 15 sec . Then it is impossible to either join or leave the channel again. but technically the bot joined the channel and is still in there.

The sequence of triggering sound commands has to be really fast to reproduce this issue. Even some delays <300 ms are enough on my setup to let this work like expected.

Include a reproducible code sample here, if possible:

const guildOptions = new Map();

async play(sound, channel) {
        if (!channel.joinable || !channel.speakable) {
            return
        }

        let connection = await channel.join();

        if (!guildOptions.has(channel.guild.id)) {
            guildOptions.set(channel.guild.id, {});
        }
        let options = guildOptions.get(channel.guild.id);


        
        return await new Promise((resolve, reject) => {
            if (options.dispatcher) {
                options.dispatcher.off('finish', options.callback);
                options.resolve();
            }

            let readStream;
    
            try {
                readStream = dbManager.getFileStream(sound.file);
            }
            catch (e) {
                log.error(`Can't play`);
                connection.disconnect();
                reject();
                return;
            }
    
            let dispatcher = connection.play(readStream, { volume: .5, highWaterMark: 1 });
            
    
            options.callback = () => {
                log.info('file ended');
                options.dispatcher.off('finish', options.callback);
                resolve();
                setTimeout(() =>
                    connection.disconnect(),
                    100
                )
            }
            options.dispatcher = dispatcher;
            options.resolve = resolve;
    
            dispatcher.on('finish', options.callback);
        })

    }

I noticed, that issuing multiple channel.join() commands after the crashes in a catch block, make the bot leave the channel after sending the file stream. But in this case the audio in discord is not hearable. I just lets the bot disconnect from the channel after the finish event. After that everything is back to normal.

Here is another code sample of the same function of my workarounds that worked for me so far, but seem a bit hacky since it uses a static delay.

const guildOptions = new Map();

async play(sound, channel) {
        return await new Promise(async (resolve, reject) => {

            if (!channel.joinable || !channel.speakable) {
                return
            }

            if (!guildOptions.has(channel.guild.id)) {
                guildOptions.set(channel.guild.id, {});
            }
            let options = guildOptions.get(channel.guild.id);

            let connection;
            try {
                if (options.connection && options.connection.status === 4) {
                    if (options.disconnectTime) {
                        // ensures a delay of 300 ms between the last leaving event before joining again
                        const timeToWait = Math.max(0, 300 - Math.abs(moment().diff(options.disconnectTime)))

                        if (timeToWait > 0) {
                            log.debug(`waiting ${timeToWait} ms`)
                            await new Promise((resolve) => {
                                setTimeout(resolve, timeToWait)
                            })
                        }
                    }
                }
                connection = await channel.join()
            }
            catch (err) {

                try {
                    channel.join()
                    connection = await channel.join()
                }
                catch (e) {
                    channel.leave()
                    reject();
                    return;
                }
            }
            if (options.dispatcher) {
                options.dispatcher.off('finish', options.callback);
            }

            if (options.resolve) {
                options.resolve();
            }
            options.resolve = resolve

            options.connection = connection;

            let readStream;

            try {
                readStream = dbManager.getFileStream(sound.file);
            }
            catch (e) {
                connection.disconnect();
                reject();
                return;
            }

            let dispatcher = connection.play(readStream, { volume: .5, highWaterMark: 1 });


            options.callback = () => {
                setTimeout(() => {
                    options.dispatcher.off('finish', options.callback);
                    options.disconnectTime = moment();
                    connection.disconnect();
                    resolve();
                },
                    100
                )
            }
            dispatcher.on('finish', options.callback);

            options.dispatcher = dispatcher;

        })

    }

Further details:

  • discord.js version: 12.0.2 / 12.1.1
  • Node.js version: 12.3.1
  • Operating system: Ubuntu 18.04 / CentOS 7
  • Priority this issue should have – please be realistic and elaborate if possible: Maybe 4/10
  • I have also tested the issue on latest master, commit hash:
@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 1, 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

4 participants