mirror of
https://github.com/Luzifer/twitch-manager.git
synced 2024-12-20 20:01:18 +00:00
Add bit events
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
54224d2118
commit
a7337cf9f1
3 changed files with 52 additions and 0 deletions
1
api.go
1
api.go
|
@ -18,6 +18,7 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
msgTypeBits string = "bits"
|
||||
msgTypeHost string = "host"
|
||||
msgTypeRaid string = "raid"
|
||||
msgTypeStore string = "store"
|
||||
|
|
46
irc.go
46
irc.go
|
@ -178,6 +178,52 @@ func (ircHandler) handleTwitchPrivmsg(m *irc.Message) {
|
|||
"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) {
|
||||
|
|
|
@ -17,6 +17,11 @@ type subscriber 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 {
|
||||
LastDonator *string `json:"last_donator"`
|
||||
LastAmount float64 `json:"last_amount"`
|
||||
|
|
Loading…
Reference in a new issue