twitch-bot/twitch/badges.go
Knut Ahlers 57211b6184
Fix: Message may be nil with new events
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-09-02 17:59:57 +02:00

73 lines
1.4 KiB
Go

package twitch
import (
"strconv"
"strings"
"github.com/go-irc/irc"
)
const (
BadgeBroadcaster = "broadcaster"
BadgeFounder = "founder"
BadgeModerator = "moderator"
BadgeSubscriber = "subscriber"
)
type BadgeCollection map[string]*int
func ParseBadgeLevels(m *irc.Message) BadgeCollection {
out := BadgeCollection{}
if m == nil {
return out
}
badgeString, ok := m.GetTag("badges")
if !ok || len(badgeString) == 0 {
return out
}
badges := strings.Split(badgeString, ",")
for _, b := range badges {
badgeParts := strings.Split(b, "/")
if len(badgeParts) != 2 { //nolint:gomnd // This is not a magic number but just an expected count
continue
}
level, err := strconv.Atoi(badgeParts[1])
if err != nil {
continue
}
out.Add(badgeParts[0], level)
}
// If there is a founders badge but no subscribers badge
// add a level-0 subscribers badge to prevent the bot to
// cause trouble on founders when subscribers are allowed
// to do something
if out.Has(BadgeFounder) && !out.Has(BadgeSubscriber) {
out.Add(BadgeSubscriber, out.Get(BadgeFounder))
}
return out
}
func (b BadgeCollection) Add(badge string, level int) {
b[badge] = &level
}
func (b BadgeCollection) Get(badge string) int {
l, ok := b[badge]
if !ok {
return 0
}
return *l
}
func (b BadgeCollection) Has(badge string) bool {
return b[badge] != nil
}