twitch-bot/timers.go

51 lines
1.3 KiB
Go
Raw Normal View History

2020-12-21 00:32:39 +00:00
package main
import (
"crypto/sha256"
"fmt"
2020-12-21 00:32:39 +00:00
"strings"
"time"
"github.com/Luzifer/twitch-bot/plugins"
)
var timerStore plugins.TimerStore = newTimer()
type timer struct{}
2020-12-21 00:32:39 +00:00
func newTimer() *timer {
return &timer{}
2020-12-21 00:32:39 +00:00
}
// Cooldown timer
2020-12-21 00:32:39 +00:00
func (t *timer) AddCooldown(tt plugins.TimerType, limiter, ruleID string, expiry time.Time) {
store.SetTimer(plugins.TimerTypeCooldown, t.getCooldownTimerKey(tt, limiter, ruleID), expiry)
2020-12-21 00:32:39 +00:00
}
func (t *timer) InCooldown(tt plugins.TimerType, limiter, ruleID string) bool {
return store.HasTimer(t.getCooldownTimerKey(tt, limiter, ruleID))
}
func (timer) getCooldownTimerKey(tt plugins.TimerType, limiter, ruleID string) string {
h := sha256.New()
fmt.Fprintf(h, "%d:%s:%s", tt, limiter, ruleID)
return fmt.Sprintf("sha256:%x", h.Sum(nil))
2020-12-21 00:32:39 +00:00
}
// Permit timer
func (t *timer) AddPermit(channel, username string) {
store.SetTimer(plugins.TimerTypePermit, t.getPermitTimerKey(channel, username), time.Now().Add(config.PermitTimeout))
2020-12-21 00:32:39 +00:00
}
func (t *timer) HasPermit(channel, username string) bool {
return store.HasTimer(t.getPermitTimerKey(channel, username))
2020-12-21 00:32:39 +00:00
}
func (timer) getPermitTimerKey(channel, username string) string {
h := sha256.New()
fmt.Fprintf(h, "%d:%s:%s", plugins.TimerTypePermit, channel, strings.ToLower(strings.TrimLeft(username, "@")))
return fmt.Sprintf("sha256:%x", h.Sum(nil))
}