Skip to content

Commit

Permalink
feat: 指令按照Order排序
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 4, 2024
1 parent f43f7a0 commit 8c02d33
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
16 changes: 16 additions & 0 deletions wclient/robot/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package robot

import (
"regexp"
"sort"
"strings"

"github.com/opentdp/wechat-rest/dbase/profile"
Expand Down Expand Up @@ -43,6 +44,21 @@ func clearHandlers() {

}

func orderHandlers() []string {

keys := make([]string, 0, len(handlers))
for k, _ := range handlers {
keys = append(keys, k)
}

sort.Slice(keys, func(i, j int) bool {
return handlers[keys[i]].Order < handlers[keys[j]].Order
})

return keys

}

func applyHandlers(msg *wcferry.WxMsg) string {

// 前置检查
Expand Down
18 changes: 6 additions & 12 deletions wclient/robot/handler_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package robot

import (
"fmt"
"sort"
"strings"

"github.com/opentdp/wechat-rest/dbase/chatroom"
Expand Down Expand Up @@ -33,25 +32,20 @@ func helpCallback(msg *wcferry.WxMsg) string {

// 生成指令菜单
helper := []string{}
for k, v := range handlers {
for _, k := range orderHandlers() {
v := handlers[k]
if v.Level > 0 {
if up == nil || v.Level > up.Level {
continue // 没有权限
}
}
if msg.IsGroup {
if v.RoomAble { // 群聊指令
helper = append(helper, k+" "+v.Describe)
}
} else {
if v.ChatAble { // 私聊指令
helper = append(helper, k+" "+v.Describe)
}
if (msg.IsGroup && v.RoomAble) || (!msg.IsGroup && v.ChatAble) {
o := fmt.Sprintf("%s %s", k, v.Describe)
helper = append(helper, o)
}
}

// 排序后转为字符串
sort.Strings(helper)
// 数组转为字符串
text := strings.Join(helper, "\n") + "\n"
if up.Level > 0 {
text += fmt.Sprintf("级别 %d;", up.Level)
Expand Down

0 comments on commit 8c02d33

Please sign in to comment.