mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 02:00:24 +00:00
优化代码结构 & fix job regex
This commit is contained in:
@@ -2,13 +2,15 @@
|
||||
package antiabuse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/FloatTech/floatbox/binary"
|
||||
fcext "github.com/FloatTech/floatbox/ctxext"
|
||||
ctrl "github.com/FloatTech/zbpctrl"
|
||||
"github.com/FloatTech/zbputils/control"
|
||||
"github.com/FloatTech/zbputils/img/text"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
)
|
||||
@@ -19,6 +21,7 @@ func init() {
|
||||
Help: "违禁词检测",
|
||||
PrivateDataFolder: "anti_abuse",
|
||||
})
|
||||
|
||||
onceRule := fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool {
|
||||
managers = ctx.State["managers"].(*ctrl.Control[*zero.Ctx]).Manager
|
||||
db.DBPath = engine.DataFolder() + "anti_abuse.db"
|
||||
@@ -32,39 +35,62 @@ func init() {
|
||||
ctx.SendChain(message.Text("create table error: ", err))
|
||||
return false
|
||||
}
|
||||
err = recoverWord()
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("recover data error: ", err))
|
||||
return true
|
||||
})
|
||||
|
||||
engine.OnMessage(onceRule, zero.OnlyGroup, func(ctx *zero.Ctx) bool {
|
||||
if !ctx.Event.IsToMe {
|
||||
return true
|
||||
}
|
||||
uid := ctx.Event.UserID
|
||||
gid := ctx.Event.GroupID
|
||||
grp := strconv.FormatInt(gid, 36)
|
||||
msg := strings.ReplaceAll(ctx.MessageString(), "\n", "")
|
||||
msg = strings.ReplaceAll(msg, "\r", "")
|
||||
msg = strings.ReplaceAll(msg, "\t", "")
|
||||
msg = strings.ReplaceAll(msg, ";", "")
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
if db.CanFind(grp, "WHERE instr('"+msg+"', word)>=0") {
|
||||
if err := managers.DoBlock(uid); err == nil {
|
||||
cache.Set(uid, struct{}{})
|
||||
ctx.SetGroupBan(gid, uid, 4*3600)
|
||||
ctx.SendChain(message.Text("检测到违禁词,已封禁/屏蔽4小时"))
|
||||
} else {
|
||||
ctx.SendChain(message.Text("block user error:", err))
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
engine.OnMessage(onceRule, zero.OnlyGroup, banRule)
|
||||
|
||||
engine.OnCommand("添加违禁词", zero.OnlyGroup, zero.AdminPermission, onceRule).Handle(
|
||||
func(ctx *zero.Ctx) {
|
||||
args := ctx.State["args"].(string)
|
||||
if err := insertWord(ctx.Event.GroupID, args); err != nil {
|
||||
ctx.SendChain(message.Text("error:", err))
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
} else {
|
||||
ctx.SendChain(message.Text(fmt.Sprintf("添加违禁词 %s 成功", args)))
|
||||
ctx.SendChain(message.Text("添加违禁词 ", args, " 成功"))
|
||||
}
|
||||
})
|
||||
|
||||
engine.OnCommand("删除违禁词", zero.OnlyGroup, zero.AdminPermission, onceRule).Handle(
|
||||
func(ctx *zero.Ctx) {
|
||||
args := ctx.State["args"].(string)
|
||||
if err := deleteWord(ctx.Event.GroupID, args); err != nil {
|
||||
ctx.SendChain(message.Text("error:", err))
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
} else {
|
||||
ctx.SendChain(message.Text(fmt.Sprintf("删除违禁词 %s 成功", args)))
|
||||
}
|
||||
})
|
||||
engine.OnCommand("查看违禁词", zero.OnlyGroup, onceRule).Handle(
|
||||
func(ctx *zero.Ctx) {
|
||||
if set, ok := wordMap[ctx.Event.GroupID]; !ok {
|
||||
ctx.SendChain(message.Text("本群无违禁词"))
|
||||
} else {
|
||||
ctx.SendChain(message.Text("本群违禁词有:", strings.Join(set.ToSlice(), " |")))
|
||||
ctx.SendChain(message.Text("删除违禁词 ", args, " 成功"))
|
||||
}
|
||||
})
|
||||
|
||||
engine.OnCommand("查看违禁词", zero.OnlyGroup, onceRule).Handle(
|
||||
func(ctx *zero.Ctx) {
|
||||
b, err := text.RenderToBase64(listWords(ctx.Event.GroupID), text.FontFile, 400, 20)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
ctx.SendChain(message.Text("本群违禁词有:\n"), message.Image("base64://"+binary.BytesToString(b)))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user