Skip to content

Commit

Permalink
feat: remove hot reload from bot (eatmoreapple#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
eatmoreapple authored Sep 30, 2024
1 parent f3ce16b commit 26b8262
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
22 changes: 0 additions & 22 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 21 additions & 1 deletion bot_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package openwechat

import (
"context"
"encoding/json"
"errors"
)

// LoginCode 定义登录状态码
Expand Down Expand Up @@ -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 热登录模式
Expand Down

0 comments on commit 26b8262

Please sign in to comment.