From 5a940f02970880572f906a531a079726e05f88e3 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 3 Aug 2022 13:35:27 -0400 Subject: [PATCH] timer command --- main.go | 18 ++++++++++++++++-- nun.go | 10 +++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index a300ed7..de45fb2 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "log" "os" "os/signal" + "strconv" "sync" mbl "git.saintnet.tech/stryan/matrixbotlib" @@ -45,6 +46,7 @@ func main() { //don't want to continue if we can't keep state panic(err) } + var nun *nunWatch matrixClient.Store = store syncer := matrixClient.Syncer.(*mautrix.DefaultSyncer) mbl.AcceptAllRoomInvites(matrixClient) @@ -67,8 +69,20 @@ func main() { } else { matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit) } + case "timer": + if len(cmd) <= 1 { + matrixClient.SendText(evt.RoomID, "!nun timer [time in minutes to wait between tries]") + return + } + time, err := strconv.Atoi(cmd[1]) + if err != nil { + matrixClient.SendText(evt.RoomID, "!nun timer needs integer timer") + return + } + nun.SetTimer(time) + matrixClient.SendText(evt.RoomID, "timer set") case "help": - matrixClient.SendText(evt.RoomID, "Supported commands: version, help") + matrixClient.SendText(evt.RoomID, "Supported commands: version, help,timer") default: //command not found matrixClient.SendText(evt.RoomID, "command not recognized") @@ -85,7 +99,7 @@ func main() { stop <- true }() - nun := newNunWatch(stop, matrixClient, 2) + nun = newNunWatch(stop, matrixClient, 2) wg.Add(1) go func() { diff --git a/nun.go b/nun.go index 5158046..20f21dd 100644 --- a/nun.go +++ b/nun.go @@ -20,9 +20,13 @@ func newNunWatch(stop chan bool, c *mautrix.Client, t int) *nunWatch { return &nunWatch{0, post{}, stop, c, t} } +func (n *nunWatch) SetTimer(t int) { + n.timer = t +} + func (n *nunWatch) Main() { ticker := time.NewTicker(time.Duration(n.timer) * time.Minute) - + curT := n.timer for { select { case <-n.stop: @@ -48,6 +52,10 @@ func (n *nunWatch) Main() { n.fail = 0 ticker.Reset(60 * time.Second) } + if curT != n.timer { + ticker.Reset(time.Duration(n.timer) * time.Minute) + curT = n.timer + } if n.curPost.Title != newPost.Title { n.curPost = newPost