Surface Stoat's voice errors and stop leaving orphan connections

Joining a channel failed silently: revoice's join() runs an async
executor inside `new Promise`, so a rejected join_call never reaches
reject() — the promise hangs forever and the real error escapes as an
unhandled rejection. The client wrapper now latches API failures and
settles the join itself, translating Stoat's error codes (AlreadyConnected,
LiveKitUnavailable, UnknownNode, ...) into messages the chat can show.

A failed join also used to leave the connection object alive, which kept
the bot registered in the channel and made the next attempt fail with
AlreadyConnected; it is now destroyed on any failure. The LiveKit node
name is configurable via VOICE_NODE for instances that renamed it, and
the README documents how to clear a stuck voice state from Redis.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Leonid Pershin
2026-09-08 23:40:21 +03:00
co-authored by Claude Opus 5
parent d4880e757e
commit 823a9f1565
6 changed files with 584 additions and 464 deletions
+23 -14
View File
@@ -146,20 +146,29 @@ export class GuildPlayer extends EventEmitter<GuildPlayerEvents> {
this.log.info({ channelId }, "joining voice channel");
const connection = await this.revoice.join(channelId);
// The room connects asynchronously inside revoice's constructor, so we wait
// for it to report readiness rather than polling a state getter.
await new Promise<void>((resolve, reject) => {
const timer = setTimeout(
() => reject(new UserFacingError("Не удалось подключиться к голосовому каналу")),
JOIN_TIMEOUT_MS,
);
const done = () => {
clearTimeout(timer);
resolve();
};
connection.once("join", done);
connection.once("roomfetched", done);
});
try {
// The room connects asynchronously inside revoice's constructor, so we wait
// for it to report readiness rather than polling a state getter.
await new Promise<void>((resolve, reject) => {
const timer = setTimeout(
() => reject(new UserFacingError("Не удалось подключиться к голосовому каналу")),
JOIN_TIMEOUT_MS,
);
const done = () => {
clearTimeout(timer);
resolve();
};
connection.once("join", done);
connection.once("roomfetched", done);
});
} catch (err) {
// Leave no orphan: an abandoned connection keeps the bot registered in the
// channel, and Stoat then refuses the next join with AlreadyConnected.
await connection.destroy().catch(() => {});
connection.removeAllListeners();
this.setStatus("idle");
throw err;
}
this.connection = connection;
this.voiceReady = true;