Fetch me from API + low balance warnings

This commit is contained in:
ekzyis 2024-10-19 19:22:08 +02:00
parent e7498352b8
commit ec8384f282
4 changed files with 44 additions and 10 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/ekzyis/chessbot
go 1.23.0 go 1.23.0
require ( require (
github.com/ekzyis/snappy v0.6.2 github.com/ekzyis/snappy v0.7.0
github.com/mattn/go-sqlite3 v1.14.23 github.com/mattn/go-sqlite3 v1.14.23
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
golang.org/x/image v0.20.0 golang.org/x/image v0.20.0

4
go.sum
View File

@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ekzyis/snappy v0.6.2 h1:iOPgoS0cSUNk8leQJku0bBgnsha/+DcYUwTYqGLINY4= github.com/ekzyis/snappy v0.7.0 h1:RcFTUHdZFTBFOnh6cG9HCL/RPK6L13qdxDrjBNZFjEM=
github.com/ekzyis/snappy v0.6.2/go.mod h1:UksYI0dU0+cnzz0LQjWB1P0QQP/ghx47e4atP99a5Lk= github.com/ekzyis/snappy v0.7.0/go.mod h1:UksYI0dU0+cnzz0LQjWB1P0QQP/ghx47e4atP99a5Lk=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0= github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

47
main.go
View File

@ -14,21 +14,54 @@ import (
) )
var ( var (
c = sn.GetClient() c = sn.GetClient()
// TODO: fetch our id from SN API me *sn.User
// prod: 25176 | local: 21858
meId = 25176
) )
func main() { func main() {
for { for {
updateMe()
tickGameStart(c) tickGameStart(c)
tickGameProgress(c) tickGameProgress(c)
time.Sleep(15 * time.Second) time.Sleep(15 * time.Second)
} }
} }
func updateMe() {
var (
oldMe *sn.User
err error
warnThreshold = 100
)
maybeWarn := func() {
if me.Privates.Sats < warnThreshold {
log.Printf("~~~ warning: low balance ~~~\n")
}
}
if me == nil {
// make sure first update is successful
if me, err = c.Me(); err != nil {
log.Fatalf("failed to fetch me: %v\n", err)
}
log.Printf("fetched me: id=%d name=%s balance=%d\n", me.Id, me.Name, me.Privates.Sats)
maybeWarn()
return
}
oldMe = me
if me, err = c.Me(); err != nil {
log.Printf("failed to update me: %v\n", err)
me = oldMe
} else {
log.Printf("updated me. balance: %d\n", me.Privates.Sats)
}
maybeWarn()
}
func tickGameStart(c *sn.Client) { func tickGameStart(c *sn.Client) {
var ( var (
mentions []sn.Notification mentions []sn.Notification
@ -98,7 +131,7 @@ func tickGameProgress(c *sn.Client) {
if parent, err := c.Item(n.Item.ParentId); err != nil { if parent, err := c.Item(n.Item.ParentId); err != nil {
log.Printf("failed to fetch parent %d of %d\n", n.Item.ParentId, n.Item.Id) log.Printf("failed to fetch parent %d of %d\n", n.Item.ParentId, n.Item.Id)
continue continue
} else if parent.User.Id != meId { } else if parent.User.Id != me.Id {
log.Printf("ignoring nested reply %d\n", n.Item.Id) log.Printf("ignoring nested reply %d\n", n.Item.Id)
continue continue
} }
@ -183,7 +216,7 @@ func handleGameProgress(req *sn.Item) error {
} }
for _, item := range thread { for _, item := range thread {
if item.User.Id == meId { if item.User.Id == me.Id {
continue continue
} }
@ -312,5 +345,5 @@ func isRecent(t time.Time) bool {
} }
func alreadyHandled(id int) (bool, error) { func alreadyHandled(id int) (bool, error) {
return db.ItemHasReply(id, meId) return db.ItemHasReply(id, me.Id)
} }

View File

@ -13,6 +13,7 @@ import (
type Client = snappy.Client type Client = snappy.Client
type Notification = snappy.Notification type Notification = snappy.Notification
type Item = snappy.Item type Item = snappy.Item
type User = snappy.User
var ( var (
c *Client c *Client