mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-30 08:31:16 +00:00
69 lines
1.3 KiB
Go
69 lines
1.3 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{}
|
||
|
|
||
|
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
|
||
|
}
|