All checks were successful
PR build (required check) / build-frontend (pull_request) Has been skipped
PR build (required check) / validate-observability (pull_request) Has been skipped
secrets-guard / encrypted (pull_request) Successful in 6s
PR build (required check) / changes (pull_request) Successful in 9s
shell-lint / shellcheck (pull_request) Successful in 6s
PR build (required check) / build-backend (pull_request) Successful in 1m10s
PR build (required check) / gate (pull_request) Successful in 2s
THERMOGRAPH_DISCORD_BOT_MENTIONS=0 makes the gateway bot silent on guild mentions. Default on, and unset counts as on, so every env file that predates the knob behaves exactly as before. The reason is a second responder. The operator's desktop now runs a conversational agent that replies as this same bot account (discord-voice-bot, AGENTS.md) and grades through the same API we do, so with both live a single "@Thermograph Phoenix" gets answered twice — once as a card, once as a reply. Only one of us can own that surface, and the agent is the one that can also hold a conversation. Scoped to guild mentions on purpose. DMs stay here: the agent polls guild channels only, so staying silent in a DM would just drop the message. And /grade is a signed HTTP interaction that never touched this path, so it keeps answering from prod while the desktop is asleep — which is what stops this from trading an always-on surface for a sometimes-on one. Claude-Session: https://claude.ai/code/session_015Z1ebLbhUxeZ9ozpNrVTCP
126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
// Message triage: the pure decision of whether — and how — to answer one
|
|
// MESSAGE_CREATE. Ported from the Python bot's _response_for_message /
|
|
// _mentions_bot / _is_dm / _strip_mentions; it is plain string/JSON logic
|
|
// with no data dependency, so it lives in Go. Anything needing climate data
|
|
// (the actual grade) goes back to Python via Grader.
|
|
|
|
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// helpText answers an empty query. Go owns this constant because it needs no
|
|
// data; the wording matches the Python original exactly.
|
|
const helpText = "Mention me with a city name — e.g. `@Thermograph Phoenix` — and I'll grade " +
|
|
"today's weather against ~45 years of local history."
|
|
|
|
// snowflake decodes a Discord id that may arrive as a JSON string (what v10
|
|
// sends) or a bare number. The Python compared ids through str() coercion;
|
|
// this keeps that tolerance instead of betting the payload shape never
|
|
// wobbles.
|
|
type snowflake string
|
|
|
|
func (s *snowflake) UnmarshalJSON(b []byte) error {
|
|
if string(b) == "null" {
|
|
*s = ""
|
|
return nil
|
|
}
|
|
var str string
|
|
if err := json.Unmarshal(b, &str); err == nil {
|
|
*s = snowflake(str)
|
|
return nil
|
|
}
|
|
var n json.Number
|
|
if err := json.Unmarshal(b, &n); err == nil {
|
|
*s = snowflake(n.String())
|
|
return nil
|
|
}
|
|
return fmt.Errorf("snowflake: cannot decode %s", b)
|
|
}
|
|
|
|
// gwMessage is the slice of a MESSAGE_CREATE payload the triage needs.
|
|
type gwMessage struct {
|
|
ID snowflake `json:"id"`
|
|
ChannelID snowflake `json:"channel_id"`
|
|
GuildID snowflake `json:"guild_id"`
|
|
Content string `json:"content"`
|
|
Author gwAuthor `json:"author"`
|
|
Mentions []gwAuthor `json:"mentions"`
|
|
}
|
|
|
|
type gwAuthor struct {
|
|
ID snowflake `json:"id"`
|
|
Bot bool `json:"bot"`
|
|
}
|
|
|
|
// action is triage's verdict for one message.
|
|
type action int
|
|
|
|
const (
|
|
actSilent action = iota // no reply
|
|
actHelp // reply with helpText
|
|
actGrade // call back into Python with the query
|
|
)
|
|
|
|
// mentionsBot reports whether the message @mentions our own user id.
|
|
func mentionsBot(m *gwMessage, botID string) bool {
|
|
for _, u := range m.Mentions {
|
|
if string(u.ID) == botID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// isDM: a DM has no guild_id; a guild message always carries one.
|
|
func isDM(m *gwMessage) bool {
|
|
return m.GuildID == ""
|
|
}
|
|
|
|
// stripMentions drops every form of the bot's own mention (<@id> and the
|
|
// legacy <@!id>) and collapses the surrounding whitespace, leaving just the
|
|
// user's query text.
|
|
func stripMentions(content, botID string) string {
|
|
out := strings.ReplaceAll(content, "<@"+botID+">", " ")
|
|
out = strings.ReplaceAll(out, "<@!"+botID+">", " ")
|
|
return strings.Join(strings.Fields(out), " ")
|
|
}
|
|
|
|
// triage decides the reply for one MESSAGE_CREATE: silence, the help line, or
|
|
// a grade lookup for the returned query.
|
|
//
|
|
// Silent unless the message DMs the bot or @mentions it, and never for a bot
|
|
// author — which includes ourselves, the guard against a mention-loop (our
|
|
// reply quoting the mention must not trigger another reply). The text after
|
|
// the mention is treated as a city query; DMs are the query as-is (trimmed
|
|
// but not collapsed, matching the Python's .strip()).
|
|
//
|
|
// guildMentions false keeps us silent on server-channel mentions while leaving
|
|
// DMs alone: the operator's conversational agent answers as this same identity,
|
|
// and both of us replying means one mention gets two answers. See
|
|
// config.DiscordGuildMentions for the full picture.
|
|
func triage(m *gwMessage, botID string, guildMentions bool) (action, string) {
|
|
if m.Author.Bot || string(m.Author.ID) == botID {
|
|
return actSilent, ""
|
|
}
|
|
dm := isDM(m)
|
|
if !dm && !mentionsBot(m, botID) {
|
|
return actSilent, ""
|
|
}
|
|
if !dm && !guildMentions {
|
|
return actSilent, ""
|
|
}
|
|
var query string
|
|
if dm {
|
|
query = strings.TrimSpace(m.Content)
|
|
} else {
|
|
query = stripMentions(m.Content, botID)
|
|
}
|
|
if query == "" {
|
|
return actHelp, ""
|
|
}
|
|
return actGrade, query
|
|
}
|