mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-20 20:01:17 +00:00
83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type timerType uint8
|
|
|
|
const (
|
|
timerTypePermit timerType = iota
|
|
timerTypeCooldown
|
|
)
|
|
|
|
var timerStore = newTimer()
|
|
|
|
type timerEntry struct {
|
|
kind timerType
|
|
time time.Time
|
|
}
|
|
|
|
type timer struct {
|
|
timers map[string]timerEntry
|
|
lock *sync.RWMutex
|
|
}
|
|
|
|
func newTimer() *timer {
|
|
return &timer{
|
|
timers: map[string]timerEntry{},
|
|
lock: new(sync.RWMutex),
|
|
}
|
|
}
|
|
|
|
// Cooldown timer
|
|
|
|
func (t *timer) AddCooldown(tt timerType, limiter, ruleID string) {
|
|
t.add(timerTypeCooldown, t.getCooldownTimerKey(tt, limiter, ruleID))
|
|
}
|
|
|
|
func (t *timer) InCooldown(tt timerType, limiter, ruleID string, cooldown time.Duration) bool {
|
|
return t.has(t.getCooldownTimerKey(tt, limiter, ruleID), cooldown)
|
|
}
|
|
|
|
func (t timer) getCooldownTimerKey(tt 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))
|
|
}
|
|
|
|
// Permit timer
|
|
|
|
func (t *timer) AddPermit(channel, username string) {
|
|
t.add(timerTypePermit, t.getPermitTimerKey(channel, username))
|
|
}
|
|
|
|
func (t *timer) HasPermit(channel, username string) bool {
|
|
return t.has(t.getPermitTimerKey(channel, username), config.PermitTimeout)
|
|
}
|
|
|
|
func (t timer) getPermitTimerKey(channel, username string) string {
|
|
h := sha256.New()
|
|
fmt.Fprintf(h, "%d:%s:%s", timerTypePermit, channel, strings.ToLower(strings.TrimLeft(username, "@")))
|
|
return fmt.Sprintf("sha256:%x", h.Sum(nil))
|
|
}
|
|
|
|
// Generic
|
|
|
|
func (t *timer) add(kind timerType, id string) {
|
|
t.lock.Lock()
|
|
defer t.lock.Unlock()
|
|
|
|
t.timers[id] = timerEntry{kind: kind, time: time.Now()}
|
|
}
|
|
|
|
func (t *timer) has(id string, validity time.Duration) bool {
|
|
t.lock.RLock()
|
|
defer t.lock.RUnlock()
|
|
|
|
return time.Since(t.timers[id].time) < validity
|
|
}
|