timer command

This commit is contained in:
stryan 2022-08-03 13:35:27 -04:00
parent ba048c35da
commit 5a940f0297
2 changed files with 25 additions and 3 deletions

18
main.go
View File

@ -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() {

10
nun.go
View File

@ -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