Skip to content

feat(voice): use voice gateway v8 #10918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/voice/src/networking/Networking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,14 @@ export class Networking extends EventEmitter {
* Creates a new WebSocket to a Discord Voice gateway.
*
* @param endpoint - The endpoint to connect to
* @param lastSequence - The last sequence to set for this WebSocket
*/
private createWebSocket(endpoint: string) {
const ws = new VoiceWebSocket(`wss://${endpoint}?v=4`, Boolean(this.debug));
private createWebSocket(endpoint: string, lastSequence?: number) {
const ws = new VoiceWebSocket(`wss://${endpoint}?v=8`, Boolean(this.debug));

if (lastSequence !== undefined) {
ws.sequence = lastSequence;
}

ws.on('error', this.onChildError);
ws.once('open', this.onWsOpen);
Expand Down Expand Up @@ -347,6 +352,7 @@ export class Networking extends EventEmitter {
server_id: this.state.connectionOptions.serverId,
session_id: this.state.connectionOptions.sessionId,
token: this.state.connectionOptions.token,
seq_ack: this.state.ws.sequence,
},
};
this.state.ws.sendPacket(packet);
Expand All @@ -363,10 +369,11 @@ export class Networking extends EventEmitter {
private onWsClose({ code }: CloseEvent) {
const canResume = code === 4_015 || code < 4_000;
if (canResume && this.state.code === NetworkingStatusCode.Ready) {
const lastSequence = this.state.ws.sequence;
this.state = {
...this.state,
code: NetworkingStatusCode.Resuming,
ws: this.createWebSocket(this.state.connectionOptions.endpoint),
ws: this.createWebSocket(this.state.connectionOptions.endpoint, lastSequence),
};
} else if (this.state.code !== NetworkingStatusCode.Closed) {
this.destroy();
Expand All @@ -379,10 +386,11 @@ export class Networking extends EventEmitter {
*/
private onUdpClose() {
if (this.state.code === NetworkingStatusCode.Ready) {
const lastSequence = this.state.ws.sequence;
this.state = {
...this.state,
code: NetworkingStatusCode.Resuming,
ws: this.createWebSocket(this.state.connectionOptions.endpoint),
ws: this.createWebSocket(this.state.connectionOptions.endpoint, lastSequence),
};
}
}
Expand Down
15 changes: 14 additions & 1 deletion packages/voice/src/networking/VoiceWebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export class VoiceWebSocket extends EventEmitter {
*/
public ping?: number;

/**
* The last sequence number acknowledged from Discord. Will be `-1` if no sequence numbered messages have been received.
*/
public sequence = -1;

/**
* The debug logger function, if debugging is enabled.
*/
Expand Down Expand Up @@ -115,6 +120,10 @@ export class VoiceWebSocket extends EventEmitter {
return;
}

if (packet.seq) {
this.sequence = packet.seq;
}

if (packet.op === VoiceOpcodes.HeartbeatAck) {
this.lastHeartbeatAck = Date.now();
this.missedHeartbeats = 0;
Expand Down Expand Up @@ -150,7 +159,11 @@ export class VoiceWebSocket extends EventEmitter {
this.sendPacket({
op: VoiceOpcodes.Heartbeat,
// eslint-disable-next-line id-length
d: nonce,
d: {
// eslint-disable-next-line id-length
t: nonce,
seq_ack: this.sequence,
},
});
}

Expand Down