Files
javis_bot/app/src/core/speakers.ts
javis-bot 8479112ddd feat(app): M2 Discord voice core in the Electron app
- app/ (Electron 45 + TypeScript): the core joins one voice channel, decodes each speaker's Opus to
  48 kHz stereo and streams it to the voice worker, sends speaking_end and speaker_leave, and emits
  speech_start/partial/final with the speaker's display name.
- Sentence playback from the worker's TTS, first come first served, with stop that silences the clip
  at once and cancels unfinished requests; slash command join/leave/stop (create-or-update by name).
- Transcription notice posted on every join; other bots ignored by default; at most maxSpeakers
  people transcribed at once.
- Registered chat channels: live message events, history reads, sends split at 2,000 characters with
  mentions disabled; Message Content intent checked before login.
- The app starts the Python voice worker with a fresh token in the environment, restarts it with
  backoff, and the worker client reconnects and fails in-flight TTS on a lost connection.
- Spec in app/src/core/discord_core.spec.md; Vitest unit tests, a protocol test against the real
  Python worker server with fake engines, and an opt-in live test with two bots on real Discord.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:14:25 +09:00

36 lines
914 B
TypeScript

/**
* Who is being transcribed right now. At most `max` people at once (settings voice.maxSpeakers);
* someone who starts while the limit is reached is not heard until their next start after the
* others free a place.
*/
export class SpeakerGate {
private active = new Set<string>();
constructor(private max: number) {}
/** Discord says `id` started speaking. True if their audio goes to the STT. */
start(id: string): boolean {
if (this.active.has(id)) return true;
if (this.active.size >= this.max) return false;
this.active.add(id);
return true;
}
/** Discord says `id` stopped (or left). True if they were being transcribed. */
end(id: string): boolean {
return this.active.delete(id);
}
isActive(id: string): boolean {
return this.active.has(id);
}
get count(): number {
return this.active.size;
}
clear(): void {
this.active.clear();
}
}