Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove hot reload from bot #537

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading