Add bit events

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-01-04 00:31:53 +01:00
parent 54224d2118
commit a7337cf9f1
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
3 changed files with 52 additions and 0 deletions

1
api.go
View file

@ -18,6 +18,7 @@ import (
) )
const ( const (
msgTypeBits string = "bits"
msgTypeHost string = "host" msgTypeHost string = "host"
msgTypeRaid string = "raid" msgTypeRaid string = "raid"
msgTypeStore string = "store" msgTypeStore string = "store"

46
irc.go
View file

@ -178,6 +178,52 @@ func (ircHandler) handleTwitchPrivmsg(m *irc.Message) {
"viewerCount": matches[2], "viewerCount": matches[2],
}) })
} }
// Handle bit-messages
if bitString, ok := m.Tags["bits"]; ok && bitString != "" {
bitAmount, err := strconv.ParseInt(string(bitString), 10, 64)
if err != nil {
log.WithError(err).Error("Unable to parse bit-string")
return
}
displayName, ok := m.Tags["msg-param-displayName"]
if !ok {
displayName = irc.TagValue(m.User)
}
strDisplayName := string(displayName)
fields := map[string]interface{}{
"from": displayName,
"amount": bitAmount,
}
store.WithModLock(func() error {
store.BitDonations.LastDonator = &strDisplayName
store.BitDonations.LastAmount = bitAmount
if store.BitDonations.TotalAmounts == nil {
store.BitDonations.TotalAmounts = map[string]int64{}
}
store.BitDonations.TotalAmounts[m.User] += bitAmount
fields["total_amount"] = store.BitDonations.TotalAmounts[m.User]
return nil
})
// Send update to sockets
log.WithFields(log.Fields(fields)).Info("Bit donation")
subscriptions.SendAllSockets(msgTypeBits, fields)
// Execute store save
if err := store.Save(cfg.StoreFile); err != nil {
log.WithError(err).Error("Unable to update persistent store")
}
if err := store.WithModRLock(func() error { return subscriptions.SendAllSockets(msgTypeStore, store) }); err != nil {
log.WithError(err).Error("Unable to send update to all sockets")
}
}
} }
func (ircHandler) handleTwitchUsernotice(m *irc.Message) { func (ircHandler) handleTwitchUsernotice(m *irc.Message) {

View file

@ -17,6 +17,11 @@ type subscriber struct {
} }
type storage struct { type storage struct {
BitDonations struct {
LastDonator *string `json:"last_donator"`
LastAmount int64 `json:"last_amount"`
TotalAmounts map[string]int64 `json:"total_amounts"`
} `json:"bit_donations"`
Donations struct { Donations struct {
LastDonator *string `json:"last_donator"` LastDonator *string `json:"last_donator"`
LastAmount float64 `json:"last_amount"` LastAmount float64 `json:"last_amount"`