2020-12-21 00:32:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-12-24 14:59:11 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
2020-12-21 00:32:39 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-01-24 00:46:40 +00:00
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/plugins"
|
2021-01-24 00:46:40 +00:00
|
|
|
)
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
var timerStore plugins.TimerStore = newTimer()
|
2021-01-24 00:46:40 +00:00
|
|
|
|
2021-06-28 22:01:26 +00:00
|
|
|
type timer struct{}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
|
|
func newTimer() *timer {
|
2021-06-28 22:01:26 +00:00
|
|
|
return &timer{}
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
// Cooldown timer
|
2020-12-21 00:32:39 +00:00
|
|
|
|
2021-08-19 13:33:56 +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
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (t *timer) InCooldown(tt plugins.TimerType, limiter, ruleID string) bool {
|
2021-06-28 22:01:26 +00:00
|
|
|
return store.HasTimer(t.getCooldownTimerKey(tt, limiter, ruleID))
|
2020-12-24 14:59:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
func (timer) getCooldownTimerKey(tt plugins.TimerType, limiter, ruleID string) string {
|
2021-01-24 00:46:40 +00:00
|
|
|
h := sha256.New()
|
2021-06-07 20:20:19 +00:00
|
|
|
fmt.Fprintf(h, "%d:%s:%s", tt, limiter, ruleID)
|
2021-01-24 00:46:40 +00:00
|
|
|
return fmt.Sprintf("sha256:%x", h.Sum(nil))
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
// Permit timer
|
|
|
|
|
|
|
|
func (t *timer) AddPermit(channel, username string) {
|
2021-08-19 13:33:56 +00:00
|
|
|
store.SetTimer(plugins.TimerTypePermit, t.getPermitTimerKey(channel, username), time.Now().Add(config.PermitTimeout))
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:46:40 +00:00
|
|
|
func (t *timer) HasPermit(channel, username string) bool {
|
2021-06-28 22:01:26 +00:00
|
|
|
return store.HasTimer(t.getPermitTimerKey(channel, username))
|
2020-12-21 00:32:39 +00:00
|
|
|
}
|
2020-12-24 14:59:11 +00:00
|
|
|
|
2021-06-28 22:01:26 +00:00
|
|
|
func (timer) getPermitTimerKey(channel, username string) string {
|
2020-12-24 14:59:11 +00:00
|
|
|
h := sha256.New()
|
2021-08-19 13:33:56 +00:00
|
|
|
fmt.Fprintf(h, "%d:%s:%s", plugins.TimerTypePermit, channel, strings.ToLower(strings.TrimLeft(username, "@")))
|
2020-12-24 14:59:11 +00:00
|
|
|
return fmt.Sprintf("sha256:%x", h.Sum(nil))
|
|
|
|
}
|