2021-09-29 16:23:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/go-irc/irc"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
|
|
|
"github.com/Luzifer/twitch-bot/twitch"
|
2021-09-29 16:23:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
twitchUserState struct {
|
|
|
|
Badges twitch.BadgeCollection
|
|
|
|
Color string
|
|
|
|
DisplayName string
|
2021-09-29 17:05:12 +00:00
|
|
|
EmoteSets []string
|
2021-09-29 16:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
twitchUserStateStore struct {
|
|
|
|
states map[string]*twitchUserState
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func newTwitchUserStateStore() *twitchUserStateStore {
|
|
|
|
return &twitchUserStateStore{
|
|
|
|
states: make(map[string]*twitchUserState),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTwitchUserState(m *irc.Message) (*twitchUserState, error) {
|
|
|
|
var (
|
|
|
|
color, _ = m.GetTag("color")
|
|
|
|
displayName, _ = m.GetTag("display-name")
|
2021-09-29 17:05:12 +00:00
|
|
|
emoteSets []string
|
2021-09-29 16:23:29 +00:00
|
|
|
rawSets, _ = m.GetTag("emote-sets")
|
|
|
|
)
|
|
|
|
|
|
|
|
if rawSets != "" {
|
2021-09-29 17:05:12 +00:00
|
|
|
emoteSets = strings.Split(rawSets, ",")
|
2021-09-29 16:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &twitchUserState{
|
|
|
|
Badges: twitch.ParseBadgeLevels(m),
|
|
|
|
Color: color,
|
|
|
|
DisplayName: displayName,
|
|
|
|
EmoteSets: emoteSets,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *twitchUserStateStore) Get(channel string) *twitchUserState {
|
|
|
|
t.lock.RLock()
|
|
|
|
defer t.lock.RUnlock()
|
|
|
|
|
|
|
|
return t.states[channel]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *twitchUserStateStore) Set(channel string, state *twitchUserState) {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
|
|
|
t.states[channel] = state
|
|
|
|
}
|