From 26b8262dad0683fe9a7b2a57a0ad742422389176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E5=90=83=E7=82=B9=E8=8B=B9=E6=9E=9C?= Date: Mon, 30 Sep 2024 15:38:53 +0800 Subject: [PATCH] feat: remove hot reload from bot (#537) --- bot.go | 22 ---------------------- bot_login.go | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/bot.go b/bot.go index 5d28faa..142706c 100644 --- a/bot.go +++ b/bot.go @@ -336,28 +336,6 @@ func (b *Bot) Context() context.Context { return b.context } -func (b *Bot) reload() error { - if b.hotReloadStorage == nil { - return errors.New("hotReloadStorage is nil") - } - var item HotReloadStorageItem - if err := b.Serializer.Decode(b.hotReloadStorage, &item); err != nil { - return err - } - b.Caller.Client.SetCookieJar(item.Jar) - b.Storage.LoginInfo = item.LoginInfo - b.Storage.Request = item.BaseRequest - b.Caller.Client.Domain = item.WechatDomain - b.uuid = item.UUID - if item.SyncKey != nil { - if b.Storage.Response == nil { - b.Storage.Response = &WebInitResponse{} - } - b.Storage.Response.SyncKey = item.SyncKey - } - return nil -} - // NewBot Bot的构造方法 // 接收外部的 context.Context,用于控制Bot的存活 func NewBot(c context.Context) *Bot { diff --git a/bot_login.go b/bot_login.go index b271785..f49a90e 100644 --- a/bot_login.go +++ b/bot_login.go @@ -2,6 +2,8 @@ package openwechat import ( "context" + "encoding/json" + "errors" ) // LoginCode 定义登录状态码 @@ -186,8 +188,26 @@ func (s *ScanLogin) checkLogin(bot *Bot, uuid string) error { } func botReload(bot *Bot, storage HotReloadStorage) error { + if storage == nil { + return errors.New("storage is nil") + } bot.hotReloadStorage = storage - return bot.reload() + var item HotReloadStorageItem + if err := json.NewDecoder(storage).Decode(&item); err != nil { + return err + } + bot.Caller.Client.SetCookieJar(item.Jar) + bot.Storage.LoginInfo = item.LoginInfo + bot.Storage.Request = item.BaseRequest + bot.Caller.Client.Domain = item.WechatDomain + bot.uuid = item.UUID + if item.SyncKey != nil { + if bot.Storage.Response == nil { + bot.Storage.Response = &WebInitResponse{} + } + bot.Storage.Response.SyncKey = item.SyncKey + } + return nil } // HotLogin 热登录模式