From ec8384f2820a4262cc1f8921057a34b19ba06f4d Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 19 Oct 2024 19:22:08 +0200 Subject: [PATCH] Fetch me from API + low balance warnings --- go.mod | 2 +- go.sum | 4 ++-- main.go | 47 ++++++++++++++++++++++++++++++++++++++++------- sn/sn.go | 1 + 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 6391a77..a8aca3e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/ekzyis/chessbot go 1.23.0 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/stretchr/testify v1.9.0 golang.org/x/image v0.20.0 diff --git a/go.sum b/go.sum index 3691bec..e6118be 100644 --- a/go.sum +++ b/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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/ekzyis/snappy v0.6.2 h1:iOPgoS0cSUNk8leQJku0bBgnsha/+DcYUwTYqGLINY4= -github.com/ekzyis/snappy v0.6.2/go.mod h1:UksYI0dU0+cnzz0LQjWB1P0QQP/ghx47e4atP99a5Lk= +github.com/ekzyis/snappy v0.7.0 h1:RcFTUHdZFTBFOnh6cG9HCL/RPK6L13qdxDrjBNZFjEM= +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/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/main.go b/main.go index 03c1416..3db4b32 100644 --- a/main.go +++ b/main.go @@ -14,21 +14,54 @@ import ( ) var ( - c = sn.GetClient() - // TODO: fetch our id from SN API - // prod: 25176 | local: 21858 - meId = 25176 + c = sn.GetClient() + me *sn.User ) func main() { for { + updateMe() tickGameStart(c) tickGameProgress(c) 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) { var ( mentions []sn.Notification @@ -98,7 +131,7 @@ func tickGameProgress(c *sn.Client) { 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) continue - } else if parent.User.Id != meId { + } else if parent.User.Id != me.Id { log.Printf("ignoring nested reply %d\n", n.Item.Id) continue } @@ -183,7 +216,7 @@ func handleGameProgress(req *sn.Item) error { } for _, item := range thread { - if item.User.Id == meId { + if item.User.Id == me.Id { continue } @@ -312,5 +345,5 @@ func isRecent(t time.Time) bool { } func alreadyHandled(id int) (bool, error) { - return db.ItemHasReply(id, meId) + return db.ItemHasReply(id, me.Id) } diff --git a/sn/sn.go b/sn/sn.go index 216b626..78dbfe8 100644 --- a/sn/sn.go +++ b/sn/sn.go @@ -13,6 +13,7 @@ import ( type Client = snappy.Client type Notification = snappy.Notification type Item = snappy.Item +type User = snappy.User var ( c *Client