22package antiabuse
33
44import (
5- "fmt "
5+ "strconv "
66 "strings"
77 "time"
88
9+ "github.com/FloatTech/floatbox/binary"
910 fcext "github.com/FloatTech/floatbox/ctxext"
1011 ctrl "github.com/FloatTech/zbpctrl"
1112 "github.com/FloatTech/zbputils/control"
13+ "github.com/FloatTech/zbputils/img/text"
1214 zero "github.com/wdvxdr1123/ZeroBot"
1315 "github.com/wdvxdr1123/ZeroBot/message"
1416)
@@ -19,6 +21,7 @@ func init() {
1921 Help : "违禁词检测" ,
2022 PrivateDataFolder : "anti_abuse" ,
2123 })
24+
2225 onceRule := fcext .DoOnceOnSuccess (func (ctx * zero.Ctx ) bool {
2326 managers = ctx .State ["managers" ].(* ctrl.Control [* zero.Ctx ]).Manager
2427 db .DBPath = engine .DataFolder () + "anti_abuse.db"
@@ -32,39 +35,62 @@ func init() {
3235 ctx .SendChain (message .Text ("create table error: " , err ))
3336 return false
3437 }
35- err = recoverWord ()
36- if err != nil {
37- ctx .SendChain (message .Text ("recover data error: " , err ))
38+ return true
39+ })
40+
41+ engine .OnMessage (onceRule , zero .OnlyGroup , func (ctx * zero.Ctx ) bool {
42+ if ! ctx .Event .IsToMe {
43+ return true
44+ }
45+ uid := ctx .Event .UserID
46+ gid := ctx .Event .GroupID
47+ grp := strconv .FormatInt (gid , 36 )
48+ msg := strings .ReplaceAll (ctx .MessageString (), "\n " , "" )
49+ msg = strings .ReplaceAll (msg , "\r " , "" )
50+ msg = strings .ReplaceAll (msg , "\t " , "" )
51+ msg = strings .ReplaceAll (msg , ";" , "" )
52+ mu .RLock ()
53+ defer mu .RUnlock ()
54+ if db .CanFind (grp , "WHERE instr('" + msg + "', word)>=0" ) {
55+ if err := managers .DoBlock (uid ); err == nil {
56+ cache .Set (uid , struct {}{})
57+ ctx .SetGroupBan (gid , uid , 4 * 3600 )
58+ ctx .SendChain (message .Text ("检测到违禁词,已封禁/屏蔽4小时" ))
59+ } else {
60+ ctx .SendChain (message .Text ("block user error:" , err ))
61+ }
3862 return false
3963 }
4064 return true
4165 })
42- engine . OnMessage ( onceRule , zero . OnlyGroup , banRule )
66+
4367 engine .OnCommand ("添加违禁词" , zero .OnlyGroup , zero .AdminPermission , onceRule ).Handle (
4468 func (ctx * zero.Ctx ) {
4569 args := ctx .State ["args" ].(string )
4670 if err := insertWord (ctx .Event .GroupID , args ); err != nil {
47- ctx .SendChain (message .Text ("error: " , err ))
71+ ctx .SendChain (message .Text ("ERROR: " , err ))
4872 } else {
49- ctx .SendChain (message .Text (fmt . Sprintf ( "添加违禁词 %s 成功 " , args ) ))
73+ ctx .SendChain (message .Text ("添加违禁词 " , args , " 成功" ))
5074 }
5175 })
76+
5277 engine .OnCommand ("删除违禁词" , zero .OnlyGroup , zero .AdminPermission , onceRule ).Handle (
5378 func (ctx * zero.Ctx ) {
5479 args := ctx .State ["args" ].(string )
5580 if err := deleteWord (ctx .Event .GroupID , args ); err != nil {
56- ctx .SendChain (message .Text ("error: " , err ))
81+ ctx .SendChain (message .Text ("ERROR: " , err ))
5782 } else {
58- ctx .SendChain (message .Text (fmt . Sprintf ( "删除违禁词 %s 成功 " , args ) ))
83+ ctx .SendChain (message .Text ("删除违禁词 " , args , " 成功" ))
5984 }
6085 })
86+
6187 engine .OnCommand ("查看违禁词" , zero .OnlyGroup , onceRule ).Handle (
6288 func (ctx * zero.Ctx ) {
63- if set , ok := wordMap [ ctx .Event .GroupID ]; ! ok {
64- ctx . SendChain ( message . Text ( "本群无违禁词" ))
65- } else {
66- ctx . SendChain ( message . Text ( "本群违禁词有:" , strings . Join ( set . ToSlice (), " |" )))
89+ b , err := text . RenderToBase64 ( listWords ( ctx .Event .GroupID ), text . FontFile , 400 , 20 )
90+ if err != nil {
91+ ctx . SendChain ( message . Text ( "ERROR: " , err ))
92+ return
6793 }
94+ ctx .SendChain (message .Text ("本群违禁词有:\n " ), message .Image ("base64://" + binary .BytesToString (b )))
6895 })
69-
7096}
0 commit comments