Skip to content

Commit

Permalink
perf: 优化一下菜单模式2的战士
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Mar 1, 2023
1 parent 9d99f1f commit df8e1d6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
31 changes: 20 additions & 11 deletions engine/control/api.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package control

import "github.com/yqchilde/wxbot/engine/robot"

type controlApi struct{}

// GetMenus 获取菜单
func (c *controlApi) GetMenus(wxId string) (menus []map[string]interface{}) {
for _, v := range managers.LookupAll() {
if v.Options.HideMenu {
continue
// 检查wxId是否存在
users := robot.GetBot().Users()
for _, v := range users {
if v.WxId == wxId {
for _, v := range managers.LookupAll() {
if v.Options.HideMenu {
continue
}
menus = append(menus, map[string]interface{}{
"name": v.Service,
"alias": v.Options.Alias,
"priority": v.Options.Priority,
"describe": v.Options.Help,
"defStatus": !v.Options.DisableOnDefault,
"curStatus": v.IsEnabledIn(wxId),
})
}
break
}
menus = append(menus, map[string]interface{}{
"name": v.Service,
"alias": v.Options.Alias,
"priority": v.Options.Priority,
"describe": v.Options.Help,
"defStatus": !v.Options.DisableOnDefault,
"curStatus": v.IsEnabledIn(wxId),
})
}
return
}
9 changes: 9 additions & 0 deletions engine/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ func (b *Bot) MPs() MPs {
return b.self.mps
}

// Users 从缓存中获取所有用户列表
func (b *Bot) Users() []*User {
var users []*User
users = append(users, b.self.friends.AsUsers()...)
users = append(users, b.self.groups.AsUsers()...)
users = append(users, b.self.mps.AsUsers()...)
return users
}

// GetSelf 获取Self对象,Self对象包含了对用户、群、公众号的包装
func (b *Bot) GetSelf() (*Self, error) {
if b.self == nil {
Expand Down
2 changes: 1 addition & 1 deletion web
Submodule web updated from 888d40 to 9d942a

0 comments on commit df8e1d6

Please sign in to comment.