timer command
This commit is contained in:
parent
ba048c35da
commit
5a940f0297
18
main.go
18
main.go
@ -4,6 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
mbl "git.saintnet.tech/stryan/matrixbotlib"
|
mbl "git.saintnet.tech/stryan/matrixbotlib"
|
||||||
@ -45,6 +46,7 @@ func main() {
|
|||||||
//don't want to continue if we can't keep state
|
//don't want to continue if we can't keep state
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
var nun *nunWatch
|
||||||
matrixClient.Store = store
|
matrixClient.Store = store
|
||||||
syncer := matrixClient.Syncer.(*mautrix.DefaultSyncer)
|
syncer := matrixClient.Syncer.(*mautrix.DefaultSyncer)
|
||||||
mbl.AcceptAllRoomInvites(matrixClient)
|
mbl.AcceptAllRoomInvites(matrixClient)
|
||||||
@ -67,8 +69,20 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
matrixClient.SendText(evt.RoomID, "NunBot version "+GitCommit)
|
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":
|
case "help":
|
||||||
matrixClient.SendText(evt.RoomID, "Supported commands: version, help")
|
matrixClient.SendText(evt.RoomID, "Supported commands: version, help,timer")
|
||||||
default:
|
default:
|
||||||
//command not found
|
//command not found
|
||||||
matrixClient.SendText(evt.RoomID, "command not recognized")
|
matrixClient.SendText(evt.RoomID, "command not recognized")
|
||||||
@ -85,7 +99,7 @@ func main() {
|
|||||||
stop <- true
|
stop <- true
|
||||||
}()
|
}()
|
||||||
|
|
||||||
nun := newNunWatch(stop, matrixClient, 2)
|
nun = newNunWatch(stop, matrixClient, 2)
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
|
||||||
|
10
nun.go
10
nun.go
@ -20,9 +20,13 @@ func newNunWatch(stop chan bool, c *mautrix.Client, t int) *nunWatch {
|
|||||||
return &nunWatch{0, post{}, stop, c, t}
|
return &nunWatch{0, post{}, stop, c, t}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *nunWatch) SetTimer(t int) {
|
||||||
|
n.timer = t
|
||||||
|
}
|
||||||
|
|
||||||
func (n *nunWatch) Main() {
|
func (n *nunWatch) Main() {
|
||||||
ticker := time.NewTicker(time.Duration(n.timer) * time.Minute)
|
ticker := time.NewTicker(time.Duration(n.timer) * time.Minute)
|
||||||
|
curT := n.timer
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-n.stop:
|
case <-n.stop:
|
||||||
@ -48,6 +52,10 @@ func (n *nunWatch) Main() {
|
|||||||
n.fail = 0
|
n.fail = 0
|
||||||
ticker.Reset(60 * time.Second)
|
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 {
|
if n.curPost.Title != newPost.Title {
|
||||||
n.curPost = newPost
|
n.curPost = newPost
|
||||||
|
Loading…
Reference in New Issue
Block a user