mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 16:50:01 +00:00
Fix: Permits must only count for one channel
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
bdd47377bc
commit
f2f4d43412
3 changed files with 16 additions and 4 deletions
|
@ -153,7 +153,7 @@ func (r rule) Matches(m *irc.Message, event *string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check on permit
|
// Check on permit
|
||||||
if r.DisableOnPermit && timerStore.HasPermit(m.User) {
|
if r.DisableOnPermit && timerStore.HasPermit(m.Params[0], m.User) {
|
||||||
logger.Trace("Non-Match: Permit")
|
logger.Trace("Non-Match: Permit")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
2
irc.go
2
irc.go
|
@ -125,7 +125,7 @@ func (i ircHandler) handlePermit(m *irc.Message) {
|
||||||
username := msgParts[1]
|
username := msgParts[1]
|
||||||
|
|
||||||
log.WithField("user", username).Debug("Added permit")
|
log.WithField("user", username).Debug("Added permit")
|
||||||
timerStore.Add(timerStore.NormalizeUsername(username))
|
timerStore.AddPermit(m.Params[0], username)
|
||||||
|
|
||||||
go handleMessage(i.c, m, eventTypePermit)
|
go handleMessage(i.c, m, eventTypePermit)
|
||||||
}
|
}
|
||||||
|
|
16
timers.go
16
timers.go
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -27,6 +29,10 @@ func (t *timer) Add(id string) {
|
||||||
t.timers[id] = time.Now()
|
t.timers[id] = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *timer) AddPermit(channel, username string) {
|
||||||
|
t.Add(t.getPermitTimerKey(channel, username))
|
||||||
|
}
|
||||||
|
|
||||||
func (t *timer) Has(id string, validity time.Duration) bool {
|
func (t *timer) Has(id string, validity time.Duration) bool {
|
||||||
t.lock.RLock()
|
t.lock.RLock()
|
||||||
defer t.lock.RUnlock()
|
defer t.lock.RUnlock()
|
||||||
|
@ -34,10 +40,16 @@ func (t *timer) Has(id string, validity time.Duration) bool {
|
||||||
return time.Since(t.timers[id]) < validity
|
return time.Since(t.timers[id]) < validity
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *timer) HasPermit(username string) bool {
|
func (t *timer) HasPermit(channel, username string) bool {
|
||||||
return t.Has(t.NormalizeUsername(username), config.PermitTimeout)
|
return t.Has(t.getPermitTimerKey(channel, username), config.PermitTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t timer) NormalizeUsername(username string) string {
|
func (t timer) NormalizeUsername(username string) string {
|
||||||
return strings.ToLower(strings.TrimLeft(username, "@"))
|
return strings.ToLower(strings.TrimLeft(username, "@"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t timer) getPermitTimerKey(channel, username string) string {
|
||||||
|
h := sha256.New()
|
||||||
|
fmt.Fprintf(h, "%s:%s", channel, t.NormalizeUsername(username))
|
||||||
|
return fmt.Sprintf("sha256:%x", h.Sum(nil))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue