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_TIMEOUT "crash" in certain scenario #3476

Closed robotboy655 closed 4 years ago

robotboy655 commented 5 years ago

Discord.js (v12.0.0 last updated 19 Sept 2019) seems to crash with the following trace in a very certain scenario. I have not tested previous Discord,js releases. I have also tested a clean install on Windows.

Reproduction steps (code below)

  1. Connect to a voice channel, bot joins automatically (not move from a channel, connect)
  2. Disconnect (not move to a different channel) from voice, bot stays
  3. Connect back into the same channel
  4. At this point no audio playback can happen from the bot. After 15 seconds the error will be thrown.

This is sort of a joke bot for fun, started noticing it crashing for seemingly no reason, decided to put a clear "disconnect before joining anywhere" and stumbled upon this repro code. As the code shows I tried adding event handlers to everywhere I can, it still crashes, and doesn't fire the event handler.

NodeJS v10.15.3

Logged in as Bottt!

Leaving voice channels... // First join and then leave
Joining channel...
Connection established...
debug! [WS] << {"op":13,"d":{"user_id":"whatever"}}

Leaving voice channels... // Second join
debug! [WS] shutdown requested
debug! [WS] reset requested
debug! [UDP] shutdown requested
debug! disconnect() triggered
debug! Sending voice state update: {"channel_id":null,"guild_id":"whatever","self_mute":false,"self_deaf":false}
debug! Connection clean up
debug! [WS] shutdown requested
debug! [WS] reset requested
disconnect! undefined
Joining channel...
debug! [UDP] socket closed
debug! [WS] closed
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error [VOICE_CONNECTION_TIMEOUT]: Connection not established within 15 seconds.
    at VoiceConnection.authenticateFailed (/home/pi/Desktop/git_staging/discord_bot/node_modules/discord.js/src/client/voice/VoiceConnection.js:300:26)
    at connectTimeout.client.setTimeout (/home/pi/Desktop/git_staging/discord_bot/node_modules/discord.js/src/client/voice/VoiceConnection.js:322:18)
    at Timeout.setTimeout (/home/pi/Desktop/git_staging/discord_bot/node_modules/discord.js/src/client/BaseClient.js:83:7)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)
Emitted 'error' event at:
    at VoiceConnection.authenticateFailed (/home/pi/Desktop/git_staging/discord_bot/node_modules/discord.js/src/client/voice/VoiceConnection.js:300:12)
    at connectTimeout.client.setTimeout (/home/pi/Desktop/git_staging/discord_bot/node_modules/discord.js/src/client/voice/VoiceConnection.js:322:18)
    [... lines matching original stack trace ...]
    at Timer.processTimers (timers.js:223:10)

Reproduction code

console.log( "NodeJS " + process.version );

const Discord = require( 'discord.js' );
const client = new Discord.Client();

// The token of your bot - https://discordapp.com/developers/applications/me
const token = 'Your token';

function leaveVoice2() {
    client.voice.broadcasts.forEach( function( broadcast, key ) {
        broadcast.end();
    } );
    client.voice.connections.forEach( function( voiceCon, key ) {
        voiceCon.channel.leave();
    } );
}

function joinChannel2( voiceChannel ) {
    console.log("Leaving voice channels...");
    leaveVoice2();

    console.log("Joining channel...");
    voiceChannel.join().then( function( connection ) {
        console.log("Connection established...");

        connection.on( 'error', ( event ) => { console.log('error!', event ); } );
        connection.on( 'disconnect', ( event ) => { console.log('disconnect!', event ); } );
        connection.on( 'failed', ( event ) => { console.log('failed!', event ); } );
        connection.on( 'debug', ( event ) => { console.log('debug!', event ); } );

        // Normally play sounds here
    }, function( error ) {
        console.log( "ERROR FAILED TO JOIN VOICE CHANNEL" );
    } );
}

client.on( "voiceStateUpdate", function( oldState, newState ) {
    if ( ( oldState && oldState.id == client.user.id ) || ( newState && newState.id == client.user.id ) ) {
        return;
    }

    if ( !newState.channel || oldState.channel ) {
        return;
    }

    joinChannel2( newState.channel );
} );

client.on( 'error', console.log );

client.login( token );

Further details:

robotboy655 commented 5 years ago

Potentially similar issue #3320

iCrawl commented 4 years ago

Closing as it is a stale issue, reopen or check open issues if this is still an issue.