Add Discord mention_only option for @-mention only responses

- Add MentionOnly config option to DiscordConfig
- Add botUserID tracking in DiscordChannel
- Add mention checking logic in handleMessage
- Add network_mode: host to docker-compose for connectivity
This commit is contained in:
Vernon Stinebaker 2026-02-16 22:33:13 +08:00
parent 13e4028d42
commit dc85d0929b
3 changed files with 25 additions and 3 deletions

View file

@ -8,6 +8,7 @@ services:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
container_name: picoclaw-agent container_name: picoclaw-agent
network_mode: host
profiles: profiles:
- agent - agent
volumes: volumes:
@ -26,6 +27,7 @@ services:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
container_name: picoclaw-gateway container_name: picoclaw-gateway
network_mode: host
restart: unless-stopped restart: unless-stopped
profiles: profiles:
- gateway - gateway

View file

@ -26,6 +26,7 @@ type DiscordChannel struct {
config config.DiscordConfig config config.DiscordConfig
transcriber *voice.GroqTranscriber transcriber *voice.GroqTranscriber
ctx context.Context ctx context.Context
botUserID string
} }
func NewDiscordChannel(cfg config.DiscordConfig, bus *bus.MessageBus) (*DiscordChannel, error) { func NewDiscordChannel(cfg config.DiscordConfig, bus *bus.MessageBus) (*DiscordChannel, error) {
@ -72,6 +73,7 @@ func (c *DiscordChannel) Start(ctx context.Context) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to get bot user: %w", err) return fmt.Errorf("failed to get bot user: %w", err)
} }
c.botUserID = botUser.ID
logger.InfoCF("discord", "Discord bot connected", map[string]any{ logger.InfoCF("discord", "Discord bot connected", map[string]any{
"username": botUser.Username, "username": botUser.Username,
"user_id": botUser.ID, "user_id": botUser.ID,
@ -296,6 +298,23 @@ func (c *DiscordChannel) handleMessage(s *discordgo.Session, m *discordgo.Messag
return return
} }
// 如果配置为仅响应提及,则检查是否被提及
if c.config.MentionOnly {
isMentioned := false
for _, mention := range m.Mentions {
if mention.ID == c.botUserID {
isMentioned = true
break
}
}
if !isMentioned {
logger.DebugCF("discord", "Message ignored - bot not mentioned", map[string]any{
"user_id": m.Author.ID,
})
return
}
}
senderID := m.Author.ID senderID := m.Author.ID
senderName := m.Author.Username senderName := m.Author.Username
if m.Author.Discriminator != "" && m.Author.Discriminator != "0" { if m.Author.Discriminator != "" && m.Author.Discriminator != "0" {

View file

@ -107,6 +107,7 @@ type DiscordConfig struct {
Enabled bool `json:"enabled" env:"PICOCLAW_CHANNELS_DISCORD_ENABLED"` Enabled bool `json:"enabled" env:"PICOCLAW_CHANNELS_DISCORD_ENABLED"`
Token string `json:"token" env:"PICOCLAW_CHANNELS_DISCORD_TOKEN"` Token string `json:"token" env:"PICOCLAW_CHANNELS_DISCORD_TOKEN"`
AllowFrom FlexibleStringSlice `json:"allow_from" env:"PICOCLAW_CHANNELS_DISCORD_ALLOW_FROM"` AllowFrom FlexibleStringSlice `json:"allow_from" env:"PICOCLAW_CHANNELS_DISCORD_ALLOW_FROM"`
MentionOnly bool `json:"mention_only" env:"PICOCLAW_CHANNELS_DISCORD_MENTION_ONLY"`
} }
type MaixCamConfig struct { type MaixCamConfig struct {