mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
feat(discord): resolve channel references and expand message links
Add resolveDiscordRefs method that: 1. Resolves <#id> channel mentions to #channel-name by calling the Discord API to fetch channel info 2. Expands Discord message links (up to 3) by fetching the linked message content and appending it as '[linked message from User]: content' Applied to both quoted/referenced messages and the main message content for full context resolution.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -342,10 +343,12 @@ func (c *DiscordChannel) handleMessage(s *discordgo.Session, m *discordgo.Messag
|
||||
if m.MessageReference != nil && m.ReferencedMessage != nil {
|
||||
refContent := m.ReferencedMessage.Content
|
||||
if refContent != "" {
|
||||
refContent = c.resolveDiscordRefs(s, refContent)
|
||||
content = fmt.Sprintf("[quoted message from %s]: %s\n\n%s",
|
||||
m.ReferencedMessage.Author.Username, refContent, content)
|
||||
}
|
||||
}
|
||||
content = c.resolveDiscordRefs(s, content)
|
||||
|
||||
senderID := m.Author.ID
|
||||
|
||||
@@ -517,6 +520,45 @@ func applyDiscordProxy(session *discordgo.Session, proxyAddr string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolveDiscordRefs resolves channel references (<#id> → #channel-name) and
|
||||
// expands Discord message links to show the linked message content.
|
||||
func (c *DiscordChannel) resolveDiscordRefs(s *discordgo.Session, text string) string {
|
||||
// 1. Resolve channel references: <#id> → #channel-name
|
||||
channelRe := regexp.MustCompile(`<#(\d+)>`)
|
||||
text = channelRe.ReplaceAllStringFunc(text, func(match string) string {
|
||||
parts := channelRe.FindStringSubmatch(match)
|
||||
if len(parts) < 2 {
|
||||
return match
|
||||
}
|
||||
ch, err := s.Channel(parts[1])
|
||||
if err != nil {
|
||||
return match
|
||||
}
|
||||
return "#" + ch.Name
|
||||
})
|
||||
|
||||
// 2. Expand Discord message links (max 3)
|
||||
msgLinkRe := regexp.MustCompile(`https://(?:discord\.com|discordapp\.com)/channels/(\d+)/(\d+)/(\d+)`)
|
||||
matches := msgLinkRe.FindAllStringSubmatch(text, 3)
|
||||
for _, m := range matches {
|
||||
if len(m) < 4 {
|
||||
continue
|
||||
}
|
||||
channelID, messageID := m[2], m[3]
|
||||
msg, err := s.ChannelMessage(channelID, messageID)
|
||||
if err != nil || msg == nil || msg.Content == "" {
|
||||
continue
|
||||
}
|
||||
author := "unknown"
|
||||
if msg.Author != nil {
|
||||
author = msg.Author.Username
|
||||
}
|
||||
text += fmt.Sprintf("\n[linked message from %s]: %s", author, msg.Content)
|
||||
}
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
// stripBotMention removes the bot mention from the message content.
|
||||
// Discord mentions have the format <@USER_ID> or <@!USER_ID> (with nickname).
|
||||
func (c *DiscordChannel) stripBotMention(text string) string {
|
||||
|
||||
Reference in New Issue
Block a user