Skip to content

Commit

Permalink
refactor: 拆分receiver处理函数
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Mar 8, 2024
1 parent 798d708 commit 6ece74c
Show file tree
Hide file tree
Showing 11 changed files with 395 additions and 343 deletions.
8 changes: 4 additions & 4 deletions wclient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@
switch msg.Type {
case 0: //朋友圈消息
case 1: //文字
hook1(msg)
receiver1(msg)
case 3: //图片
case 34: //语音
case 37: //好友确认
hook37(msg)
receiver37(msg)
case 40: //POSSIBLEFRIEND_MSG
case 42: //名片
case 43: //视频
Expand All @@ -107,9 +107,9 @@ switch msg.Type {
case 66: //微信红包
case 9999: //SYSNOTICE
case 10000: //红包、系统消息
hook10000(msg)
receiver10000(msg)
case 10002: //撤回消息
hook10002(msg)
receiver10002(msg)
case 1048625: //搜狗表情
case 16777265: //链接
case 436207665: //微信红包
Expand Down
41 changes: 41 additions & 0 deletions wclient/robot/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package robot

import (
"github.com/opentdp/go-helper/logman"
"github.com/opentdp/wechat-rest/dbase/setting"
"github.com/opentdp/wechat-rest/wcferry"
"github.com/opentdp/wechat-rest/wclient"
)

var wc *wcferry.Client

var clientId string
var selfInfo *wcferry.UserInfo

func Start() {

setting.Laod()

if !setting.BotEnable {
logman.Warn("robot disabled")
return
}

if clientId != "" {
logman.Warn("robot already started")
return
}

initHandlers()

wc = wclient.Register()
clientId, _ = wc.EnrollReceiver(true, receiver)

}

func Redo() {

selfInfo = nil
initHandlers()

}
39 changes: 3 additions & 36 deletions wclient/robot/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"sort"
"strings"

"github.com/opentdp/wechat-rest/dbase/chatroom"
"github.com/opentdp/wechat-rest/dbase/profile"
"github.com/opentdp/wechat-rest/dbase/setting"
"github.com/opentdp/wechat-rest/wcferry"
Expand All @@ -16,8 +15,9 @@ type Handler struct {
ChatAble bool // 是否允许在私聊使用
RoomAble bool // 是否允许在群聊使用
Command string // 指令
Alias string // 指令别名
Describe string // 指令的描述信息
PreCheck func(*wcferry.WxMsg) string // 前置检查,可拦截所有聊天内容
PreCheck func(*wcferry.WxMsg) string // 前置检查,可拦截文本聊天内容
Callback func(*wcferry.WxMsg) string // 指令回调,返回回复内容
}

Expand Down Expand Up @@ -47,18 +47,12 @@ func initHandlers() {
})

// 更新列表
Handlers = list
HandlerMap = lmap
Handlers, HandlerMap = list, lmap

}

func applyHandlers(msg *wcferry.WxMsg) string {

// 白名单
if txt := whiteLimit(msg); txt != "" {
return txt
}

// 前置钩子
for _, v := range Handlers {
if v.PreCheck != nil {
Expand Down Expand Up @@ -114,30 +108,3 @@ func applyHandlers(msg *wcferry.WxMsg) string {
return handler.Callback(msg)

}

// 白名单模式
func whiteLimit(msg *wcferry.WxMsg) string {

if !setting.WhiteLimit {
return ""
}

// 管理豁免
up, _ := profile.Fetch(&profile.FetchParam{Wxid: msg.Sender, Roomid: prid(msg)})
if up.Level >= 7 {
return ""
}

// 验证权限
if msg.IsGroup {
room, _ := chatroom.Fetch(&chatroom.FetchParam{Roomid: msg.Roomid})
if room.Level < 2 {
return "-"
}
} else if up.Level < 2 {
return "-"
}

return ""

}
Loading

0 comments on commit 6ece74c

Please sign in to comment.