2021-09-02 15:09:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-11-08 19:17:07 +00:00
|
|
|
"encoding/json"
|
2021-09-02 15:09:30 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/internal/service/access"
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-09-02 15:09:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2022-10-29 13:16:30 +00:00
|
|
|
topicRegistration struct {
|
|
|
|
Topic string
|
|
|
|
Condition twitch.EventSubCondition
|
|
|
|
RequiredScopes []string
|
|
|
|
AnyScope bool
|
|
|
|
Hook func(json.RawMessage) error
|
2023-02-04 11:25:46 +00:00
|
|
|
Version string
|
2022-10-29 13:16:30 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
twitchChannelState struct {
|
|
|
|
Category string
|
|
|
|
IsLive bool
|
|
|
|
Title string
|
2021-11-08 19:17:07 +00:00
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
isInitialized bool
|
|
|
|
esc *twitch.EventSubSocketClient
|
2021-09-02 15:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
twitchWatcher struct {
|
|
|
|
ChannelStatus map[string]*twitchChannelState
|
|
|
|
|
|
|
|
lock sync.RWMutex
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
func (t *twitchChannelState) CloseESC() {
|
|
|
|
t.esc.Close()
|
|
|
|
t.esc = nil
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
func (t twitchChannelState) Equals(c twitchChannelState) bool {
|
|
|
|
return t.Category == c.Category &&
|
|
|
|
t.IsLive == c.IsLive &&
|
|
|
|
t.Title == c.Title
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:42:37 +00:00
|
|
|
func (t *twitchChannelState) Update(c twitchChannelState) {
|
|
|
|
t.Category = c.Category
|
|
|
|
t.IsLive = c.IsLive
|
|
|
|
t.Title = c.Title
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
func newTwitchWatcher() *twitchWatcher {
|
|
|
|
return &twitchWatcher{
|
|
|
|
ChannelStatus: make(map[string]*twitchChannelState),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:17:07 +00:00
|
|
|
func (t *twitchWatcher) AddChannel(channel string) error {
|
|
|
|
t.lock.RLock()
|
|
|
|
_, ok := t.ChannelStatus[channel]
|
|
|
|
t.lock.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
2021-09-02 15:09:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:42:37 +00:00
|
|
|
// Initialize for check loop
|
|
|
|
t.lock.Lock()
|
|
|
|
t.ChannelStatus[channel] = &twitchChannelState{}
|
|
|
|
t.lock.Unlock()
|
|
|
|
|
|
|
|
return t.updateChannelFromAPI(channel)
|
2021-09-02 15:09:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 19:17:07 +00:00
|
|
|
func (t *twitchWatcher) Check() {
|
2021-09-02 15:09:30 +00:00
|
|
|
var channels []string
|
2021-11-08 19:17:07 +00:00
|
|
|
t.lock.RLock()
|
|
|
|
for c := range t.ChannelStatus {
|
2023-05-18 13:05:43 +00:00
|
|
|
if t.ChannelStatus[c].esc != nil {
|
2021-11-08 19:17:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
channels = append(channels, c)
|
|
|
|
}
|
2021-11-08 19:17:07 +00:00
|
|
|
t.lock.RUnlock()
|
2021-09-02 15:09:30 +00:00
|
|
|
|
|
|
|
for _, ch := range channels {
|
2021-12-31 12:42:37 +00:00
|
|
|
if err := t.updateChannelFromAPI(ch); err != nil {
|
2021-09-02 15:09:30 +00:00
|
|
|
log.WithError(err).WithField("channel", ch).Error("Unable to update channel status")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:17:07 +00:00
|
|
|
func (t *twitchWatcher) RemoveChannel(channel string) error {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
if t.ChannelStatus[channel].esc != nil {
|
|
|
|
t.ChannelStatus[channel].esc.Close()
|
2021-11-08 19:17:07 +00:00
|
|
|
}
|
2021-09-02 15:09:30 +00:00
|
|
|
|
2021-11-08 19:17:07 +00:00
|
|
|
delete(t.ChannelStatus, channel)
|
2021-09-02 15:09:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-29 13:16:30 +00:00
|
|
|
func (t *twitchWatcher) getTopicRegistrations(userID string) []topicRegistration {
|
|
|
|
return []topicRegistration{
|
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeChannelUpdate,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID},
|
|
|
|
RequiredScopes: nil,
|
|
|
|
Hook: t.handleEventSubChannelUpdate,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeStreamOffline,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID},
|
|
|
|
RequiredScopes: nil,
|
|
|
|
Hook: t.handleEventSubStreamOnOff(false),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeStreamOnline,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID},
|
|
|
|
RequiredScopes: nil,
|
|
|
|
Hook: t.handleEventSubStreamOnOff(true),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeChannelFollow,
|
2023-03-21 23:12:06 +00:00
|
|
|
Version: twitch.EventSubTopicVersion2,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID, ModeratorUserID: userID},
|
|
|
|
RequiredScopes: []string{twitch.ScopeModeratorReadFollowers},
|
2022-10-29 13:16:30 +00:00
|
|
|
Hook: t.handleEventSubChannelFollow,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeChannelRaid,
|
|
|
|
Condition: twitch.EventSubCondition{FromBroadcasterUserID: userID},
|
|
|
|
RequiredScopes: nil,
|
|
|
|
Hook: t.handleEventSubChannelOutboundRaid,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeChannelPointCustomRewardRedemptionAdd,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID},
|
|
|
|
RequiredScopes: []string{twitch.ScopeChannelReadRedemptions, twitch.ScopeChannelManageRedemptions},
|
|
|
|
AnyScope: true,
|
|
|
|
Hook: t.handleEventSubChannelPointCustomRewardRedemptionAdd,
|
|
|
|
},
|
2023-05-21 12:59:06 +00:00
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeChannelPollBegin,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID},
|
|
|
|
RequiredScopes: []string{twitch.ScopeChannelReadPolls, twitch.ScopeChannelManagePolls},
|
|
|
|
AnyScope: true,
|
|
|
|
Hook: t.handleEventSubChannelPollChange(eventTypePollBegin),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeChannelPollEnd,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID},
|
|
|
|
RequiredScopes: []string{twitch.ScopeChannelReadPolls, twitch.ScopeChannelManagePolls},
|
|
|
|
AnyScope: true,
|
|
|
|
Hook: t.handleEventSubChannelPollChange(eventTypePollEnd),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeChannelPollProgress,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID},
|
|
|
|
RequiredScopes: []string{twitch.ScopeChannelReadPolls, twitch.ScopeChannelManagePolls},
|
|
|
|
AnyScope: true,
|
|
|
|
Hook: t.handleEventSubChannelPollChange(eventTypePollProgress),
|
|
|
|
},
|
2023-03-21 09:51:34 +00:00
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeChannelShoutoutCreate,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID, ModeratorUserID: userID},
|
|
|
|
RequiredScopes: []string{twitch.ScopeModeratorManageShoutouts, twitch.ScopeModeratorReadShoutouts},
|
|
|
|
AnyScope: true,
|
|
|
|
Hook: t.handleEventSubShoutoutCreated,
|
|
|
|
},
|
2023-02-11 22:00:15 +00:00
|
|
|
{
|
|
|
|
Topic: twitch.EventSubEventTypeChannelShoutoutReceive,
|
|
|
|
Condition: twitch.EventSubCondition{BroadcasterUserID: userID, ModeratorUserID: userID},
|
|
|
|
RequiredScopes: []string{twitch.ScopeModeratorManageShoutouts, twitch.ScopeModeratorReadShoutouts},
|
|
|
|
AnyScope: true,
|
|
|
|
Hook: t.handleEventSubShoutoutReceived,
|
|
|
|
},
|
2022-10-29 13:16:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 23:47:40 +00:00
|
|
|
func (t *twitchWatcher) handleEventSubChannelFollow(m json.RawMessage) error {
|
|
|
|
var payload twitch.EventSubEventFollow
|
|
|
|
if err := json.Unmarshal(m, &payload); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling event")
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-02-05 22:50:30 +00:00
|
|
|
"channel": "#" + payload.BroadcasterUserLogin,
|
2021-12-24 23:47:40 +00:00
|
|
|
"followed_at": payload.FollowedAt,
|
|
|
|
"user_id": payload.UserID,
|
|
|
|
"user": payload.UserLogin,
|
|
|
|
})
|
|
|
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("User followed")
|
|
|
|
go handleMessage(ircHdl.Client(), nil, eventTypeFollow, fields)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *twitchWatcher) handleEventSubChannelPointCustomRewardRedemptionAdd(m json.RawMessage) error {
|
|
|
|
var payload twitch.EventSubEventChannelPointCustomRewardRedemptionAdd
|
|
|
|
if err := json.Unmarshal(m, &payload); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling event")
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-02-05 22:50:30 +00:00
|
|
|
"channel": "#" + payload.BroadcasterUserLogin,
|
2021-12-24 23:47:40 +00:00
|
|
|
"reward_cost": payload.Reward.Cost,
|
|
|
|
"reward_id": payload.Reward.ID,
|
|
|
|
"reward_title": payload.Reward.Title,
|
|
|
|
"status": payload.Status,
|
|
|
|
"user_id": payload.UserID,
|
|
|
|
"user_input": payload.UserInput,
|
|
|
|
"user": payload.UserLogin,
|
|
|
|
})
|
|
|
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("ChannelPoint reward was redeemed")
|
|
|
|
go handleMessage(ircHdl.Client(), nil, eventTypeChannelPointRedeem, fields)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-29 13:16:30 +00:00
|
|
|
func (t *twitchWatcher) handleEventSubChannelOutboundRaid(m json.RawMessage) error {
|
|
|
|
var payload twitch.EventSubEventRaid
|
|
|
|
if err := json.Unmarshal(m, &payload); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling event")
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]interface{}{
|
|
|
|
"channel": "#" + payload.FromBroadcasterUserLogin,
|
|
|
|
"to_id": payload.ToBroadcasterUserID,
|
|
|
|
"to": payload.ToBroadcasterUserLogin,
|
|
|
|
"viewers": payload.Viewers,
|
|
|
|
})
|
|
|
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("Outbound raid detected")
|
|
|
|
go handleMessage(ircHdl.Client(), nil, eventTypeOutboundRaid, fields)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-24 23:47:40 +00:00
|
|
|
func (t *twitchWatcher) handleEventSubChannelUpdate(m json.RawMessage) error {
|
|
|
|
var payload twitch.EventSubEventChannelUpdate
|
|
|
|
if err := json.Unmarshal(m, &payload); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling event")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.triggerUpdate(payload.BroadcasterUserLogin, &payload.Title, &payload.CategoryName, nil)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-21 12:59:06 +00:00
|
|
|
func (t *twitchWatcher) handleEventSubChannelPollChange(event *string) func(json.RawMessage) error {
|
|
|
|
return func(m json.RawMessage) error {
|
|
|
|
var payload twitch.EventSubEventPoll
|
|
|
|
if err := json.Unmarshal(m, &payload); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling event")
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]any{
|
|
|
|
"channel": "#" + payload.BroadcasterUserLogin,
|
|
|
|
"hasChannelPointVoting": payload.ChannelPointsVoting.IsEnabled,
|
|
|
|
"title": payload.Title,
|
|
|
|
})
|
|
|
|
|
|
|
|
logger := log.WithFields(log.Fields(fields.Data()))
|
|
|
|
|
|
|
|
switch event {
|
|
|
|
case eventTypePollBegin:
|
|
|
|
logger.Info("Poll started")
|
|
|
|
|
|
|
|
case eventTypePollEnd:
|
|
|
|
logger.WithField("status", payload.Status).Info("Poll ended")
|
|
|
|
|
|
|
|
case eventTypePollProgress:
|
|
|
|
// Lets not spam the info-level-log with every single vote but
|
|
|
|
// provide them for bots with debug-level-logging
|
|
|
|
logger.Debug("Poll changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set after logging not to spam logs with full payload
|
|
|
|
fields.Set("poll", payload)
|
|
|
|
|
|
|
|
go handleMessage(ircHdl.Client(), nil, event, fields)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 09:51:34 +00:00
|
|
|
func (t *twitchWatcher) handleEventSubShoutoutCreated(m json.RawMessage) error {
|
|
|
|
var payload twitch.EventSubEventShoutoutCreated
|
|
|
|
if err := json.Unmarshal(m, &payload); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling event")
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]any{
|
|
|
|
"channel": "#" + payload.BroadcasterUserLogin,
|
|
|
|
"to_id": payload.ToBroadcasterUserID,
|
|
|
|
"to": payload.ToBroadcasterUserLogin,
|
|
|
|
"viewers": payload.ViewerCount,
|
|
|
|
})
|
|
|
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("Shoutout created")
|
|
|
|
go handleMessage(ircHdl.Client(), nil, eventTypeShoutoutCreated, fields)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-11 22:00:15 +00:00
|
|
|
func (t *twitchWatcher) handleEventSubShoutoutReceived(m json.RawMessage) error {
|
|
|
|
var payload twitch.EventSubEventShoutoutReceived
|
|
|
|
if err := json.Unmarshal(m, &payload); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling event")
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]any{
|
2023-03-28 22:13:46 +00:00
|
|
|
"channel": "#" + payload.BroadcasterUserLogin,
|
2023-02-11 22:00:15 +00:00
|
|
|
"from_id": payload.FromBroadcasterUserID,
|
|
|
|
"from": payload.FromBroadcasterUserLogin,
|
|
|
|
"viewers": payload.ViewerCount,
|
|
|
|
})
|
|
|
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("Shoutout received")
|
|
|
|
go handleMessage(ircHdl.Client(), nil, eventTypeShoutoutReceived, fields)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-24 23:47:40 +00:00
|
|
|
func (t *twitchWatcher) handleEventSubStreamOnOff(isOnline bool) func(json.RawMessage) error {
|
|
|
|
return func(m json.RawMessage) error {
|
|
|
|
var payload twitch.EventSubEventFollow
|
|
|
|
if err := json.Unmarshal(m, &payload); err != nil {
|
|
|
|
return errors.Wrap(err, "unmarshalling event")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.triggerUpdate(payload.BroadcasterUserLogin, nil, nil, &isOnline)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:42:37 +00:00
|
|
|
func (t *twitchWatcher) updateChannelFromAPI(channel string) error {
|
|
|
|
t.lock.Lock()
|
|
|
|
defer t.lock.Unlock()
|
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
var (
|
2021-12-31 12:42:37 +00:00
|
|
|
err error
|
|
|
|
status twitchChannelState
|
|
|
|
storedStatus = t.ChannelStatus[channel]
|
2021-09-02 15:09:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
status.IsLive, err = twitchClient.HasLiveStream(channel)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting live status")
|
|
|
|
}
|
|
|
|
|
|
|
|
status.Category, status.Title, err = twitchClient.GetRecentStreamInfo(channel)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "getting stream info")
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:42:37 +00:00
|
|
|
if storedStatus == nil {
|
|
|
|
storedStatus = &twitchChannelState{}
|
|
|
|
t.ChannelStatus[channel] = storedStatus
|
2021-09-02 15:09:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:42:37 +00:00
|
|
|
if storedStatus.isInitialized && !storedStatus.Equals(status) {
|
|
|
|
// Send updates only when we do have an update
|
2021-11-08 19:17:07 +00:00
|
|
|
t.triggerUpdate(channel, &status.Title, &status.Category, &status.IsLive)
|
2021-12-31 12:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storedStatus.Update(status)
|
|
|
|
storedStatus.isInitialized = true
|
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
if storedStatus.esc != nil {
|
2021-12-31 12:42:37 +00:00
|
|
|
// Do not register twice
|
2021-11-08 19:17:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-02 15:39:21 +00:00
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
if storedStatus.esc, err = t.registerEventSubCallbacks(channel); err != nil {
|
2021-11-08 19:17:07 +00:00
|
|
|
return errors.Wrap(err, "registering eventsub callbacks")
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
if storedStatus.esc != nil {
|
|
|
|
log.WithField("channel", channel).Info("watching for eventsub events")
|
|
|
|
go func(storedStatus *twitchChannelState) {
|
|
|
|
if err := storedStatus.esc.Run(); err != nil {
|
|
|
|
log.WithField("channel", channel).WithError(err).Error("eventsub client caused error")
|
|
|
|
}
|
|
|
|
storedStatus.CloseESC()
|
|
|
|
}(storedStatus)
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:17:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
func (t *twitchWatcher) registerEventSubCallbacks(channel string) (*twitch.EventSubSocketClient, error) {
|
|
|
|
tc, err := accessService.GetTwitchClientForChannel(channel, access.ClientConfig{
|
|
|
|
TwitchClient: cfg.TwitchClient,
|
|
|
|
TwitchClientSecret: cfg.TwitchClientSecret,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, access.ErrChannelNotAuthorized) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.Wrap(err, "getting twitch client for channel")
|
2021-11-08 19:17:07 +00:00
|
|
|
}
|
2021-09-02 15:09:30 +00:00
|
|
|
|
2021-11-08 19:17:07 +00:00
|
|
|
userID, err := twitchClient.GetIDForUsername(channel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "resolving channel to user-id")
|
|
|
|
}
|
2021-09-02 15:09:30 +00:00
|
|
|
|
2021-12-24 23:47:40 +00:00
|
|
|
var (
|
2022-10-29 13:16:30 +00:00
|
|
|
topicRegistrations = t.getTopicRegistrations(userID)
|
2023-05-18 13:05:43 +00:00
|
|
|
topicOpts []twitch.EventSubSocketClientOpt
|
2021-11-08 19:17:07 +00:00
|
|
|
)
|
|
|
|
|
2021-12-24 23:47:40 +00:00
|
|
|
for _, tr := range topicRegistrations {
|
|
|
|
logger := log.WithFields(log.Fields{
|
|
|
|
"any": tr.AnyScope,
|
|
|
|
"channel": channel,
|
|
|
|
"scopes": tr.RequiredScopes,
|
|
|
|
"topic": tr.Topic,
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(tr.RequiredScopes) > 0 {
|
2022-09-10 11:39:07 +00:00
|
|
|
fn := accessService.HasPermissionsForChannel
|
2021-12-24 23:47:40 +00:00
|
|
|
if tr.AnyScope {
|
2022-09-10 11:39:07 +00:00
|
|
|
fn = accessService.HasAnyPermissionForChannel
|
2021-11-08 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-10 11:39:07 +00:00
|
|
|
hasScopes, err := fn(channel, tr.RequiredScopes...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "checking granted scopes")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasScopes {
|
2021-12-24 23:47:40 +00:00
|
|
|
logger.Debug("Missing scopes for eventsub topic")
|
|
|
|
continue
|
2021-12-24 19:23:38 +00:00
|
|
|
}
|
2021-12-24 23:47:40 +00:00
|
|
|
}
|
2021-12-24 19:23:38 +00:00
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
topicOpts = append(topicOpts, twitch.WithSubscription(tr.Topic, tr.Version, tr.Condition, tr.Hook))
|
2021-12-24 19:23:38 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
esClient, err := twitch.NewEventSubSocketClient(append(
|
|
|
|
topicOpts,
|
|
|
|
twitch.WithLogger(log.WithField("channel", channel)),
|
|
|
|
twitch.WithTwitchClient(tc),
|
|
|
|
)...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "getting eventsub client for channel")
|
|
|
|
}
|
2021-12-31 12:42:37 +00:00
|
|
|
|
2023-05-18 13:05:43 +00:00
|
|
|
return esClient, nil
|
2021-12-31 12:42:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 19:17:07 +00:00
|
|
|
func (t *twitchWatcher) triggerUpdate(channel string, title, category *string, online *bool) {
|
|
|
|
if category != nil && t.ChannelStatus[channel].Category != *category {
|
|
|
|
t.ChannelStatus[channel].Category = *category
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"channel": channel,
|
|
|
|
"category": *category,
|
2023-05-21 11:52:40 +00:00
|
|
|
}).Info("Category updated")
|
2021-11-11 13:59:08 +00:00
|
|
|
go handleMessage(ircHdl.Client(), nil, eventTypeTwitchCategoryUpdate, plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-02-05 22:50:30 +00:00
|
|
|
"channel": "#" + channel,
|
2021-11-08 19:17:07 +00:00
|
|
|
"category": *category,
|
2021-11-11 13:59:08 +00:00
|
|
|
}))
|
2021-11-08 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if title != nil && t.ChannelStatus[channel].Title != *title {
|
|
|
|
t.ChannelStatus[channel].Title = *title
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"channel": channel,
|
|
|
|
"title": *title,
|
2023-05-21 11:52:40 +00:00
|
|
|
}).Info("Title updated")
|
2021-11-11 13:59:08 +00:00
|
|
|
go handleMessage(ircHdl.Client(), nil, eventTypeTwitchTitleUpdate, plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-02-05 22:50:30 +00:00
|
|
|
"channel": "#" + channel,
|
2021-11-08 19:17:07 +00:00
|
|
|
"title": *title,
|
2021-11-11 13:59:08 +00:00
|
|
|
}))
|
2021-11-08 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if online != nil && t.ChannelStatus[channel].IsLive != *online {
|
|
|
|
t.ChannelStatus[channel].IsLive = *online
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"channel": channel,
|
|
|
|
"isLive": *online,
|
2023-05-21 11:52:40 +00:00
|
|
|
}).Info("Live-status updated")
|
2021-11-08 19:17:07 +00:00
|
|
|
|
|
|
|
evt := eventTypeTwitchStreamOnline
|
|
|
|
if !*online {
|
|
|
|
evt = eventTypeTwitchStreamOffline
|
|
|
|
}
|
|
|
|
|
2021-11-11 13:59:08 +00:00
|
|
|
go handleMessage(ircHdl.Client(), nil, evt, plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-02-05 22:50:30 +00:00
|
|
|
"channel": "#" + channel,
|
2021-11-11 13:59:08 +00:00
|
|
|
}))
|
2021-11-08 19:17:07 +00:00
|
|
|
}
|
2021-09-02 15:09:30 +00:00
|
|
|
}
|