discord.js icon indicating copy to clipboard operation
discord.js copied to clipboard

joinVoiceChannel() fails with "TypeError: Cannot read properties of undefined (reading 'status')"

Open benjamonnguyen opened this issue 2 years ago • 2 comments

Which package is this bug report for?

voice

Issue description

Inconsistently, joinVoiceChannel() will throw error: TypeError: Cannot read properties of undefined (reading 'status')
at Object.sendPayload at createVoiceConnection at joinVoiceChannel

Code sample

const guild = await discordClient.guilds.fetch(guildId);

const conn = joinVoiceChannel({
		channelId: channelId,
		guildId: guildId,
		adapterCreator: guild.voiceAdapterCreator,
});

Package version

14.7.0

Node.js version

8.19.2

Operating system

macOS

Priority this issue should have

High

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds Guild_voice_states

benjamonnguyen avatar Jan 25 '23 22:01 benjamonnguyen

Can you please provide the full stack trace (and never omit it)?

Jiralite avatar Feb 22 '23 13:02 Jiralite

@Jiralite

The error is due to the WebSocketManager losing WebSocketShards. Guild.shard() then returns undefined. I was trying to run 25 shards on one machine so I believe my bot was underprovisioned.

I've since added a check in Guild. voiceAdapterCreator():

  get voiceAdapterCreator() {
    return methods => {
      this.client.voice.adapters.set(this.id, methods);
      return {
        sendPayload: data => {
          if (!this.shard) {
            console.error("shard missing for guild", this.id);
            return false;
          }
          if (this.shard.status !== Status.Ready) return false;
          this.shard.send(data);
          return true;
        },
        destroy: () => {
          this.client.voice.adapters.delete(this.id);
        },
      };
    };
  }

Properly provisioning the bot has resolved the missing WebSocketShard issue, but I think emitting a more descriptive error could help a dev debug this issue much more quickly.

Thank you!

benjamonnguyen avatar Feb 22 '23 16:02 benjamonnguyen

The WebSocket has been rewritten to a new implementation since this issue. Feel free to open a new issue if the problem persists with the new implementation (ideally on 14.11.0+).

Jiralite avatar Jun 29 '23 18:06 Jiralite