mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 13:59:39 +08:00
✨ add 设置默认限速 #241
This commit is contained in:
parent
c0b75accf4
commit
14096f24ba
@ -340,11 +340,7 @@ print("run[CQ:image,file="+j["img"]+"]")
|
|||||||
|
|
||||||
- [x] 查询计算机当前活跃度: [检查身体 | 自检 | 启动自检 | 系统状态]
|
- [x] 查询计算机当前活跃度: [检查身体 | 自检 | 启动自检 | 系统状态]
|
||||||
|
|
||||||
- [x] 清理缓存 (仅适用于 gocq 且需要 bot 的运行目录和 gocq 相同)
|
- [x] 设置默认限速为每 m [分钟 | 秒] n 次触发
|
||||||
|
|
||||||
- [ ] 简易语音
|
|
||||||
|
|
||||||
- [ ] 爬图合成 [@xxx]
|
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
<details>
|
<details>
|
||||||
|
|||||||
@ -4,13 +4,15 @@ package aifalse
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
control "github.com/FloatTech/zbputils/control"
|
control "github.com/FloatTech/zbputils/control"
|
||||||
|
"github.com/FloatTech/zbputils/ctxext"
|
||||||
"github.com/shirou/gopsutil/v3/cpu"
|
"github.com/shirou/gopsutil/v3/cpu"
|
||||||
"github.com/shirou/gopsutil/v3/disk"
|
"github.com/shirou/gopsutil/v3/disk"
|
||||||
"github.com/shirou/gopsutil/v3/mem"
|
"github.com/shirou/gopsutil/v3/mem"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
"github.com/wdvxdr1123/ZeroBot/message"
|
"github.com/wdvxdr1123/ZeroBot/message"
|
||||||
@ -20,8 +22,20 @@ func init() { // 插件主体
|
|||||||
engine := control.Register("aifalse", &control.Options{
|
engine := control.Register("aifalse", &control.Options{
|
||||||
DisableOnDefault: false,
|
DisableOnDefault: false,
|
||||||
Help: "AIfalse\n" +
|
Help: "AIfalse\n" +
|
||||||
"- 查询计算机当前活跃度: [检查身体 | 自检 | 启动自检 | 系统状态]",
|
"- 查询计算机当前活跃度: [检查身体 | 自检 | 启动自检 | 系统状态]\n" +
|
||||||
|
"- 设置默认限速为每 m [分钟 | 秒] n 次触发",
|
||||||
})
|
})
|
||||||
|
c, ok := control.Lookup("aifalse")
|
||||||
|
if !ok {
|
||||||
|
panic("register aifalse error")
|
||||||
|
}
|
||||||
|
m := c.GetData(0)
|
||||||
|
n := (m >> 16) & 0xffff
|
||||||
|
m &= 0xffff
|
||||||
|
if m != 0 || n != 0 {
|
||||||
|
ctxext.SetDefaultLimiterManagerParam(time.Duration(m)*time.Second, int(n))
|
||||||
|
logrus.Infoln("设置默认限速为每", m, "秒触发", n, "次")
|
||||||
|
}
|
||||||
engine.OnFullMatchGroup([]string{"检查身体", "自检", "启动自检", "系统状态"}, zero.AdminPermission).SetBlock(true).
|
engine.OnFullMatchGroup([]string{"检查身体", "自检", "启动自检", "系统状态"}, zero.AdminPermission).SetBlock(true).
|
||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
ctx.SendChain(message.Text(
|
ctx.SendChain(message.Text(
|
||||||
@ -31,14 +45,37 @@ func init() { // 插件主体
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
engine.OnFullMatch("清理缓存", zero.SuperUserPermission).SetBlock(true).
|
engine.OnRegex(`^设置默认限速为每\s*(\d+)\s*(分钟|秒)\s*(\d+)\s*次触发$`, zero.SuperUserPermission).SetBlock(true).
|
||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
err := os.RemoveAll("data/cache/*")
|
c, ok := control.Lookup("aifalse")
|
||||||
if err != nil {
|
if !ok {
|
||||||
ctx.SendChain(message.Text("错误: ", err.Error()))
|
ctx.SendChain(message.Text("ERROR:no such plugin"))
|
||||||
} else {
|
return
|
||||||
ctx.SendChain(message.Text("成功!"))
|
|
||||||
}
|
}
|
||||||
|
m, err := strconv.ParseInt(ctx.State["regex_matched"].([]string)[1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
ctx.SendChain(message.Text("ERROR:", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ctx.State["regex_matched"].([]string)[2] == "分钟" {
|
||||||
|
m *= 60
|
||||||
|
}
|
||||||
|
if m >= 65536 || m <= 0 {
|
||||||
|
ctx.SendChain(message.Text("ERROR:interval too big"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n, err := strconv.ParseInt(ctx.State["regex_matched"].([]string)[3], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
ctx.SendChain(message.Text("ERROR:", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if n >= 65536 || n <= 0 {
|
||||||
|
ctx.SendChain(message.Text("ERROR:burst too big"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctxext.SetDefaultLimiterManagerParam(time.Duration(m)*time.Second, int(n))
|
||||||
|
c.SetData(0, (m&0xffff)|((n<<16)&0xffff0000))
|
||||||
|
ctx.SendChain(message.Text("设置默认限速为每", m, "秒触发", n, "次"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user