From f70382876acbaa291296509b96f0d5579f4f7238 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 24 Dec 2021 20:23:38 +0100 Subject: [PATCH] [core] Add "follow" event using EventSub Signed-off-by: Knut Ahlers --- events.go | 2 ++ twitch/eventsub.go | 1 + twitchWatcher.go | 27 +++++++++++++++++++++++++++ wiki/Events.md | 11 +++++++++++ 4 files changed, 41 insertions(+) diff --git a/events.go b/events.go index ddb1ee9..27203d4 100644 --- a/events.go +++ b/events.go @@ -6,6 +6,7 @@ var ( eventTypeBan = ptrStr("ban") eventTypeBits = ptrStr("bits") eventTypeClearChat = ptrStr("clearchat") + eventTypeFollow = ptrStr("follow") eventTypeGiftPaidUpgrade = ptrStr("giftpaidupgrade") eventTypeHost = ptrStr("host") eventTypeJoin = ptrStr("join") @@ -28,6 +29,7 @@ var ( eventTypeBan, eventTypeBits, eventTypeClearChat, + eventTypeFollow, eventTypeGiftPaidUpgrade, eventTypeHost, eventTypeJoin, diff --git a/twitch/eventsub.go b/twitch/eventsub.go index a4b569f..50aa309 100644 --- a/twitch/eventsub.go +++ b/twitch/eventsub.go @@ -42,6 +42,7 @@ const ( // eventSubStatusUserRemoved = "user_removed" // eventSubStatusVerificationFailed = "webhook_callback_verification_failed" + EventSubEventTypeChannelFollow = "channel.follow" EventSubEventTypeChannelUpdate = "channel.update" EventSubEventTypeStreamOffline = "stream.offline" EventSubEventTypeStreamOnline = "stream.online" diff --git a/twitchWatcher.go b/twitchWatcher.go index 124390a..ae7b16b 100644 --- a/twitchWatcher.go +++ b/twitchWatcher.go @@ -183,10 +183,37 @@ func (t *twitchWatcher) registerEventSubCallbacks(channel string) (func(), error return nil, errors.Wrap(err, "registering channel-update eventsub") } + unsubFollow, err := twitchEventSubClient.RegisterEventSubHooks( + twitch.EventSubEventTypeChannelFollow, + twitch.EventSubCondition{BroadcasterUserID: userID}, + func(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{}{ + "channel": channel, + "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 + }, + ) + if err != nil { + return nil, errors.Wrap(err, "registering channel-follow eventsub") + } + return func() { unsubCU() unsubSOff() unsubSOn() + unsubFollow() }, nil } diff --git a/wiki/Events.md b/wiki/Events.md index d9da712..be3ebc4 100644 --- a/wiki/Events.md +++ b/wiki/Events.md @@ -41,6 +41,17 @@ Fields: - `channel` - The channel the event occurred in +## `follow` + +User followed the channel. This event is not de-duplicated and therefore might be used to spam! (Only available when EventSub support is available!) + +Fields: + +- `channel` - The channel the event occurred in +- `followed_at` - Time object of the follow date +- `user_id` - ID of the newly following user +- `user` - The login-name of the user who followed + ## `giftpaidupgrade` User upgraded their gifted subscription into a paid one. This event does not contain any details about the tier of the paid subscription.