Skip to content

Commit 6acd930

Browse files
committed
优化代码结构 & fix job regex
1 parent 340db0c commit 6acd930

File tree

7 files changed

+120
-392
lines changed

7 files changed

+120
-392
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ require (
77
github.com/FloatTech/AnimeAPI v1.5.1-0.20220901132657-2585bbc03bf6
88
github.com/FloatTech/floatbox v0.0.0-20220822040527-f059031fec44
99
github.com/FloatTech/sqlite v0.3.3
10+
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b
1011
github.com/FloatTech/zbpctrl v1.4.1-0.20220715042842-93f081cb0133
11-
github.com/FloatTech/zbputils v1.5.1-0.20220826103123-0c73a585e38f
12+
github.com/FloatTech/zbputils v1.5.1-0.20220906100116-30c0892066d6
1213
github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c
1314
github.com/antchfx/htmlquery v1.2.5
1415
github.com/corona10/goimagehash v1.0.3
@@ -36,7 +37,6 @@ require (
3637
)
3738

3839
require (
39-
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b // indirect
4040
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc // indirect
4141
github.com/antchfx/xpath v1.2.1 // indirect
4242
github.com/cheekybits/genny v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b h1:tvciXWq2nuvTbFeJG
2121
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
2222
github.com/FloatTech/zbpctrl v1.4.1-0.20220715042842-93f081cb0133 h1:nP9NI4I+vtwAbiU7wCJwjuzCzMZ/yJYg8h3667HGnv0=
2323
github.com/FloatTech/zbpctrl v1.4.1-0.20220715042842-93f081cb0133/go.mod h1:72BnjyBwQWUC8mqM9dPk5ZrjxXCilQCVp+jfgHATNdw=
24-
github.com/FloatTech/zbputils v1.5.1-0.20220826103123-0c73a585e38f h1:R13GKhRZfB42nsCEqek/ZlGbKBopQOZFYwBU9qSCfhg=
25-
github.com/FloatTech/zbputils v1.5.1-0.20220826103123-0c73a585e38f/go.mod h1:ZT91eCYR6y1HbLRTO5EB7o8K7qEPbCsX7vjhYIuMijc=
24+
github.com/FloatTech/zbputils v1.5.1-0.20220906100116-30c0892066d6 h1:9+PCM0UzbtrpYSzkvy+AeVy6bpprmVd6BUs17Njao04=
25+
github.com/FloatTech/zbputils v1.5.1-0.20220906100116-30c0892066d6/go.mod h1:ZT91eCYR6y1HbLRTO5EB7o8K7qEPbCsX7vjhYIuMijc=
2626
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
2727
github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c h1:cNPOdTNiVwxLpROLjXCgbIPvdkE+BwvxDvgmdYmWx6Q=
2828
github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c/go.mod h1:KqZzu7slNKROh3TSYEH/IUMG6f4M+1qubZ5e52QypsE=

plugin/antiabuse/anti.go

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
package antiabuse
33

44
import (
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
}

plugin/antiabuse/db.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package antiabuse
2+
3+
import (
4+
"errors"
5+
"strconv"
6+
"strings"
7+
"sync"
8+
"time"
9+
"unsafe"
10+
11+
sqlite "github.com/FloatTech/sqlite"
12+
"github.com/FloatTech/ttl"
13+
ctrl "github.com/FloatTech/zbpctrl"
14+
"github.com/sirupsen/logrus"
15+
zero "github.com/wdvxdr1123/ZeroBot"
16+
)
17+
18+
var managers *ctrl.Manager[*zero.Ctx] // managers lazy load
19+
var db = &sqlite.Sqlite{}
20+
var mu sync.RWMutex
21+
22+
func onDel(uid int64, _ struct{}) {
23+
if managers == nil {
24+
return
25+
}
26+
if err := managers.DoUnblock(uid); err != nil {
27+
logrus.Error("do unblock error:", err)
28+
}
29+
}
30+
31+
var cache = ttl.NewCacheOn(4*time.Hour, [4]func(int64, struct{}){nil, nil, onDel, nil})
32+
33+
type banWord struct {
34+
Word string `db:"word"`
35+
}
36+
37+
var nilban = &banWord{}
38+
39+
func insertWord(gid int64, word string) error {
40+
grp := strconv.FormatInt(gid, 36)
41+
mu.Lock()
42+
defer mu.Unlock()
43+
err := db.Create(grp, nilban)
44+
if err != nil {
45+
return err
46+
}
47+
return db.Insert(grp, (*banWord)(unsafe.Pointer(&word)))
48+
}
49+
50+
func deleteWord(gid int64, word string) error {
51+
grp := strconv.FormatInt(gid, 36)
52+
mu.Lock()
53+
defer mu.Unlock()
54+
if n, _ := db.Count(grp); n == 0 {
55+
return errors.New("本群还没有违禁词~")
56+
}
57+
return db.Del(grp, "WHRER word='"+word+"'")
58+
}
59+
60+
func listWords(gid int64) string {
61+
grp := strconv.FormatInt(gid, 36)
62+
word := ""
63+
ptr := (*banWord)(unsafe.Pointer(&word))
64+
sb := strings.Builder{}
65+
mu.Lock()
66+
defer mu.Unlock()
67+
_ = db.FindFor(grp, ptr, "", func() error {
68+
sb.WriteString(word)
69+
sb.WriteString(" |")
70+
return nil
71+
})
72+
if sb.Len() <= 2 {
73+
return ""
74+
}
75+
return sb.String()[:sb.Len()-2]
76+
}

plugin/antiabuse/set.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

plugin/antiabuse/utils.go

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)