Files
IRC-kosmi-relay/bridge/jackbox/votes.go

49 lines
1.3 KiB
Go
Raw Normal View History

2025-11-01 10:40:53 -04:00
package jackbox
import (
"regexp"
"strings"
)
// tickerVoteRe matches a ticker symbol (2-4 uppercase alphanumeric chars)
// immediately followed by ++ or --.
var tickerVoteRe = regexp.MustCompile(`(?i)\b([A-Z0-9]{2,4})(\+\+|--)`)
2025-11-01 10:40:53 -04:00
// DetectVote checks if a message contains a vote and returns the vote type
// and optional ticker symbol.
//
// For "thisgame++" / "thisgame--": returns (true, "up"/"down", "")
// For "QPL3++" / "tmp2--": returns (true, "up"/"down", "QPL3"/"TMP2")
// For non-vote messages: returns (false, "", "")
func DetectVote(text string) (isVote bool, voteType string, ticker string) {
2025-11-01 10:40:53 -04:00
lower := strings.ToLower(text)
2025-11-01 10:40:53 -04:00
if strings.Contains(lower, "thisgame++") {
return true, "up", ""
2025-11-01 10:40:53 -04:00
}
2025-11-01 10:40:53 -04:00
if strings.Contains(lower, "thisgame--") {
return true, "down", ""
2025-11-01 10:40:53 -04:00
}
if m := tickerVoteRe.FindStringSubmatch(text); m != nil {
sym := strings.ToUpper(m[1])
if _, ok := LookupTicker(sym); ok {
if m[2] == "++" {
return true, "up", sym
}
return true, "down", sym
}
}
return false, "", ""
2025-11-01 10:40:53 -04:00
}
// IsRelayedMessage checks if a message is relayed from another chat
// Returns true if the message has [irc] or [kosmi] prefix
func IsRelayedMessage(text string) bool {
lower := strings.ToLower(text)
return strings.HasPrefix(lower, "[irc]") || strings.HasPrefix(lower, "[kosmi]")
}