discordjs / discord.js

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

Weird bug + "Cannot read property 'key' of undefined" #3044

Closed Titancube closed 5 years ago

Titancube commented 5 years ago

Please describe the problem you are having in as much detail as possible: I made a simple bot that joins the voice channel I'm in, and play a shore mp3 file. The bot joins voicechannel perfectly, but none of these code works past ".then". I've seen #1499 already. It says this bug is fixed on master branch but can't find the problem.

Include a reproducible code sample here, if possible:


  if (msg.content === 'v/ yeaboi') {
    // Only try to join the sender's voice channel if they are in one themselves
    if (!msg.guild) return console.log(debug+`No guild`);

    if (msg.member.voiceChannel) {
      let vChan = msg.member.voiceChannel
      console.log(debug+'Stage 0: Before joining vChan');
      // Joins voiceChannel
      vChan.join().then(connection =>{
        console.log(debug+'Stage 1: Before playing audiofile.');

        // Play file
        const dispatcher = connection.playFile('sound/yeaboi.mp3');
        dispatcher.on('end', ()=>{
          console.log(debug+'Stage 2: File ended, on END event, then leave.');
          vChan.leave();
        });
        dispatcher.on('error', e =>{
          console.log(e);
        });
        }).catch(console.error);
    } else {
      msg.reply('You need to join a voice channel first!');
    }
  }

Error

 TypeError: Cannot read property 'key' of undefined
     at StreamDispatcher.createPacket (/home/cabox/workspace/node_modules/discord.js/src/client/voice/dispatcher/StreamDispatcher.js:166:106)
     at StreamDispatcher.sendBuffer (/home/cabox/workspace/node_modules/discord.js/src/client/voice/dispatcher/StreamDispatcher.js:134:25)
     at StreamDispatcher.process (/home/cabox/workspace/node_modules/discord.js/src/client/voice/dispatcher/StreamDispatcher.js:243:14)
     at Socket.stream.once (/home/cabox/workspace/node_modules/discord.js/src/client/voice/dispatcher/StreamDispatcher.js:324:12)
     at Object.onceWrapper (events.js:273:13)
     at Socket.emit (events.js:182:13)
     at emitReadable_ (_stream_readable.js:534:12)
     at process._tickCallback (internal/process/next_tick.js:63:19)

Btw, I've tested with simpler code for debugging:

if (msg.content === 'v/ yeaboi') {
    if (!msg.guild) return console.log(debug+`No guild`);
    if (msg.member.voiceChannel) {
      let vChan = msg.member.voiceChannel
      // Joins voiceChannel
      vChan.join().then(connection =>{
        console.log('test');
        }).catch(console.error);
    }

Still, "console.log('test'); " didn't run.

Further details:

amishshah commented 5 years ago

If the latter code example didn't work I'm convinced you have a network connection issue somewhere since in that example you're not even trying to play audio, just connect to voice.

amishshah commented 5 years ago

Could you update to the latest commit (https://github.com/discordjs/discord.js/commit/aab3523fb5b60d21d20cbf2be01c32bdf21aa8ae) and try the following to produce some more debug information:

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

You should see output something like this: image

If you could try this again and send me the full output from this log -- this will contain sensitive information so I'd recommend messaging me on Discord at hydrabolt#0001. However, you should still be ok to post the details here on GitHub if you remove your IP address from the logs and partially redact any IDs.

Titancube commented 5 years ago

Hi, I was out for few days and checked this just an hour ago. Thank you for the reply! I Updated to the latest commit on master and also had to change few lines.

//11.4.2
let vChan = msg.member.voiceChannel;
dispatcher.on('end', () => {
          vChan.leave();
        });

//12.0.0-dev
let vChan = msg.member.voice.channel;
dispatcher.on('finnish', () => {
          vChan.leave();
        });
//Add detroy
dispatcher.destroy();

Then I tested the bot, and got VOICE_CONNECTION_TIMEOUT error.

Optional packages installed

[VOICE (GUILD_ID:2)]: Sending voice state update (queue length is 0): {"guild_id":"GUILD_ID","channel_id":"CHANNEL_ID","self_mute":false,"self_deaf":false}
[VOICE] received voice state update: {"channel_id":"CHANNEL_ID","deaf":false,"guild_id":"GUILD_ID","mute":false,"self_deaf":false,"self_mute":false,"self_video":false,"session_id":"SESSION_ID","suppress":false,"user_id":"USER_ID","member":{"deaf":false,"joined_at":"2019-01-17T10:41:23.123491+00:00","mute":false,"nick":null,"roles":["ROLE_ID"],"user":{"avatar":"f7ba7df099315fbf0fc5035cd5039463","bot":true,"discriminator":"7283","id":"USER_ID","username":"H3RM4N"}}}
[VOICE] connection? true, GUILD_ID SESSION_ID CHANNEL_ID
[VOICE (GUILD_ID:2)]: Setting sessionID SESSION_ID (stored as "undefined")
[VOICE (GUILD_ID:2)]: Authenticated with sessionID SESSION_ID
[VOICE] received voice server: {"d":{"endpoint":"japan576.discord.gg:80","guild_id":"GUILD_ID","token":"TOKEN"},"op":0,"s":7,"t":"VOICE_SERVER_UPDATE"}
[VOICE] voiceServer guild: GUILD_ID token: TOKEN endpoint: japan576.discord.gg:80
[VOICE (GUILD_ID:2)]: Token "TOKEN" and endpoint "japan576.discord.gg:80"
[VOICE (GUILD_ID:2)]: Endpoint resolved as japan576.discord.gg
[VOICE (GUILD_ID:2)]: Authenticated with sessionID SESSION_ID
[VOICE (GUILD_ID:1)]: Connect triggered
[VOICE (GUILD_ID:1)]: [WS] connect requested
[VOICE (GUILD_ID:1)]: [WS] connecting, 1 attempts, undefined
[VOICE (GUILD_ID:1)]: [WS] opened at gateway japan576.discord.gg
[VOICE (GUILD_ID:1)]: [WS] >> {"op":0,"d":{"server_id":"GUILD_ID","user_id":"USER_ID","token":"TOKEN","session_id":"SESSION_ID"}}
[VOICE (GUILD_ID:1)]: [WS] << {"op":8,"d":{"v":4,"heartbeat_interval":13750}}
[VOICE (GUILD_ID:1)]: [WS] << {"op":2,"d":{"ssrc":7,"port":50104,"modes":["xsalsa20_poly1305_lite","xsalsa20_poly1305_suffix","xsalsa20_poly1305"],"ip":"III.PPP.VVV.444"}}
[VOICE (GUILD_ID:1)]: Selecting the xsalsa20_poly1305_lite mode
[VOICE (GUILD_ID:1)]: [UDP] created socket
[VOICE (GUILD_ID:1)]: [WS] >> {"op":3,"d":2301756717}
[VOICE (GUILD_ID:1)]: [WS] << {"op":6,"d":2301756717}
[VOICE (GUILD_ID:1)]: Authenticate failed - VOICE_CONNECTION_TIMEOUT
{ Error [VOICE_CONNECTION_TIMEOUT]: Connection not established within 15 seconds.
    at VoiceConnection.authenticateFailed (/home/cabox/workspace/H3RM4N/node_modules/discord.js/src/client/voice/VoiceConnection.js:291:26)
    at connectTimeout.client.setTimeout (/home/cabox/workspace/H3RM4N/node_modules/discord.js/src/client/voice/VoiceConnection.js:313:18)
    at Timeout.setTimeout (/home/cabox/workspace/H3RM4N/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) [Symbol(code)]: 'VOICE_CONNECTION_TIMEOUT' }
amishshah commented 5 years ago

Thanks for the info @Titancube, could you try uninstalling uws and just using ws to see if that helps? Thanks

amishshah commented 5 years ago

I think there's something wrong with the UDP socket, can you try the above commit and see if that produces any more debug information?

Titancube commented 5 years ago

Deleted uws and updated discord.js to latest commit.

This is the log where [WS] appears.

[VOICE (GUILD_ID:1)]: Connect triggered
[VOICE (GUILD_ID:1)]: [WS] connect requested
[VOICE (GUILD_ID:1)]: [WS] connecting, 1 attempts, undefined
[VOICE (GUILD_ID:1)]: [WS] opened at gateway japan894.discord.media
[VOICE (GUILD_ID:1)]: [WS] >> {"op":0,"d":{"server_id":"GUILD_ID","user_id":"USER_ID","token":"TOKEN","session_id":"SESSION_ID"}}
[VOICE (GUILD_ID:1)]: [WS] << {"op":8,"d":{"v":4,"heartbeat_interval":13750}}
[VOICE (GUILD_ID:1)]: [WS] << {"op":2,"d":{"ssrc":7,"port":51694,"modes":["xsalsa20_poly1305_lite","xsalsa20_poly1305_suffix","xsalsa20_poly1305"],"ip":"III.PPP.VVV.444"}}
[VOICE (GUILD_ID:1)]: Selecting the xsalsa20_poly1305_lite mode
[VOICE (GUILD_ID:1)]: [UDP] created socket
[VOICE (GUILD_ID:1)]: Sending IP discovery packet: [0,0,0,7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
[VOICE (GUILD_ID:1)]: Successfully sent IP discovery packet
[VOICE (GUILD_ID:1)]: [WS] >> {"op":3,"d":99740693725}
[VOICE (GUILD_ID:1)]: [WS] << {"op":6,"d":99740693725}
[VOICE (GUILD_ID:1)]: Authenticate failed - VOICE_CONNECTION_TIMEOUT
{ Error [VOICE_CONNECTION_TIMEOUT]: Connection not established within 15 seconds.
    at VoiceConnection.authenticateFailed (/home/cabox/workspace/H3RM4N/node_modules/discord.js/src/client/voice/VoiceConnection.js:291:26)
    at connectTimeout.client.setTimeout (/home/cabox/workspace/H3RM4N/node_modules/discord.js/src/client/voice/VoiceConnection.js:313:18)
    at Timeout.setTimeout (/home/cabox/workspace/H3RM4N/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) [Symbol(code)]: 'VOICE_CONNECTION_TIMEOUT' }
[VOICE (GUILD_ID:4)]: [WS] >> {"op":3,"d":32248445883}
[VOICE (GUILD_ID:4)]: [WS] << {"op":6,"d":32248445883}
amishshah commented 5 years ago

Ok, it looks like there's a network issue here as Discord.js is successfully sending the IP discovery packet, but a response is never received so the connection can't be fulfilled. Can you make sure that a firewall isn't blocking your bot?

Could you also try ping address with the address you have redacted above as III.PPP.VVV.444?

Thanks

Titancube commented 5 years ago

Tried ping from both container and PC and no packet loss. And it Seems like ports from 50000 to 65535 are all open.

PING III.PPP.VVV.444 (III.PPP.VVV.444) 56(84) bytes of data.
64 bytes from III.PPP.VVV.444: icmp_seq=1 ttl=44 time=148 ms
64 bytes from III.PPP.VVV.444: icmp_seq=2 ttl=44 time=148 ms
64 bytes from III.PPP.VVV.444: icmp_seq=3 ttl=44 time=148 ms
64 bytes from III.PPP.VVV.444: icmp_seq=4 ttl=44 time=148 ms
64 bytes from III.PPP.VVV.444: icmp_seq=5 ttl=44 time=148 ms
64 bytes from III.PPP.VVV.444: icmp_seq=6 ttl=44 time=148 ms
64 bytes from III.PPP.VVV.444: icmp_seq=7 ttl=44 time=148 ms
64 bytes from III.PPP.VVV.444: icmp_seq=8 ttl=44 time=148 ms
64 bytes from III.PPP.VVV.444: icmp_seq=9 ttl=44 time=148 ms
64 bytes from III.PPP.VVV.444: icmp_seq=10 ttl=44 time=148 ms
....
--- II.PPP.VVV.444 ping statistics ---
30 packets transmitted, 30 received, 0% packet loss, time 29029ms
rtt min/avg/max/mdev = 148.152/148.500/153.743/1.042 ms
Titancube commented 5 years ago

It's already 7 months passed since the last comment and I have to admit it's the network problem. So apparently It seems like Codeanywhere server can send normal things quite well but no voice packets. I don't know exactly why though.