Skip to content

Voice

player.voices holds one VoiceState per connected guild. A voice connection requires two Discord gateway events to establish — Discolink handles them once you forward their raw payloads.

When your bot joins a voice channel, Discord sends VOICE_STATE_UPDATE and VOICE_SERVER_UPDATE. Discolink waits for both, then makes a request to Lavalink to register the player. Forward all raw dispatches — Discolink filters for them internally:

discord.js
// 'rawWS' for eris
client.on("raw", (payload) => player.voices.handleDispatch(payload));

handleDispatch processes three event types:

  • VOICE_STATE_UPDATE — captures the bot’s session ID and channel
  • VOICE_SERVER_UPDATE — captures the endpoint and token, then completes the connection
  • READY — triggers player.init() automatically when autoInit: true
const voice = await player.voices.connect(guildId, voiceChannelId);
// With options (only applied when creating a new connection)
await player.voices.connect(guildId, voiceChannelId, {
node: "eu-west", // pin to a specific node
volume: 80, // initial volume for the queue (0–1000)
filters: { timescale: { speed: 1.1 } }, // initial filters
context: { textChannelId }, // initial queue context
});

If a connection to the same guild and channel is already in progress, connect() returns the existing promise rather than starting a new one. If the bot is already connected to that channel and the connection is live, it returns the existing VoiceState immediately.

connect() times out after voiceTimeout milliseconds (default 10_000 / 10s) if the gateway events don’t arrive.

  • forwardVoiceUpdate not wired up — the bot never sends the voice state update to Discord, so the gateway never sends the required events back.
  • handleDispatch not wired up — VOICE_STATE_UPDATE / VOICE_SERVER_UPDATE are never forwarded to discolink, so it never receives the data it needs to complete the connection.
  • Missing GuildVoiceStates intent — Discord won’t send VOICE_STATE_UPDATE without it, which means the session ID is never captured.
  • Missing channel permissions — the bot needs Connect (and Speak to produce audio) on the target channel.
  • Voice channel is full — easy to miss: when the channel has hit its user limit, Discord does not send any voice updates, so the connection times out. The bot needs the Move Members permission to bypass the limit and receive both events.

When a new connection is established or a node change is requested without specifying a node, Discolink picks the best node for the voice region. It uses VoiceRegion.getRelevantNode(), which:

  1. Runs NodeManager.relevant() to get ready nodes sorted by their metrics
  2. Sorts them by their voice ping in that region — the median ping plus spike penalty
  3. Returns the first result

The spike penalty is controlled by the voicePenalty player option (default 0, clamped to [0, 1]). With the default, selection uses the median alone; higher values push nodes with erratic latency down the list.

Access the region through player.voices.regions using the voice state’s regionId:

const region = player.voices.regions.get(voice.regionId)!;
const node = region.getRelevantNode("us-east"); // skip us-east

Node ping entries can also be cleared:

region.forgetNode("us-east"); // remove a node's ping stats
region.forgetAllNodes(); // clear all node ping stats for this region

Ping data is collected from queueUpdate ticks — each tick carries the voice WebSocket latency from Lavalink. Samples are kept for the region’s windowSize (the regionWindow player option, 300_000 ms / 5 min by default). Nodes with no entry for the region sort first so they get tried; nodes with stale data score -1, and nodes with fewer than four samples are ranked by their median.

The region also exposes its ping data directly:

region.windowSize; // sample window in ms (regionWindow option)
region.hasNode("us-east"); // whether this region has data for a node
region.inSync(); // whether all ready nodes have data
region.getMedianPing("us-east"); // median ping in ms, -1 if insufficient
region.getAveragePing("us-east"); // average ping in ms, -1 if insufficient
const voice = player.voices.get(guildId)!;
voice.connected; // true when Lavalink reports the player connected
voice.reconnecting; // true during a voice reconnect
voice.disconnected; // true when not connected and not reconnecting
voice.changingNode; // true while changeNode() is in progress
voice.destroyed; // true if this instance is no longer in VoiceManager
voice.unusable; // true if destroyed or its queue is being destroyed
voice.channelId; // current voice channel ID
voice.joined; // true when the bot is in a voice channel
voice.regionId; // voice region extracted from the endpoint (e.g. 'us-east')
voice.ping; // voice WebSocket latency in ms, sourced from Lavalink player state
voice.node; // the Node this connection is on
voice.selfDeaf; // whether the bot has deafened itself
voice.selfMute; // whether the bot has muted itself
voice.serverDeaf; // whether the guild has deafened the bot
voice.serverMute; // whether the guild has muted the bot
voice.suppressed; // whether the bot is suppressed (stage channels)

When a voice connection is severed, Discolink emits voiceClose after having received the corresponding event from the connection’s node and checks the close code. For recoverable codes (4004, 4006, 4011) with voiceReconnect enabled, it calls voice.reconnect(). voiceDisconnect fires only when that reconnect attempt fails — a non-recoverable code, or voiceReconnect: false does not emit it.

// Leave the channel but keep the VoiceState and queue alive
await player.voices.disconnect(guildId);
// Tear everything down — destroys the queue and voice connection
await player.queues.destroy(guildId, "User left the channel");

queues.destroy() is the right call when you’re done with a guild — on channel delete, bot kick, or the last user leaving. It removes the Lavalink player, tears down the voice connection, and emits queueDestroy.

disconnect() only leaves the voice channel; it leaves both the VoiceState and its queue fully intact.

You can also call these directly on a VoiceState or Queue instance:

const voice = player.voices.get(guildId)!;
await voice.connect(); // connect to the same channel
await voice.connect(otherChannelId); // move to a different channel
await voice.reconnect(); // leave and rejoin the same channel
await voice.disconnect(); // disconnect from the channel
const queue = player.queues.get(guildId)!;
await queue.destroy("reason"); // tear down queue and voice connection
await player.voices.get(guildId)!.changeNode("eu-west");
// name is optional, auto-picks the best ready node for the region
await player.voices.get(guildId)!.changeNode();

changeNode() migrates the player to the new node:

  1. Destroys the player on the old node
  2. Creates it on the new node with the same state
  3. Includes and resumes any active track if its source is supported
  4. Emits voiceChange with the previous node and whether playback was active

If changeNode() is already in progress for this voice state, the call returns the existing promise.

With relocateQueues: true (the default), this happens automatically when a node closes or disconnects. The relocation algorithm distributes queues across available nodes proportionally by their load scores.

Full API: VoiceState