Skip to content

Commit

Permalink
【bug-fix】修改不在通讯录无法获取群组的bug 解决方案:程序运行后,如果群组(不在通讯录中),发送过任意消息,则更新群组 (#242)
Browse files Browse the repository at this point in the history
Co-authored-by: wenyoufu <[email protected]>
  • Loading branch information
wenyoufu and wenyoufu authored Feb 6, 2023
1 parent 24fe67a commit d1fee1a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,27 @@ func (b *Bot) WebInit() error {
return nil
}

func (b *Bot) updateGroups(msg *Message) {
if msg.IsSendByGroup() {
if msg.FromUserName == msg.bot.self.User.UserName {
return
}
// 首先尝试从缓存里面查找, 如果没有找到则从服务器获取
members, err := msg.bot.self.Members()
if err != nil {
return
}
user, exist := members.GetByUserName(msg.FromUserName)
if !exist {
// 找不到, 从服务器获取
user = &User{self: msg.bot.self, UserName: msg.FromUserName}
err = user.Detail()
b.self.members = b.self.members.Apppend(user)
b.self.groups = b.self.members.Groups()
}
}
}

// 轮询请求
// 根据状态码判断是否有新的请求
func (b *Bot) syncCheck() error {
Expand Down Expand Up @@ -241,6 +262,7 @@ func (b *Bot) syncCheck() error {
// 如果异步调用则需自行处理
// 如配合 openwechat.MessageMatchDispatcher 使用
// NOTE: 请确保 MessageHandler 不会阻塞,否则会导致收不到后续的消息
b.updateGroups(message)
b.MessageHandler(message)
}
}
Expand Down
5 changes: 5 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ func (s *Self) updateMembers() error {
return nil
}

// append
func (m Members) Apppend(user *User) (results Members) {
return append(m, []*User{user}...)
}

// FileHelper 获取文件传输助手对象,封装成Friend返回
//
// fh := self.FileHelper() // or fh := openwechat.NewFriendHelper(self)
Expand Down

0 comments on commit d1fee1a

Please sign in to comment.