From 7294127c7204c393bb362a33e190a14e990b89a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E6=B5=B7?= Date: Sat, 16 Mar 2024 13:29:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9F=BA=E4=BA=8Edfa=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E8=BF=9D=E7=A6=81=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ wclient/robot/handler_bad.go | 67 +++++++++++++++++++++--------------- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index 8c10914..0c30503 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/clbanning/mxj v1.8.4 github.com/gin-gonic/gin v1.9.1 github.com/gorilla/websocket v1.5.1 + github.com/importcjj/sensitive v0.0.0-20200106142752-42d1c505be7b github.com/mitchellh/mapstructure v1.5.0 github.com/opentdp/go-helper v0.7.1-0.20240314081626-c3e3805521f2 github.com/rehiy/one-llm v0.1.0 diff --git a/go.sum b/go.sum index ec37f28..961002a 100644 --- a/go.sum +++ b/go.sum @@ -59,6 +59,8 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/importcjj/sensitive v0.0.0-20200106142752-42d1c505be7b h1:9hudrgWUhyfR4FRMOfL9KB1uYw48DUdHkkgr9ODOw7Y= +github.com/importcjj/sensitive v0.0.0-20200106142752-42d1c505be7b/go.mod h1:zLVdX6Ed2SvCbEamKmve16U0E03UkdJo4ls1TBfmc8Q= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= diff --git a/wclient/robot/handler_bad.go b/wclient/robot/handler_bad.go index a88f9a8..a238f38 100644 --- a/wclient/robot/handler_bad.go +++ b/wclient/robot/handler_bad.go @@ -2,22 +2,22 @@ package robot import ( "fmt" - "strings" + + "github.com/importcjj/sensitive" "github.com/opentdp/wechat-rest/dbase/keyword" "github.com/opentdp/wechat-rest/dbase/profile" - "github.com/opentdp/wechat-rest/dbase/tables" "github.com/opentdp/wechat-rest/wcferry" ) var badMember = map[string]int{} -var badwordList = []*tables.Keyword{} +var badFilter *sensitive.Filter var roomMemberAlias = map[string]string{} func badHandler() []*Handler { - updateBadWord() + updateBadWordFilter() cmds := []*Handler{} @@ -32,7 +32,7 @@ func badHandler() []*Handler { Group: "badword", Roomid: prid(msg), Phrase: msg.Content, Level: 1, }) if err == nil { - updateBadWord() + badFilter.AddWord(msg.Content) return "违禁词添加成功" } return "违禁词已存在" @@ -51,7 +51,7 @@ func badHandler() []*Handler { Group: "badword", Roomid: prid(msg), Phrase: msg.Content, }) if err == nil { - updateBadWord() + badFilter.DelWord(msg.Content) return "违禁词删除成功" } return "违禁词删除失败" @@ -75,28 +75,33 @@ func badPreCheck(msg *wcferry.WxMsg) string { return "" } - // 遍历关键词 + // 查找违禁词 text := roomMemberName(msg.Sender, msg.Roomid) + msg.Content - for _, v := range badwordList { - if msg.IsGroup { - if v.Roomid != "*" && v.Roomid != "+" && v.Roomid != msg.Roomid { - continue // 忽略 - } - } else { - if v.Roomid != "*" && v.Roomid != "-" { - continue // 忽略 - } + keys := badFilter.FindAll(text) + if len(keys) == 0 { + return "" + } + + // 判断违禁级别 + level := 0 + for _, k := range keys { + v, _ := keyword.Fetch(&keyword.FetchParam{Group: "badword", Phrase: k}) + if v.Level > 0 && (v.Roomid == msg.Roomid || v.Roomid == "*" || v.Roomid == "+") { + level += int(v.Level) } - if v.Level > 0 && strings.Contains(text, v.Phrase) { - badMember[msg.Sender] += int(v.Level) - if badMember[msg.Sender] > 10 { - defer delete(badMember, msg.Sender) - defer wc.CmdClient.DelChatRoomMembers(msg.Roomid, msg.Sender) - return "我送你离开,天涯之外你是否还在" - } - str := "违规风险 +%d,当前累计:%d,大于 10 将被请出群聊" - return fmt.Sprintf(str, v.Level, badMember[msg.Sender]) + } + + // 等级违规积分 + if level > 0 { + badMember[msg.Sender] += level + if badMember[msg.Sender] > 10 { + defer delete(badMember, msg.Sender) + defer wc.CmdClient.DelChatRoomMembers(msg.Roomid, msg.Sender) + str := "违规累计 %d;送你离开,天涯之外你是否还在" + return fmt.Sprintf(str, badMember[msg.Sender]) } + str := "违规风险 +%d,当前累计:%d,大于 10 将被请出群聊" + return fmt.Sprintf(str, level, badMember[msg.Sender]) } return "" @@ -110,14 +115,22 @@ func roomMemberName(wxid, roomid string) string { if roomMemberAlias[k] == "" { roomMemberAlias[k] = wc.CmdClient.GetAliasInChatRoom(wxid, roomid) } + return roomMemberAlias[k] } -func updateBadWord() { +func updateBadWordFilter() { - badwordList, _ = keyword.FetchAll(&keyword.FetchAllParam{ + items, _ := keyword.FetchAll(&keyword.FetchAllParam{ Group: "badword", }) + filter := sensitive.New() + for _, v := range items { + filter.AddWord(v.Phrase) + } + + badFilter = filter + }