diff --git a/README.md b/README.md index a8d14505..53da9c6f 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ zerobot [-h] [-t token] [-u url] [-n nickname] [-p prefix] [-d|w] [-g 监听地 - **定时指令触发器** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/job"` - [x] 记录在"cron"触发的指令 - [x] 取消在"cron"触发的指令 + - [x] 查看所有触发指令 + - [x] 查看在"cron"触发的指令 - **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/chat"` - [x] [BOT名字] - [x] [戳一戳BOT] diff --git a/plugin/job/main.go b/plugin/job/main.go index f1c92e30..19758e32 100644 --- a/plugin/job/main.go +++ b/plugin/job/main.go @@ -24,13 +24,13 @@ var ( lo map[int64]vevent.Loop entries map[int64]cron.EntryID // id entryid mu sync.Mutex - limit = rate.NewLimiter(time.Second*10, 5) + limit = rate.NewLimiter(time.Second*2, 1) ) func init() { en := control.Register("job", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, - Help: "定时指令触发器\n- 记录在\"cron\"触发的指令\n- 取消在\"cron\"触发的指令", + Help: "定时指令触发器\n- 记录在\"cron\"触发的指令\n- 取消在\"cron\"触发的指令\n- 查看所有触发指令\n- 查看在\"cron\"触发的指令", PrivateDataFolder: "job", }) db.DBPath = en.DataFolder() + "job.db" @@ -101,6 +101,49 @@ func init() { } ctx.SendChain(message.Text("成功!")) }) + en.OnFullMatch("查看所有触发指令", zero.SuperUserPermission, islonotnil).SetBlock(true).Handle(func(ctx *zero.Ctx) { + c := &cmd{} + ids := strconv.FormatInt(ctx.Event.SelfID, 36) + mu.Lock() + defer mu.Unlock() + n, err := db.Count(ids) + if err != nil { + ctx.SendChain(message.Text("ERROR:", err)) + return + } + lst := make([]string, 0, n) + err = db.FindFor(ids, c, "GROUP BY cron", func() error { + lst = append(lst, c.Cron+"\n") + return nil + }) + if err != nil { + ctx.SendChain(message.Text("ERROR:", err)) + return + } + ctx.SendChain(message.Text(lst)) + }) + en.OnRegex(`^查看在"(.*)"触发的指令$`, zero.SuperUserPermission, islonotnil).SetBlock(true).Handle(func(ctx *zero.Ctx) { + c := &cmd{} + ids := strconv.FormatInt(ctx.Event.SelfID, 36) + cron := ctx.State["regex_matched"].([]string)[1] + mu.Lock() + defer mu.Unlock() + n, err := db.Count(ids) + if err != nil { + ctx.SendChain(message.Text("ERROR:", err)) + return + } + lst := make([]string, 0, n) + err = db.FindFor(ids, c, "WHERE cron='"+cron+"'", func() error { + lst = append(lst, c.Cmd+"\n") + return nil + }) + if err != nil { + ctx.SendChain(message.Text("ERROR:", err)) + return + } + ctx.SendChain(message.Text(lst)) + }) } func islonotnil(ctx *zero.Ctx) bool {