From 8a201f1461570e64e6321b2c1807c09d043a320a Mon Sep 17 00:00:00 2001 From: eatmoreapple Date: Fri, 9 Dec 2022 13:33:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Bot=E5=AD=98=E6=B4=BB?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bot.go b/bot.go index b8093c9..a973cc0 100644 --- a/bot.go +++ b/bot.go @@ -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} } @@ -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]) }