nunbot/nun.go

98 lines
2.2 KiB
Go
Raw Normal View History

2022-07-17 18:15:29 -04:00
package main
import (
"fmt"
"time"
2023-10-10 21:01:01 -04:00
"github.com/charmbracelet/log"
2022-07-17 18:15:29 -04:00
"maunium.net/go/mautrix"
)
type nunWatch struct {
fail int
curPost post
stop chan bool
client *mautrix.Client
2022-08-02 13:02:12 -04:00
timer int
2022-07-17 18:15:29 -04:00
}
2022-08-02 13:02:12 -04:00
func newNunWatch(stop chan bool, c *mautrix.Client, t int) *nunWatch {
return &nunWatch{0, post{}, stop, c, t}
2022-07-17 18:15:29 -04:00
}
2022-08-03 13:35:27 -04:00
func (n *nunWatch) SetTimer(t int) {
n.timer = t
2022-08-21 16:43:52 -04:00
n.fail = 0
2022-08-03 13:35:27 -04:00
}
2022-07-17 18:15:29 -04:00
func (n *nunWatch) Main() {
2022-08-02 13:02:12 -04:00
ticker := time.NewTicker(time.Duration(n.timer) * time.Minute)
2022-08-03 13:35:27 -04:00
curT := n.timer
2022-07-17 18:15:29 -04:00
for {
select {
case <-n.stop:
return
case <-ticker.C:
2022-09-01 13:36:33 -04:00
2022-07-17 18:15:29 -04:00
newPost, err := getNewestPost("LittleNuns")
if err != nil {
2023-10-10 21:01:01 -04:00
log.Warn("error getting newest post:", "error", err)
log.Warn("skipping this cycle, incrementing fail count")
2022-07-17 18:15:29 -04:00
n.fail++
2022-09-01 13:36:33 -04:00
if n.fail > 5 {
2023-10-10 21:01:01 -04:00
log.Fatal("failgithub.com/charmbracelet/log count too high; ending loop")
2022-09-01 13:36:33 -04:00
return
} else if n.fail > 3 {
2023-10-10 21:01:01 -04:00
log.Warnf("over three failures, increasing tick time to %v minutes", 3*n.timer)
2022-11-21 18:21:01 -05:00
ticker.Reset(3 * time.Duration(n.timer) * time.Minute)
2022-09-01 13:36:33 -04:00
break
} else {
2023-10-10 21:01:01 -04:00
log.Warnf("fail count %v", n.fail)
2022-09-01 13:36:33 -04:00
break
}
2022-07-17 18:15:29 -04:00
}
2022-09-02 19:57:34 -04:00
n.fail = 0
2022-11-21 18:21:01 -05:00
ticker.Reset(time.Duration(n.timer) * time.Minute)
2022-08-03 13:35:27 -04:00
if curT != n.timer {
2023-10-10 21:01:01 -04:00
log.Info("updating nunwatch timer")
2022-08-03 13:35:27 -04:00
ticker.Reset(time.Duration(n.timer) * time.Minute)
2022-11-21 18:21:01 -05:00
n.fail = 0
2022-08-03 13:35:27 -04:00
curT = n.timer
}
2022-07-17 18:15:29 -04:00
if n.curPost.Title != newPost.Title {
n.curPost = newPost
2022-07-29 16:02:23 -04:00
var cur post
err := n.client.GetAccountData("nun.newest", &cur)
if err != nil {
2023-10-10 21:01:01 -04:00
log.Warnf("tried to get saved account data but failed %v", err)
2022-07-29 16:02:23 -04:00
cur.Number = 0
}
2022-07-17 18:15:29 -04:00
roomResp, err := n.client.JoinedRooms()
if err != nil {
2023-10-10 21:01:01 -04:00
log.Warnf("error getting joined rooms: %v", err)
2022-08-10 14:14:52 -04:00
break
2022-07-17 18:15:29 -04:00
}
rooms := roomResp.JoinedRooms
for _, room := range rooms {
2022-07-29 16:02:23 -04:00
if n.curPost.Number != cur.Number {
2023-10-10 21:01:01 -04:00
log.Infof("posting %v", n.curPost.Number)
_, err := n.client.SendText(room, fmt.Sprintf("%v\n%v", n.curPost.Title, n.curPost.Link))
if err != nil {
log.Printf("err sending to room: %v", err)
}
2022-07-29 16:02:23 -04:00
if n.curPost.Number > 0 {
err := n.client.SetAccountData("nun.newest", n.curPost)
if err != nil {
2023-10-10 21:01:01 -04:00
log.Warnf("error saving current post: %v", err)
2022-07-29 16:02:23 -04:00
}
}
}
2022-07-17 18:15:29 -04:00
}
}
}
}
}