nunbot/nun.go
Steve 45b50e4733
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
switch to botlib, waitgroups
2022-07-17 18:15:29 -04:00

58 lines
1.1 KiB
Go

package main
import (
"fmt"
"log"
"time"
"maunium.net/go/mautrix"
)
type nunWatch struct {
fail int
curPost post
stop chan bool
client *mautrix.Client
}
func newNunWatch(stop chan bool, c *mautrix.Client) *nunWatch {
return &nunWatch{0, post{}, stop, c}
}
func (n *nunWatch) Main() {
ticker := time.NewTicker(30 * time.Second)
for {
select {
case <-n.stop:
return
case <-ticker.C:
if n.fail > 5 {
log.Fatal("fail count too high; ending loop")
return
}
newPost, err := getNewestPost("LittleNuns")
if err != nil {
log.Printf("error getting newest post: %v", err)
log.Println("skipping this cycle, incrementing fail count")
n.fail++
continue
}
n.fail = 0
if n.curPost.Title != newPost.Title {
n.curPost = newPost
roomResp, err := n.client.JoinedRooms()
if err != nil {
log.Printf("error getting joined rooms: %v", err)
continue
}
rooms := roomResp.JoinedRooms
for _, room := range rooms {
n.client.SendText(room, fmt.Sprintf("%v\n%v", n.curPost.Title, n.curPost.Link))
}
}
}
}
}