import { AudioPlayerStatus, createAudioPlayer, entersState, joinVoiceChannel, NoSubscriberBehavior, VoiceConnection, VoiceConnectionStatus } from "@discordjs/voice"; import { Guild, VoiceBasedChannel } from "discord.js"; import { Logger } from "../utils/Logger"; import { createResourceFromMp3Buffer } from "../utils/Transcode"; export class VoiceSession { private player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Pause } }); // 마지막으로 재생을 시작한 채팅의 도착 순번(TTSClient.seq). private lastPlayedSeq = 0; private isSessionEnd = false; private lastTime = Date.now(); private endTimer?: NodeJS.Timeout | null; private connection: VoiceConnection; constructor( private guild: Guild, private channel: VoiceBasedChannel, private endTimeMs: number, private onSessionEnd: (guildId: string) => void ) { this.connection = joinVoiceChannel({ guildId: this.guild.id, channelId: this.channel.id, adapterCreator: this.guild.voiceAdapterCreator, selfDeaf: true, selfMute: false, }); this.init(); } private get tag() { return `[Voice] guild=${this.guild.id}`; } // 끊김 원인 추적용: 음성 연결 상태 변화와 오류를 남긴다. private watchConnection(connection: VoiceConnection) { connection.on("stateChange", (oldState, newState) => { if (oldState.status === newState.status) return; const reason = newState.status === VoiceConnectionStatus.Disconnected ? ` reason=${newState.reason} closeCode=${"closeCode" in newState ? newState.closeCode : "-"}` : ""; Logger.info(`${this.tag} connection ${oldState.status} -> ${newState.status}${reason}`); }); connection.on("error", (e) => { Logger.error(`${this.tag} connection error: ${String(e)}`); }); } private async init() { this.watchConnection(this.connection); // 재생 중 일시정지(연결 미준비로 AutoPaused 등)는 음성이 끊기는 직접 신호다. this.player.on("stateChange", (oldState, newState) => { const s = [oldState.status, newState.status]; if (s.includes(AudioPlayerStatus.AutoPaused) || s.includes(AudioPlayerStatus.Paused)) { Logger.info(`${this.tag} player ${oldState.status} -> ${newState.status}`); } }); this.connection.subscribe(this.player); await entersState(this.connection, VoiceConnectionStatus.Ready, 20_000).catch(() => {}); this.player.on(AudioPlayerStatus.Playing, () => { this.startEndTimer(); }); this.player.on(AudioPlayerStatus.Idle, () => { this.startEndTimer(); }); this.player.on("error", (e) => { Logger.error("player error:"+String(e)); this.startEndTimer(); }); } private startEndTimer() { if (this.endTimer) clearTimeout(this.endTimer); this.endTimer = setTimeout(() => { this.endTimer = null; if ( !this.isSessionEnd && this.player.state.status === AudioPlayerStatus.Idle && (Date.now() - this.lastTime) >= this.endTimeMs ) { this.sessionEnd(); } else { this.startEndTimer(); } }, this.endTimeMs); } /** * 새 TTS가 들어오면 이전 재생을 의도적으로 interrupt한다 (큐잉 없음). * 다중 사용자 환경에서 모든 메시지를 큐로 쌓으면 자기 발화가 수십 초 * 밀려 채팅 맥락과 어긋나므로, "최신 발화 우선, 앞건 잘림"을 정책으로 * 채택했다. 이전 mp3가 잘리는 동작 자체는 설계 의도이다. * 단, "최신"은 합성 완료 순서가 아니라 채팅 도착 순서(seq)로 판단한다. * 먼저 온 채팅의 합성이 늦게 끝났다면, 이미 재생을 시작한 더 최신 * 채팅을 덮어쓰지 않고 버린다. */ public async play(mp3Buf: Buffer, seq: number) { if (this.isSessionEnd) return; if (seq <= this.lastPlayedSeq) { Logger.info(`${this.tag} seq=${seq} dropped (newer seq=${this.lastPlayedSeq} already playing)`); return; } this.lastPlayedSeq = seq; this.lastTime = Date.now(); this.startEndTimer(); const res = createResourceFromMp3Buffer(mp3Buf); // try { this.player.stop(true); } catch {}; try { this.player.play(res); } catch {}; } async moveTo(newChannel: VoiceBasedChannel) { if (this.isSessionEnd) return; if (this.channel.id === newChannel.id) return; this.channel = newChannel; this.startEndTimer(); try { this.connection.disconnect(); } catch {}; try { this.connection.destroy(); } catch {}; this.connection = joinVoiceChannel({ guildId: this.guild.id, channelId: this.channel.id, adapterCreator: this.guild.voiceAdapterCreator, selfDeaf: true, selfMute: false, }); this.watchConnection(this.connection); this.connection.subscribe(this.player); await entersState(this.connection, VoiceConnectionStatus.Ready, 20_000).catch(() => {}); } sessionEnd() { if (this.isSessionEnd) return; this.isSessionEnd = true; if (this.endTimer) clearTimeout(this.endTimer); this.endTimer = null; try { this.player.stop(true); } catch {}; try { this.connection.disconnect(); } catch {}; try { this.connection.destroy(); } catch {}; this.onSessionEnd(this.guild.id); } }