@@ -4,13 +4,15 @@ package aifalse
4
4
import (
5
5
"fmt"
6
6
"math"
7
- "os "
7
+ "strconv "
8
8
"time"
9
9
10
10
control "github.com/FloatTech/zbputils/control"
11
+ "github.com/FloatTech/zbputils/ctxext"
11
12
"github.com/shirou/gopsutil/v3/cpu"
12
13
"github.com/shirou/gopsutil/v3/disk"
13
14
"github.com/shirou/gopsutil/v3/mem"
15
+ "github.com/sirupsen/logrus"
14
16
15
17
zero "github.com/wdvxdr1123/ZeroBot"
16
18
"github.com/wdvxdr1123/ZeroBot/message"
@@ -20,8 +22,20 @@ func init() { // 插件主体
20
22
engine := control .Register ("aifalse" , & control.Options {
21
23
DisableOnDefault : false ,
22
24
Help : "AIfalse\n " +
23
- "- 查询计算机当前活跃度: [检查身体 | 自检 | 启动自检 | 系统状态]" ,
25
+ "- 查询计算机当前活跃度: [检查身体 | 自检 | 启动自检 | 系统状态]\n " +
26
+ "- 设置默认限速为每 m [分钟 | 秒] n 次触发" ,
24
27
})
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
+ }
25
39
engine .OnFullMatchGroup ([]string {"检查身体" , "自检" , "启动自检" , "系统状态" }, zero .AdminPermission ).SetBlock (true ).
26
40
Handle (func (ctx * zero.Ctx ) {
27
41
ctx .SendChain (message .Text (
@@ -31,14 +45,37 @@ func init() { // 插件主体
31
45
),
32
46
)
33
47
})
34
- engine .OnFullMatch ( "清理缓存" , zero .SuperUserPermission ).SetBlock (true ).
48
+ engine .OnRegex ( `^设置默认限速为每\s*(\d+)\s*(分钟|秒)\s*(\d+)\s*次触发$` , zero .SuperUserPermission ).SetBlock (true ).
35
49
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 )
37
68
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
41
75
}
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 , "次" ))
42
79
})
43
80
}
44
81
0 commit comments