diff --git a/pkg/channels/discord.go b/pkg/channels/discord.go index deacbc7d5..dc92b6256 100644 --- a/pkg/channels/discord.go +++ b/pkg/channels/discord.go @@ -220,6 +220,7 @@ func (c *DiscordChannel) handleMessage(s *discordgo.Session, m *discordgo.Messag if isAudio { localPath := c.downloadAttachment(attachment.URL, attachment.Filename) if localPath != "" { + mediaPaths = append(mediaPaths, localPath) transcribedText := "" if c.transcriber != nil && c.transcriber.IsAvailable() { ctx, cancel := context.WithTimeout(c.getContext(), transcriptionTimeout) diff --git a/pkg/channels/onebot.go b/pkg/channels/onebot.go index 06186f783..726970fdf 100644 --- a/pkg/channels/onebot.go +++ b/pkg/channels/onebot.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "os" "strconv" "strings" "sync" @@ -570,9 +569,8 @@ func parseJSONString(raw json.RawMessage) string { type parseMessageResult struct { Text string IsBotMentioned bool - Media []string - LocalFiles []string - ReplyTo string + Media []string + ReplyTo string } func (c *OneBotChannel) parseMessageSegments(raw json.RawMessage, selfID int64) parseMessageResult { @@ -603,7 +601,6 @@ func (c *OneBotChannel) parseMessageSegments(raw json.RawMessage, selfID int64) mentioned := false selfIDStr := strconv.FormatInt(selfID, 10) var media []string - var localFiles []string var replyTo string for _, seg := range segments { @@ -642,7 +639,6 @@ func (c *OneBotChannel) parseMessageSegments(raw json.RawMessage, selfID int64) }) if localPath != "" { media = append(media, localPath) - localFiles = append(localFiles, localPath) textParts = append(textParts, fmt.Sprintf("[%s]", segType)) } } @@ -656,7 +652,6 @@ func (c *OneBotChannel) parseMessageSegments(raw json.RawMessage, selfID int64) LoggerPrefix: "onebot", }) if localPath != "" { - localFiles = append(localFiles, localPath) if c.transcriber != nil && c.transcriber.IsAvailable() { tctx, tcancel := context.WithTimeout(c.ctx, 30*time.Second) result, err := c.transcriber.Transcribe(tctx, localPath) @@ -702,9 +697,8 @@ func (c *OneBotChannel) parseMessageSegments(raw json.RawMessage, selfID int64) return parseMessageResult{ Text: strings.TrimSpace(strings.Join(textParts, "")), IsBotMentioned: mentioned, - Media: media, - LocalFiles: localFiles, - ReplyTo: replyTo, + Media: media, + ReplyTo: replyTo, } } @@ -824,19 +818,10 @@ func (c *OneBotChannel) handleMessage(raw *oneBotRawEvent) { } } - // Clean up temp files when done - if len(parsed.LocalFiles) > 0 { - defer func() { - for _, f := range parsed.LocalFiles { - if err := os.Remove(f); err != nil { - logger.DebugCF("onebot", "Failed to remove temp file", map[string]interface{}{ - "path": f, - "error": err.Error(), - }) - } - } - }() - } + // Note: media files in os.TempDir()/picoclaw_media/ are not cleaned up here + // because HandleMessage publishes to an async message bus. The consumer + // goroutine may still need these files after this function returns. + // Temp files are managed by OS temp directory lifecycle. if c.isDuplicate(messageID) { logger.DebugCF("onebot", "Duplicate message, skipping", map[string]interface{}{