Skip to content

Commit

Permalink
添加Bot存活控制
Browse files Browse the repository at this point in the history
  • Loading branch information
eatmoreapple committed Dec 9, 2022
1 parent 8ba4460 commit 8a201f1
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,12 @@ func (b *Bot) OnLogout(f func(bot *Bot)) {
}

// NewBot Bot的构造方法
func NewBot() *Bot {
// 接收外部的 context.Context,用于控制Bot的存活
func NewBot(c context.Context) *Bot {
caller := DefaultCaller()
// 默认行为为桌面模式
caller.Client.SetMode(Normal)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(c)
return &Bot{Caller: caller, Storage: &Storage{}, context: ctx, cancel: cancel}
}

Expand All @@ -399,7 +400,7 @@ func NewBot() *Bot {
//
// bot := openwechat.DefaultBot(openwechat.Desktop)
func DefaultBot(modes ...Mode) *Bot {
bot := NewBot()
bot := NewBot(context.Background())
if len(modes) > 0 {
bot.Caller.Client.SetMode(modes[0])
}
Expand Down

0 comments on commit 8a201f1

Please sign in to comment.