From 52172d615fe225de611e44072fe21e9f7770af06 Mon Sep 17 00:00:00 2001 From: Hua Audio Date: Sat, 21 Mar 2026 09:33:15 +0100 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- pkg/channels/discord/voice.go | 30 ++++++++++++++++++++++++------ pkg/voice/agent.go | 5 ++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/pkg/channels/discord/voice.go b/pkg/channels/discord/voice.go index 8dd1a6b55..d5d5d303f 100644 --- a/pkg/channels/discord/voice.go +++ b/pkg/channels/discord/voice.go @@ -113,16 +113,24 @@ func (c *DiscordChannel) receiveVoice(vc *discordgo.VoiceConnection, guildID str return } + if p == nil { + logger.DebugCF("discord", "Received nil Opus packet", nil) + continue + } + + if len(p.Opus) == 0 { + logger.DebugCF("discord", "Received empty Opus packet", map[string]any{ + "seq": p.Sequence, + "ssrc": p.SSRC, + }) + continue + } + logger.DebugCF("discord", "Received Opus packet", map[string]any{ "seq": p.Sequence, "len": len(p.Opus), "ssrc": p.SSRC, }) - - if p == nil || len(p.Opus) == 0 { - continue - } - // Interruption detection: if user sends voice while TTS is playing, // cancel TTS after a short debounce (3 packets in 200ms) now := time.Now() @@ -158,7 +166,17 @@ func (c *DiscordChannel) receiveVoice(vc *discordgo.VoiceConnection, guildID str Data: p.Opus, } - c.bus.PublishAudioChunk(c.ctx, chunk) + ctx, cancel := context.WithTimeout(c.ctx, 100*time.Millisecond) + err := c.bus.PublishAudioChunk(ctx, chunk) + cancel() + if err != nil { + logger.ErrorCF("discord", "Failed to publish audio chunk", map[string]any{ + "guild": guildID, + "sessionID": sessionID, + "sequence": sequence, + "error": err.Error(), + }) + } } } } diff --git a/pkg/voice/agent.go b/pkg/voice/agent.go index ceb14540e..8e978487a 100644 --- a/pkg/voice/agent.go +++ b/pkg/voice/agent.go @@ -90,7 +90,10 @@ func (a *Agent) listenChunks(ctx context.Context) { select { case <-ctx.Done(): return - case chunk := <-chunks: + case chunk, ok := <-chunks: + if !ok { + return + } a.handleChunk(chunk) } }