feat(voice): retry unrecoverable disconnects
Build Validation / build (push) Successful in 48s

This commit is contained in:
fraujulian committed 2026-09-28 13:38:14 +02:00
1 parent 2b90b2775f
commit ac20d12b13
3 files changed
+47 -6

No files matched your search

+29
View File
@@ -48,6 +48,7 @@ export default class AudioManager implements Disposable {
private connectAttempt: AbortController | undefined;
private renewTimer: NodeJS.Timeout | undefined;
private playbackState: PlaybackState = 'idle';
private reconnectGeneration = 0;
private connectionOptions: VoiceConnectionOptions | undefined;
private audioSource: AudioSource | undefined;
private readonly options: Required<Pick<AudioManagerOptions, 'connectTimeoutMs'>> &
@@ -61,6 +62,13 @@ export default class AudioManager implements Disposable {
assertValidTimerDelay(options.renewIntervalMs, 'renewIntervalMs');
}
if (
options.reconnectAttempts !== undefined &&
(!Number.isSafeInteger(options.reconnectAttempts) || options.reconnectAttempts < 0)
) {
throw new AudioManagerConfigError('reconnectAttempts must be a non-negative safe integer.');
}
if (options.volume?.initialPercent !== undefined) {
if (options.volume.enabled !== true) {
throw new AudioManagerConfigError('volume.initialPercent requires volume.enabled to be true.');
@@ -254,6 +262,7 @@ export default class AudioManager implements Disposable {
return;
}
this.reconnectGeneration += 1;
this.cancelConnectAttempt();
this.clearRenewTimer();
this.stopCurrentPlayback();
@@ -285,6 +294,7 @@ export default class AudioManager implements Disposable {
return;
}
this.reconnectGeneration += 1;
this.cancelConnectAttempt();
this.clearRenewTimer();
this.stopCurrentPlayback();
@@ -366,6 +376,9 @@ export default class AudioManager implements Disposable {
}
private async handleDisconnectedConnection(connection: VoiceConnection): Promise<void> {
const shouldReconnect = this.resource !== undefined && (this.options.reconnectAttempts ?? 0) > 0;
const reconnectGeneration = this.reconnectGeneration;
try {
await Promise.race([
entersState(connection, VoiceConnectionStatus.Signalling, DISCONNECT_RECOVERY_TIMEOUT_MS),
@@ -376,6 +389,22 @@ export default class AudioManager implements Disposable {
return;
}
if (shouldReconnect) {
this.clearRenewTimer();
for (let attempt = 0; attempt < (this.options.reconnectAttempts ?? 0); attempt += 1) {
try {
await this.start();
return;
} catch (reconnectError) {
error = reconnectError;
}
if (this.reconnectGeneration !== reconnectGeneration || this.playbackState === 'disposed') {
return;
}
}
}
this.clearRenewTimer();
this.stopCurrentPlayback();
this.audioPlayer.stop(true);