Skip to content

Commit a1621f3

Browse files
authored
optimize(antiabuse): 添加违禁词解释 (FloatTech#1183)
1 parent 21aa3bc commit a1621f3

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,18 @@ zerobot [-h] [-m] [-n nickname] [-t token] [-u url] [-g url] [-p prefix] [-d|w]
192192

193193
- [x] 早安 | 晚安
194194

195+
</details>
196+
<details>
197+
<summary>违禁词检测</summary>
198+
199+
`import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/antiabuse"
200+
`
201+
- [x] 添加违禁词
202+
203+
- [x] 删除违禁词
204+
205+
- [x] 查看违禁词
206+
195207
</details>
196208
<details>
197209
<summary>ATRI</summary>

plugin/antiabuse/anti.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import (
1717
"github.com/wdvxdr1123/ZeroBot/message"
1818
)
1919

20-
const bandur time.Duration = time.Minute * 10
20+
const (
21+
bandur time.Duration = time.Minute * 2
22+
add = "添加违禁词"
23+
del = "删除违禁词"
24+
list = "查看违禁词"
25+
)
2126

2227
var (
2328
managers *ctrl.Manager[*zero.Ctx] // managers lazy load
@@ -41,7 +46,7 @@ func init() {
4146
engine := control.AutoRegister(&ctrl.Options[*zero.Ctx]{
4247
DisableOnDefault: false,
4348
Brief: "违禁词检测",
44-
Help: "- /[添加|删除|查看]违禁词",
49+
Help: "- [添加|删除|查看]违禁词",
4550
PrivateDataFolder: "anti_abuse",
4651
})
4752

@@ -56,10 +61,14 @@ func init() {
5661
return true
5762
})
5863

59-
engine.OnMessage(onceRule, zero.OnlyGroup, func(ctx *zero.Ctx) bool {
60-
if !ctx.Event.IsToMe {
61-
return true
64+
notAntiabuse := func(ctx *zero.Ctx) bool {
65+
if zero.PrefixRule(add)(ctx) || zero.PrefixRule(del)(ctx) || zero.PrefixRule(list)(ctx) {
66+
return false
6267
}
68+
return true
69+
}
70+
71+
engine.OnMessage(onceRule, notAntiabuse, zero.OnlyGroup, func(ctx *zero.Ctx) bool {
6372
uid := ctx.Event.UserID
6473
gid := ctx.Event.GroupID
6574
msg := strings.ReplaceAll(ctx.MessageString(), "\n", "")
@@ -70,7 +79,8 @@ func init() {
7079
if err := ctx.State["manager"].(*ctrl.Control[*zero.Ctx]).Manager.DoBlock(uid); err == nil {
7180
t := time.Now().Unix()
7281
cache.Set(uid, struct{}{})
73-
ctx.SetThisGroupBan(uid, int64(bandur.Minutes()))
82+
ctx.SetThisGroupBan(uid, int64(bandur.Seconds()))
83+
ctx.DeleteMessage(ctx.Event.MessageID)
7484
ctx.SendChain(message.Text("检测到违禁词, 已封禁/屏蔽", bandur))
7585
db.Lock()
7686
defer db.Unlock()
@@ -92,27 +102,27 @@ func init() {
92102
return true
93103
})
94104

95-
engine.OnCommand("添加违禁词", zero.OnlyGroup, zero.AdminPermission, onceRule).Handle(
105+
engine.OnPrefix(add, zero.OnlyGroup, zero.AdminPermission, onceRule).SetBlock(true).Handle(
96106
func(ctx *zero.Ctx) {
97-
args := ctx.State["args"].(string)
107+
args := strings.TrimSpace(ctx.State["args"].(string))
98108
if err := db.insertWord(ctx.Event.GroupID, args); err != nil {
99109
ctx.SendChain(message.Text("ERROR: ", err))
100110
} else {
101111
ctx.SendChain(message.Text("成功"))
102112
}
103113
})
104114

105-
engine.OnCommand("删除违禁词", zero.OnlyGroup, zero.AdminPermission, onceRule).Handle(
115+
engine.OnPrefix(del, zero.OnlyGroup, zero.AdminPermission, onceRule).SetBlock(true).Handle(
106116
func(ctx *zero.Ctx) {
107-
args := ctx.State["args"].(string)
117+
args := strings.TrimSpace(ctx.State["args"].(string))
108118
if err := db.deleteWord(ctx.Event.GroupID, args); err != nil {
109119
ctx.SendChain(message.Text("ERROR: ", err))
110120
} else {
111121
ctx.SendChain(message.Text("成功"))
112122
}
113123
})
114124

115-
engine.OnCommand("查看违禁词", zero.OnlyGroup, onceRule).Handle(
125+
engine.OnPrefix(list, zero.OnlyGroup, onceRule).SetBlock(true).Handle(
116126
func(ctx *zero.Ctx) {
117127
b, err := text.RenderToBase64(db.listWords(ctx.Event.GroupID), text.FontFile, 400, 20)
118128
if err != nil {

0 commit comments

Comments
 (0)