Make the deadline configurable

This commit is contained in:
2020-01-01 16:18:13 +00:00
parent c31e8d9f51
commit 90dc6edfad
2 changed files with 7 additions and 2 deletions

View File

@@ -22,6 +22,8 @@ type CommandDeadline struct {
Deadline time.Time Deadline time.Time
} }
var Cooldown = 2 * time.Minute // default cooldown is 2 minutes
func NewCommand(next hugot.CommandHandler) hugot.CommandHandler { func NewCommand(next hugot.CommandHandler) hugot.CommandHandler {
return &CommandDeadline{CommandHandler: next, Deadline: time.Now().UTC()} return &CommandDeadline{CommandHandler: next, Deadline: time.Now().UTC()}
} }
@@ -33,7 +35,7 @@ func NewHears(next hugot.HearsHandler) hugot.HearsHandler {
func (i *HearsDeadline) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, matches [][]string) { func (i *HearsDeadline) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, matches [][]string) {
now := time.Now().UTC() now := time.Now().UTC()
if now.After(i.Deadline) { if now.After(i.Deadline) {
i.Deadline = now.Add(2 * time.Minute) i.Deadline = now.Add(Cooldown)
i.HearsHandler.Heard(ctx, w, m, matches) i.HearsHandler.Heard(ctx, w, m, matches)
return return
} }
@@ -45,7 +47,7 @@ func (i *HearsDeadline) Heard(ctx context.Context, w hugot.ResponseWriter, m *hu
func (i *CommandDeadline) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error { func (i *CommandDeadline) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
now := time.Now().UTC() now := time.Now().UTC()
if now.After(i.Deadline) { if now.After(i.Deadline) {
i.Deadline = now.Add(2 * time.Minute) i.Deadline = now.Add(Cooldown)
return i.CommandHandler.Command(ctx, w, m) return i.CommandHandler.Command(ctx, w, m)
} }

View File

@@ -21,6 +21,7 @@ import (
var ( var (
channels = flag.String("channels", "##testing,##test", "Channels to join (separated by comma)") channels = flag.String("channels", "##testing,##test", "Channels to join (separated by comma)")
cooldown = flag.Int("cooldown", 120, "Command rate-limit, in seconds")
host = flag.String("host", "chat.freenode.net", "Server host[:port]") host = flag.String("host", "chat.freenode.net", "Server host[:port]")
ident = flag.String("ident", "lysenko", "Lysenko Bot") ident = flag.String("ident", "lysenko", "Lysenko Bot")
nick = flag.String("nick", "lysenko", "Lysenko Bot") nick = flag.String("nick", "lysenko", "Lysenko Bot")
@@ -71,6 +72,8 @@ func main() {
} }
defer db.DB.Close() defer db.DB.Close()
deadline.Cooldown = time.Second * time.Duration(*cooldown)
mux := hugot.DefaultMux mux := hugot.DefaultMux
mux.HandleHears(&quotedb.AddQuoteHandler{QuoteDB: db}) mux.HandleHears(&quotedb.AddQuoteHandler{QuoteDB: db})
mux.HandleHears(deadline.NewHears(&quotedb.LastQuoteHandler{QuoteDB: db})) mux.HandleHears(deadline.NewHears(&quotedb.LastQuoteHandler{QuoteDB: db}))