Fetch me from API + low balance warnings
This commit is contained in:
parent
e7498352b8
commit
ec8384f282
2
go.mod
2
go.mod
|
@ -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
4
go.sum
|
@ -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
47
main.go
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue