Skip to content

Commit 14096f2

Browse files
committed
✨ add 设置默认限速 #241
1 parent c0b75ac commit 14096f2

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,7 @@ print("run[CQ:image,file="+j["img"]+"]")
340340

341341
- [x] 查询计算机当前活跃度: [检查身体 | 自检 | 启动自检 | 系统状态]
342342

343-
- [x] 清理缓存 (仅适用于 gocq 且需要 bot 的运行目录和 gocq 相同)
344-
345-
- [ ] 简易语音
346-
347-
- [ ] 爬图合成 [@xxx]
343+
- [x] 设置默认限速为每 m [分钟 | 秒] n 次触发
348344

349345
</details>
350346
<details>

plugin/ai_false/ai_false.go

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ package aifalse
44
import (
55
"fmt"
66
"math"
7-
"os"
7+
"strconv"
88
"time"
99

1010
control "github.com/FloatTech/zbputils/control"
11+
"github.com/FloatTech/zbputils/ctxext"
1112
"github.com/shirou/gopsutil/v3/cpu"
1213
"github.com/shirou/gopsutil/v3/disk"
1314
"github.com/shirou/gopsutil/v3/mem"
15+
"github.com/sirupsen/logrus"
1416

1517
zero "github.com/wdvxdr1123/ZeroBot"
1618
"github.com/wdvxdr1123/ZeroBot/message"
@@ -20,8 +22,20 @@ func init() { // 插件主体
2022
engine := control.Register("aifalse", &control.Options{
2123
DisableOnDefault: false,
2224
Help: "AIfalse\n" +
23-
"- 查询计算机当前活跃度: [检查身体 | 自检 | 启动自检 | 系统状态]",
25+
"- 查询计算机当前活跃度: [检查身体 | 自检 | 启动自检 | 系统状态]\n" +
26+
"- 设置默认限速为每 m [分钟 | 秒] n 次触发",
2427
})
28+
c, ok := control.Lookup("aifalse")
29+
if !ok {
30+
panic("register aifalse error")
31+
}
32+
m := c.GetData(0)
33+
n := (m >> 16) & 0xffff
34+
m &= 0xffff
35+
if m != 0 || n != 0 {
36+
ctxext.SetDefaultLimiterManagerParam(time.Duration(m)*time.Second, int(n))
37+
logrus.Infoln("设置默认限速为每", m, "秒触发", n, "次")
38+
}
2539
engine.OnFullMatchGroup([]string{"检查身体", "自检", "启动自检", "系统状态"}, zero.AdminPermission).SetBlock(true).
2640
Handle(func(ctx *zero.Ctx) {
2741
ctx.SendChain(message.Text(
@@ -31,14 +45,37 @@ func init() { // 插件主体
3145
),
3246
)
3347
})
34-
engine.OnFullMatch("清理缓存", zero.SuperUserPermission).SetBlock(true).
48+
engine.OnRegex(`^设置默认限速为每\s*(\d+)\s*(分钟|秒)\s*(\d+)\s*次触发$`, zero.SuperUserPermission).SetBlock(true).
3549
Handle(func(ctx *zero.Ctx) {
36-
err := os.RemoveAll("data/cache/*")
50+
c, ok := control.Lookup("aifalse")
51+
if !ok {
52+
ctx.SendChain(message.Text("ERROR:no such plugin"))
53+
return
54+
}
55+
m, err := strconv.ParseInt(ctx.State["regex_matched"].([]string)[1], 10, 64)
56+
if err != nil {
57+
ctx.SendChain(message.Text("ERROR:", err))
58+
return
59+
}
60+
if ctx.State["regex_matched"].([]string)[2] == "分钟" {
61+
m *= 60
62+
}
63+
if m >= 65536 || m <= 0 {
64+
ctx.SendChain(message.Text("ERROR:interval too big"))
65+
return
66+
}
67+
n, err := strconv.ParseInt(ctx.State["regex_matched"].([]string)[3], 10, 64)
3768
if err != nil {
38-
ctx.SendChain(message.Text("错误: ", err.Error()))
39-
} else {
40-
ctx.SendChain(message.Text("成功!"))
69+
ctx.SendChain(message.Text("ERROR:", err))
70+
return
71+
}
72+
if n >= 65536 || n <= 0 {
73+
ctx.SendChain(message.Text("ERROR:burst too big"))
74+
return
4175
}
76+
ctxext.SetDefaultLimiterManagerParam(time.Duration(m)*time.Second, int(n))
77+
c.SetData(0, (m&0xffff)|((n<<16)&0xffff0000))
78+
ctx.SendChain(message.Text("设置默认限速为每", m, "秒触发", n, "次"))
4279
})
4380
}
4481

0 commit comments

Comments
 (0)