job增加参数读取

This commit is contained in:
源文雨 2022-03-08 18:07:59 +08:00
parent 156e9f07ad
commit 86fc5c51c8
2 changed files with 79 additions and 33 deletions

View File

@ -71,14 +71,14 @@ zerobot [-h] [-t token] [-u url] [-n nickname] [-p prefix] [-d|w] [-g 监听地
- [x] 取消在"cron"触发的指令 - [x] 取消在"cron"触发的指令
- [x] 查看所有触发指令 - [x] 查看所有触发指令
- [x] 查看在"cron"触发的指令 - [x] 查看在"cron"触发的指令
- [x] 注入指令结果:任意指令 - [x] 注入指令结果:任意指令,可以使用形如`?::参数1提示语::1!`,`?::参数2提示语::2!`的未定参数,在注入式一一匹配
- 一些示例 - 一些示例
> 定时指令触发器编程实现每日9:30推送摸鱼人日历示例 > 每日9:30推送摸鱼人日历
``` ```
记录在"30 9 * * *"触发的指令 记录在"30 9 * * *"触发的指令
run[CQ:image,file=https://api.vvhan.com/api/moyu] run[CQ:image,file=https://api.vvhan.com/api/moyu]
``` ```
> 定时指令触发器编程实现每日12:00以1/2概率执行coser指令 > 每日12:00以1/2概率执行coser指令
```python ```python
记录在"0 12 * * *"触发的指令 记录在"0 12 * * *"触发的指令
注入指令结果:>runcoderaw py 注入指令结果:>runcoderaw py
@ -86,6 +86,13 @@ from random import random
if random() > 0.5: print('coser') if random() > 0.5: print('coser')
else: print('今天没有coser哦~') else: print('今天没有coser哦~')
``` ```
> 每日15:00询问设置定时者否想看coser
```python
记录在"0 15 * * *"触发的指令
注入指令结果:>runcoderaw py
if '?::想看coser吗::1!' == '想': print('coser')
else: print('好吧')
```
- **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/chat"` - **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/chat"`
- [x] [BOT名字] - [x] [BOT名字]
- [x] [戳一戳BOT] - [x] [戳一戳BOT]

View File

@ -146,9 +146,9 @@ func init() {
} }
ctx.SendChain(message.Text(lst)) ctx.SendChain(message.Text(lst))
}) })
en.OnPrefix("注入指令结果:", ctxext.UserOrGrpAdmin, islonotnil).SetBlock(true).Handle(func(ctx *zero.Ctx) { en.OnPrefix("注入指令结果:", ctxext.UserOrGrpAdmin, islonotnil, func(ctx *zero.Ctx) bool {
command := ctx.State["args"].(string) return ctx.State["args"].(string) != ""
if command != "" { }, parseArgs).SetBlock(true).Handle(func(ctx *zero.Ctx) {
vevent.NewLoopOf(vevent.NewAPICallerHook(ctx, func(rsp zero.APIResponse, err error) { vevent.NewLoopOf(vevent.NewAPICallerHook(ctx, func(rsp zero.APIResponse, err error) {
if err == nil { if err == nil {
logrus.Debugln("[job] CallerHook returned") logrus.Debugln("[job] CallerHook returned")
@ -176,7 +176,6 @@ func init() {
cl() cl()
} }
})).Echo([]byte(strings.ReplaceAll(ctx.Event.RawEvent.Raw, "注入指令结果:", ""))) })).Echo([]byte(strings.ReplaceAll(ctx.Event.RawEvent.Raw, "注入指令结果:", "")))
}
}) })
} }
@ -225,3 +224,43 @@ func rmcmd(bot int64, cron string) error {
} }
return db.Del(bots, "WHERE cron='"+cron+"'") return db.Del(bots, "WHERE cron='"+cron+"'")
} }
func parseArgs(ctx *zero.Ctx) bool {
if !strings.Contains(ctx.State["args"].(string), "?::") {
return true
}
args := make(map[int]string)
for strings.Contains(ctx.Event.RawEvent.Raw, "?::") {
start := strings.Index(ctx.Event.RawEvent.Raw, "?::")
msgend := strings.Index(ctx.Event.RawEvent.Raw[start+3:], "::") + start + 3
numend := strings.Index(ctx.Event.RawEvent.Raw[msgend+2:], "!") + msgend + 2
logrus.Debugln("[job]", start, msgend, numend)
msg := ctx.Event.RawEvent.Raw[start+3 : msgend]
arg, err := strconv.Atoi(ctx.Event.RawEvent.Raw[msgend+2 : numend])
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return false
}
arr, ok := args[arg]
if !ok {
if msg == "" {
ctx.SendChain(message.At(ctx.Event.UserID), message.Text("请输入参数", arg))
} else {
ctx.SendChain(message.At(ctx.Event.UserID), message.Text("[", arg, "] ", msg))
}
select {
case <-time.After(time.Second * 120):
ctx.SendChain(message.Text("参数读取超时"))
return false
case e := <-zero.NewFutureEvent("message", 0, false, zero.CheckUser(ctx.Event.UserID)).Next():
args[arg] = e.Message.String()
ctx.SendChain(message.Reply(e.MessageID), message.Text("已记录"))
process.SleepAbout1sTo2s()
arr = args[arg]
}
}
ctx.Event.RawEvent.Raw = ctx.Event.RawEvent.Raw[:start] + arr + ctx.Event.RawEvent.Raw[numend+1:]
}
ctx.SendChain(message.Text("指令读取完成"))
return true
}