From d0af4542f7fdac6c38ae164dbcabd93a1e3e9bc1 Mon Sep 17 00:00:00 2001 From: wood chen Date: Wed, 18 Sep 2024 02:04:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84(service/binance.go=20&=20ser?= =?UTF-8?q?vice/guard.go):=20=E4=BC=98=E5=8C=96=E6=97=B6=E5=8C=BA=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E5=A2=9E=E5=BC=BA=E4=BB=A3=E7=A0=81=E5=81=A5?= =?UTF-8?q?=E5=A3=AE=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/binance.go | 27 ++++++++++++++++----------- service/guard.go | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/service/binance.go b/service/binance.go index 5e9c51e..9e56d94 100644 --- a/service/binance.go +++ b/service/binance.go @@ -28,6 +28,9 @@ func init() { chatID = mustParseInt64(os.Getenv("CHAT_ID")) symbols = strings.Split(os.Getenv("SYMBOLS"), ",") + // 初始化 singaporeTZ + singaporeTZ = time.FixedZone("Asia/Singapore", 8*60*60) // UTC+8 + bot, err = tgbotapi.NewBotAPI(botToken) if err != nil { log.Fatal(err) @@ -95,7 +98,13 @@ func formatChange(changePercent float64) string { } func sendPriceUpdate() { - now := time.Now().In(singaporeTZ) + var now time.Time + if singaporeTZ != nil { + now = time.Now().In(singaporeTZ) + } else { + now = time.Now().UTC() + log.Println("Warning: singaporeTZ is nil, using UTC") + } message := fmt.Sprintf("市场更新 - %s (SGT)\n\n", now.Format("2006-01-02 15:04:05")) for _, symbol := range symbols { @@ -132,16 +141,12 @@ func sendPriceUpdate() { } func RunBinance() { - log.Println("Sending initial price update...") - sendPriceUpdate() - - ticker := time.NewTicker(1 * time.Hour) - defer ticker.Stop() - + log.Println("Starting Binance service...") for { - select { - case <-ticker.C: - sendPriceUpdate() - } + log.Println("Sending price update...") + sendPriceUpdate() + + log.Println("Waiting for next update...") + time.Sleep(1 * time.Hour) } } diff --git a/service/guard.go b/service/guard.go index 99f769b..a1dffc0 100644 --- a/service/guard.go +++ b/service/guard.go @@ -145,7 +145,7 @@ func StartBot() error { log.Printf("Authorized on account %s", bot.Self.UserName) - err = core.RegisterCommands(bot, adminID) + err = core.RegisterCommands(bot, 0) if err != nil { return fmt.Errorf("error registering commands: %w", err) }