discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

voice connection says it timed out, despite bot visibly joining in the voice channel #4302

Closed zimmah closed 3 years ago

zimmah commented 4 years ago

Please describe the problem you are having in as much detail as possible: Background: I have a bot that should play a very short sound when a timer runs out. This bot runs in multiple servers and should serve multiple groups per server (each with their own textchannel and voice channel). When the timer for a group runs out, it should connect to the voice channel of that group, play a very short sound, and disconnect. Then it should be ready to repeat the process.

The problem is, quite often it would just connect, say the connection timed out, and crash. Sometimes crashing the whole bot in the process.

 (node:23) UnhandledPromiseRejectionWarning: Error [VOICE_CONNECTION_TIMEOUT]: Connection not established within 15 seconds.
2020-05-19T12:18:59.877314+00:00 app[worker.1]: at VoiceConnection.authenticateFailed (/app/node_modules/discord.js/src/client/voice/VoiceConnection.js:304:26)
2020-05-19T12:18:59.877315+00:00 app[worker.1]: at /app/node_modules/discord.js/src/client/voice/VoiceConnection.js:325:61
2020-05-19T12:18:59.877316+00:00 app[worker.1]: at Timeout.<anonymous> (/app/node_modules/discord.js/src/client/BaseClient.js:83:7)

This is one error, but after that, sometimes it has another, much longer error, like this:

message: 'WebSocket was closed before the connection was established',
2020-05-19T12:19:25.426020+00:00 app[worker.1]: error: Error: WebSocket was closed before the connection was established
2020-05-19T12:19:25.426021+00:00 app[worker.1]: at WebSocket.close (/app/node_modules/ws/lib/websocket.js:220:14)
2020-05-19T12:19:25.426021+00:00 app[worker.1]: at VoiceWebSocket.reset (/app/node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:53:60)
2020-05-19T12:19:25.426022+00:00 app[worker.1]: at VoiceWebSocket.shutdown (/app/node_modules/discord.js/src/client/voice/networking/VoiceWebSocket.js:44:10)
2020-05-19T12:19:25.426022+00:00 app[worker.1]: at VoiceConnection.cleanup (/app/node_modules/discord.js/src/client/voice/VoiceConnection.js:393:10)
2020-05-19T12:19:25.426023+00:00 app[worker.1]: at VoiceConnection._disconnect (/app/node_modules/discord.js/src/client/voice/VoiceConnection.js:368:10)

Include a reproducible code sample here, if possible:

let isVoiceBusy = false;
const queue = [];

const playNext = async (msg) => {
    isVoiceBusy = true;
    const next = queue.shift();
    const voiceChannel = msg.guild.channels.cache.find(channel => channel.name === next);
    try {
        const connection = await voiceChannel.join();
        const dispatcher = connection.play('./sounds/honk-sound.mp3');
        dispatcher.on('finish', () => {
            connection.disconnect();
            dispatcher.destroy();
            if (queue.length > 0) {
                setTimeout(() => playNext(msg), 500);
            } else {
                setTimeout(() => isVoiceBusy = false, 500);

            }
        });
    } catch (error) {

    }
}

const honk = (msg, mob) => {
    queue.push(mob.mobName); // the mobName corresponds to the voice channel name
    if (!isVoiceBusy) playNext(msg);
}

Further details:

zimmah commented 4 years ago

Note that it also sometimes complains about 'Unexpected server response: 525'

ObscuritySRL commented 4 years ago

@zimmah - It sounds like your client options is missing GUILD_VOICE_STATES in the ws intents.

DELUUXE commented 4 years ago

im also seeing a few of these 'Unexpected server response: 525' errors about a week ago, this does seem to have decreased now, maybe that one was related to some server-side change at discord. _i do have the GUILD_VOICE_STATES ws intent defined_

zimmah commented 4 years ago

The weird thing is it that it often works fine, but then sometimes it suddenly doesn't. It's a little unreliable, that's why it seems like a bug.

CShigaki commented 4 years ago

I'm also dealing with this problem. A workaround for it is waiting 5 seconds after leaving and before joining another channel

Nathan-Search commented 3 years ago

Try to regenerate the token in the developer website

jerardoauiniga commented 3 years ago
amishshah commented 3 years ago

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 (https://github.com/discordjs/discord.js/pull/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: