1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00

Fix timers and add config reloading

This commit is contained in:
Ash Keel 2021-11-24 12:43:55 +01:00
parent a1fef7caae
commit cec486fff2
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E

View file

@ -1,11 +1,15 @@
package twitch
import (
"context"
"math/rand"
"sync"
"time"
jsoniter "github.com/json-iterator/go"
irc "github.com/gempir/go-twitch-irc/v2"
"github.com/strimertul/strimertul/modules/database"
)
const BotTimersKey = "twitch/bot-modules/timers/config"
@ -51,6 +55,20 @@ func SetupTimers(bot *Bot) *BotTimerModule {
}
}
go bot.api.db.Subscribe(context.Background(), func(changed []database.ModifiedKV) error {
for _, kv := range changed {
if kv.Key == BotTimersKey {
err := jsoniter.ConfigFastest.Unmarshal(kv.Data, &mod.Config)
if err != nil {
bot.logger.WithError(err).Debug("error reloading timer config")
} else {
bot.logger.Info("reloaded timer config")
}
}
}
return nil
}, BotTimersKey)
bot.logger.WithField("timers", len(mod.Config.Timers)).Debug("loaded timers")
// Start goroutine for clearing message counters and running timers
@ -95,6 +113,7 @@ func (m *BotTimerModule) runTimers() {
if !ok {
// If it's the first time we're checking it, start the cooldown
lastTriggeredTime = time.Now()
m.lastTrigger[name] = lastTriggeredTime
}
minDelay := timer.MinimumDelay
if minDelay < 60 {