Skip to content

Commit

Permalink
feat: 支持黑白名单
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Jan 10, 2024
1 parent f4a0393 commit 3dd4437
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
9 changes: 6 additions & 3 deletions args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ type BotRoom struct {
}

var Bot = struct {
Enable bool `yaml:"enable"`
Welcome string `yaml:"welcome"`
InvitableRooms []*BotRoom `yaml:"invitableRooms"`
Enable bool `yaml:"enable"`
Welcome string `yaml:"welcome"`
Managers []string `yaml:"managers"`
BlackList []string `yaml:"blackList"`
WhiteList []string `yaml:"whiteList"`
HostedRooms []*BotRoom `yaml:"hostedRooms"`
}{
Enable: true,
}
Expand Down
5 changes: 4 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
bot:
enable: true # 是否启用内置机器人
welcome: 回复 /help 查询指令 # 添加好友或加群后自动回复
invitableRooms: # 可邀请的群组列表
managers: [] # 管理员人员
blackList: [] # 黑名单,禁止使用机器人
whiteList: [] # 白名单,禁止其他人使用机器人
hostedRooms: # 托管的群聊列表
- mask: 1 # 群标记
name: OpenTDP 开发群 # 群聊名称
roomId: 38699745819@chatroom # 群聊 Id
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions wclient/proto/xml_atuser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package proto

type AtMsgSource struct {
AtUserList string `xml:"atuserlist"`
Silence int32 `xml:"silence"`
MemberCount int32 `xml:"membercount"`
Signature string `xml:"signature"`
TmpNode TmpNode `xml:"tmp_node"`
}

type TmpNode struct {
PublisherID string `xml:",chardata"`
}
44 changes: 42 additions & 2 deletions wclient/robot/handle.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package robot

import (
"encoding/xml"
"fmt"
"math/rand"
"regexp"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/opentdp/wechat-rest/args"
"github.com/opentdp/wechat-rest/wcferry"
"github.com/opentdp/wechat-rest/wclient/model"
"github.com/opentdp/wechat-rest/wclient/proto"
)

type Handler struct {
Expand Down Expand Up @@ -47,6 +49,27 @@ func initHandlers() {
},
}

handlers["/ban"] = &Handler{
Level: 1,
ChatAble: true,
RoomAble: true,
Describe: "禁止用户使用Ai服务",
Callback: func(msg *wcferry.WxMsg) string {
ret := &proto.AtMsgSource{}
err := xml.Unmarshal([]byte(msg.Xml), ret)
if err == nil && ret.AtUserList != "" {
users := strings.Split(ret.AtUserList, ",")
for _, v := range users {
if v != "" && !contains(args.Bot.BlackList, v) {
args.Bot.BlackList = append(args.Bot.BlackList, v)
}
}
return fmt.Sprintf("操作完成,已禁止用户数:%d", len(args.Bot.BlackList))
}
return "参数错误"
},
}

handlers["/mr"] = &Handler{
Level: 0,
ChatAble: true,
Expand All @@ -72,7 +95,7 @@ func initHandlers() {
}
}

for _, v := range args.Bot.InvitableRooms {
for _, v := range args.Bot.HostedRooms {
v := v // copy it
cmdkey := "/room:" + v.Mask
handlers[cmdkey] = &Handler{
Expand Down Expand Up @@ -127,6 +150,14 @@ func initHandlers() {

func applyHandler(msg *wcferry.WxMsg) string {

// 匹配名单
if len(args.Bot.WhiteList) > 0 && !contains(args.Bot.WhiteList, msg.Sender) {
return ""
}
if len(args.Bot.BlackList) > 0 && contains(args.Bot.BlackList, msg.Sender) {
return ""
}

// 解析指令
re := regexp.MustCompile(`^(/[\w:-]{2,20})(\s|$)`)
matches := re.FindStringSubmatch(msg.Content)
Expand All @@ -148,7 +179,7 @@ func applyHandler(msg *wcferry.WxMsg) string {
}

// 检查权限
if hd.Level > 0 {
if hd.Level > 0 && !contains(args.Bot.Managers, msg.Sender) {
return "无权限使用该指令"
}

Expand All @@ -167,3 +198,12 @@ func applyHandler(msg *wcferry.WxMsg) string {
return hd.Callback(msg)

}

func contains(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}

0 comments on commit 3dd4437

Please sign in to comment.