mirror of
https://github.com/Luzifer/twitch-manager.git
synced 2024-12-20 20:01:18 +00:00
Add sub events
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
42749b2501
commit
5293fc8ea2
2 changed files with 84 additions and 10 deletions
1
api.go
1
api.go
|
@ -21,6 +21,7 @@ const (
|
|||
msgTypeHost string = "host"
|
||||
msgTypeRaid string = "raid"
|
||||
msgTypeStore string = "store"
|
||||
msgTypeSub string = "sub"
|
||||
)
|
||||
|
||||
var subscriptions = newSubscriptionStore()
|
||||
|
|
93
irc.go
93
irc.go
|
@ -7,6 +7,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-irc/irc"
|
||||
|
@ -185,24 +186,96 @@ func (ircHandler) handleTwitchUsernotice(m *irc.Message) {
|
|||
"trailing": m.Trailing,
|
||||
}).Debug("IRC USERNOTICE event")
|
||||
|
||||
displayName, ok := m.Tags["msg-param-displayName"]
|
||||
if !ok {
|
||||
displayName = m.Tags["login"]
|
||||
}
|
||||
|
||||
switch m.Tags["msg-id"] {
|
||||
case "":
|
||||
// Notices SHOULD have msg-id tags...
|
||||
log.WithField("msg", m).Warn("Received usernotice without msg-id")
|
||||
|
||||
case "raid":
|
||||
log.WithFields(log.Fields{
|
||||
"from": m.Tags["login"],
|
||||
"viewercount": m.Tags["msg-param-viewerCount"],
|
||||
}).Info("Incoming raid")
|
||||
|
||||
subscriptions.SendAllSockets(msgTypeRaid, map[string]interface{}{
|
||||
"from": m.Tags["msg-param-displayName"],
|
||||
fields := map[string]interface{}{
|
||||
"from": displayName,
|
||||
"viewerCount": m.Tags["msg-param-viewerCount"],
|
||||
})
|
||||
}
|
||||
|
||||
case "resub":
|
||||
// FIXME: Fill in later
|
||||
log.WithFields(log.Fields(fields)).Info("Incoming raid")
|
||||
subscriptions.SendAllSockets(msgTypeRaid, fields)
|
||||
|
||||
case "sub", "resub":
|
||||
fields := map[string]interface{}{
|
||||
"from": displayName,
|
||||
"is_resub": m.Tags["msg-id"] == "resub",
|
||||
"paid_for": m.Tags["msg-param-multimonth-duration"],
|
||||
"streak": m.Tags["msg-param-streak-months"],
|
||||
"tier": m.Tags["msg-param-sub-plan"],
|
||||
"total": m.Tags["msg-param-cumulative-months"],
|
||||
}
|
||||
|
||||
// Update store
|
||||
strDisplayName := string(displayName)
|
||||
var duration int64
|
||||
if v, err := strconv.ParseInt(string(m.Tags["msg-param-cumulative-months"]), 10, 64); err == nil {
|
||||
duration = v
|
||||
}
|
||||
|
||||
store.Subs.Last = &strDisplayName
|
||||
store.Subs.LastDuration = duration
|
||||
|
||||
// Send update to sockets
|
||||
log.WithFields(log.Fields(fields)).Info("New subscriber")
|
||||
subscriptions.SendAllSockets(msgTypeSub, fields)
|
||||
|
||||
// Execute store save
|
||||
if err := store.Save(cfg.StoreFile); err != nil {
|
||||
log.WithError(err).Error("Unable to update persistent store")
|
||||
}
|
||||
|
||||
if err := subscriptions.SendAllSockets(msgTypeStore, store); err != nil {
|
||||
log.WithError(err).Error("Unable to send update to all sockets")
|
||||
}
|
||||
|
||||
case "subgift", "anonsubgift":
|
||||
toName, ok := m.Tags["msg-param-recipient-display-name"]
|
||||
if !ok {
|
||||
toName = m.Tags["msg-param-recipient-user-name"]
|
||||
}
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"from": displayName,
|
||||
"is_anon": m.Tags["msg-id"] == "anonsubgift",
|
||||
"gift_to": toName,
|
||||
"paid_for": m.Tags["msg-param-gift-months"],
|
||||
"streak": m.Tags["msg-param-streak-months"],
|
||||
"tier": m.Tags["msg-param-sub-plan"],
|
||||
"total": m.Tags["msg-param-months"],
|
||||
}
|
||||
|
||||
// Update store
|
||||
strDisplayName := string(displayName)
|
||||
var duration int64
|
||||
if v, err := strconv.ParseInt(string(m.Tags["msg-param-months"]), 10, 64); err == nil {
|
||||
duration = v
|
||||
}
|
||||
|
||||
store.Subs.Last = &strDisplayName
|
||||
store.Subs.LastDuration = duration
|
||||
|
||||
// Send update to sockets
|
||||
log.WithFields(log.Fields(fields)).Info("New sub-gift")
|
||||
subscriptions.SendAllSockets(msgTypeSub, fields)
|
||||
|
||||
// Execute store save
|
||||
if err := store.Save(cfg.StoreFile); err != nil {
|
||||
log.WithError(err).Error("Unable to update persistent store")
|
||||
}
|
||||
|
||||
if err := subscriptions.SendAllSockets(msgTypeStore, store); err != nil {
|
||||
log.WithError(err).Error("Unable to send update to all sockets")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue