Skip to content

Commit 309efe8

Browse files
committed
增加定时指令触发器插件
1 parent bf54789 commit 309efe8

File tree

11 files changed

+173
-6
lines changed

11 files changed

+173
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ zerobot [-h] [-t token] [-u url] [-n nickname] [-p prefix] [-d|w] [-g 监听地
6666
- [x] /服务列表
6767
- [x] /服务详情
6868
- [x] @Bot 插件冲突检测 (会在本群发送一条消息并在约 1s 后撤回以检测其它同类 bot 中已启用的插件并禁用)
69+
- **定时指令触发器**
70+
- [x] 记录在"cron"触发的指令
71+
- [x] 取消在"cron"触发的指令
6972
- **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/chat"`
7073
- [x] [BOT名字]
7174
- [x] [戳一戳BOT]

data

Submodule data updated 1 file

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.17
55
require (
66
github.com/FloatTech/AnimeAPI v1.3.1-0.20220305143953-376e5d5b6c94
77
github.com/FloatTech/sqlite v0.2.0
8-
github.com/FloatTech/zbputils v1.3.1-0.20220304041409-6607dccd2a10
8+
github.com/FloatTech/zbputils v1.3.1-0.20220306130312-258cc20d69cb
99
github.com/antchfx/htmlquery v1.2.4
1010
github.com/corona10/goimagehash v1.0.3
1111
github.com/fogleman/gg v1.3.0

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ github.com/FloatTech/AnimeAPI v1.3.1-0.20220305143953-376e5d5b6c94/go.mod h1:hA4
33
github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ=
44
github.com/FloatTech/sqlite v0.2.0 h1:x3uls/hExXH1+bbaNLkvilce6ATtWlDx4IqoxBW/bv8=
55
github.com/FloatTech/sqlite v0.2.0/go.mod h1:xIDWIvpOFl8AXmZm0FC8t3PZjiR6ZutytCpBv2EWCns=
6-
github.com/FloatTech/zbputils v1.3.1-0.20220304041409-6607dccd2a10 h1:akteVs9gqHzPZuX1gvRiT/1HoSGD9DcO/kWrcWG/7p0=
76
github.com/FloatTech/zbputils v1.3.1-0.20220304041409-6607dccd2a10/go.mod h1:ts1Srsgp6iZlvo3K/7Q2NtwQyD1/AzG4EaPxRitR0S4=
7+
github.com/FloatTech/zbputils v1.3.1-0.20220306130312-258cc20d69cb h1:ykdnk1600xRXJtIvm5Xj7MnbWimwY5GR9K4GBM5Ammo=
8+
github.com/FloatTech/zbputils v1.3.1-0.20220306130312-258cc20d69cb/go.mod h1:ts1Srsgp6iZlvo3K/7Q2NtwQyD1/AzG4EaPxRitR0S4=
89
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
910
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0=
1011
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc/go.mod h1:OMmITAib6POA37xCichWM0aRnoVpSMZO1rB/G01wrr0=

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import (
7676
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/hs" // 炉石
7777
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/image_finder" // 关键字搜图
7878
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/jandan" // 煎蛋网无聊图
79+
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/job" // 炉石
7980
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/juejuezi" // 绝绝子生成器
8081
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/lolicon" // lolicon 随机图片
8182
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/moyu" // 摸鱼
@@ -140,6 +141,7 @@ import (
140141
// //
141142
// -----------------------以下为内置依赖,勿动------------------------ //
142143
"github.com/FloatTech/zbputils/control/order"
144+
"github.com/FloatTech/zbputils/process"
143145
"github.com/fumiama/go-registry"
144146
"github.com/sirupsen/logrus"
145147
zero "github.com/wdvxdr1123/ZeroBot"
@@ -235,7 +237,7 @@ func main() {
235237
Handle(func(ctx *zero.Ctx) {
236238
ctx.SendChain(message.Text(getKanban()))
237239
})
238-
zero.RunAndBlock(
240+
zero.Run(
239241
zero.Config{
240242
NickName: append([]string{*adana}, nicks...),
241243
CommandPrefix: *prefix,
@@ -245,4 +247,6 @@ func main() {
245247
Driver: []zero.Driver{driver.NewWebSocketClient(*url, *token)},
246248
},
247249
)
250+
process.GlobalInitMutex.Unlock()
251+
select {}
248252
}

plugin/acgimage/classify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ func reply(ctx *zero.Ctx, class int, dhash string, comment string) error {
155155
}
156156
} else {
157157
send = func(msg interface{}) int64 {
158-
return int64(ctx.SendGroupForwardMessage(ctx.Event.GroupID, message.Message{
158+
return ctx.SendGroupForwardMessage(ctx.Event.GroupID, message.Message{
159159
ctxext.FakeSenderForwardNode(ctx, append(
160160
msg.(message.Message),
161161
message.Text(comment))...,
162162
),
163-
}).Get("message_id").Int())
163+
}).Get("message_id").Int()
164164
}
165165
}
166166

plugin/diana/data/text.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
sql "github.com/FloatTech/sqlite"
99
binutils "github.com/FloatTech/zbputils/binary"
10+
"github.com/FloatTech/zbputils/control/order"
1011
"github.com/FloatTech/zbputils/file"
1112
"github.com/sirupsen/logrus"
1213
)
@@ -20,6 +21,7 @@ type text struct {
2021

2122
// LoadText 加载小作文
2223
func LoadText(dbfile string) {
24+
defer order.DoneOnExit()()
2325
_, err := file.GetLazyData(dbfile, false, false)
2426
db.DBPath = dbfile
2527
if err != nil {

plugin/job/main.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Package job 定时指令触发器
2+
package job
3+
4+
import (
5+
"hash/crc64"
6+
"strconv"
7+
"sync"
8+
"time"
9+
10+
"github.com/FloatTech/zbputils/binary"
11+
"github.com/FloatTech/zbputils/control"
12+
"github.com/FloatTech/zbputils/control/order"
13+
"github.com/FloatTech/zbputils/process"
14+
"github.com/FloatTech/zbputils/vevent"
15+
"github.com/fumiama/cron"
16+
"github.com/sirupsen/logrus"
17+
zero "github.com/wdvxdr1123/ZeroBot"
18+
"github.com/wdvxdr1123/ZeroBot/message"
19+
)
20+
21+
var (
22+
lo map[int64]vevent.Loop
23+
entries map[int64]cron.EntryID // id entryid
24+
mu sync.Mutex
25+
)
26+
27+
func init() {
28+
en := control.Register("job", order.AcquirePrio(), &control.Options{
29+
DisableOnDefault: false,
30+
Help: "定时指令触发器\n- 记录在\"cron\"触发的指令\n- 取消在\"cron\"触发的指令",
31+
PrivateDataFolder: "job",
32+
})
33+
db.DBPath = en.DataFolder() + "job.db"
34+
err := db.Open()
35+
if err != nil {
36+
panic(err)
37+
}
38+
go func() {
39+
process.GlobalInitMutex.Lock()
40+
process.SleepAbout1sTo2s()
41+
lo = make(map[int64]vevent.Loop, len(zero.BotConfig.Driver))
42+
entries = map[int64]cron.EntryID{}
43+
for _, drv := range zero.BotConfig.Driver {
44+
id := drv.SelfID()
45+
ids := strconv.FormatInt(id, 36)
46+
c := &cmd{}
47+
lo[id] = vevent.NewLoop(id)
48+
err := db.Create(ids, c)
49+
logrus.Infoln("[job]创建表", ids)
50+
if err != nil {
51+
panic(err)
52+
}
53+
db.FindFor(ids, c, "", func() error {
54+
mu.Lock()
55+
defer mu.Unlock()
56+
eid, err := process.CronTab.AddFunc(c.Cron, inject(id, []byte(c.Cmd)))
57+
if err != nil {
58+
return err
59+
}
60+
entries[c.ID] = eid
61+
return nil
62+
})
63+
}
64+
logrus.Infoln("[job]本地环回初始化完成")
65+
process.GlobalInitMutex.Unlock()
66+
}()
67+
en.OnRegex(`^记录在"(.*)"触发的指令$`, zero.AdminPermission, islonotnil, func(ctx *zero.Ctx) bool {
68+
ctx.SendChain(message.Text("您的下一条指令将被记录,在", ctx.State["regex_matched"].([]string)[1], "时触发"))
69+
select {
70+
case <-time.After(time.Second * 120):
71+
ctx.SendChain(message.Text("指令记录超时"))
72+
return false
73+
case e := <-zero.NewFutureEvent("message", 0, false, zero.CheckUser(ctx.Event.UserID)).Next():
74+
ctx.State["job_raw_event"] = e.RawEvent.Raw
75+
return true
76+
}
77+
}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
78+
cron := ctx.State["regex_matched"].([]string)[1]
79+
command := ctx.State["job_raw_event"].(string)
80+
c := &cmd{
81+
ID: idof(cron, command),
82+
Cron: cron,
83+
Cmd: command,
84+
}
85+
err := addcmd(ctx.Event.SelfID, c)
86+
if err != nil {
87+
ctx.SendChain(message.Text("ERROR:", err))
88+
return
89+
}
90+
ctx.SendChain(message.Text("成功!"))
91+
})
92+
en.OnRegex(`^取消在"(.*)"触发的指令$`, zero.AdminPermission, islonotnil).SetBlock(true).Handle(func(ctx *zero.Ctx) {
93+
cron := ctx.State["regex_matched"].([]string)[1]
94+
err := rmcmd(ctx.Event.SelfID, cron)
95+
if err != nil {
96+
ctx.SendChain(message.Text("ERROR:", err))
97+
return
98+
}
99+
ctx.SendChain(message.Text("成功!"))
100+
})
101+
}
102+
103+
func islonotnil(ctx *zero.Ctx) bool {
104+
return len(lo) > 0
105+
}
106+
107+
func inject(bot int64, response []byte) func() {
108+
return func() {
109+
lo[bot].Echo(response)
110+
}
111+
}
112+
113+
func idof(cron, cmd string) int64 {
114+
return int64(crc64.Checksum(binary.StringToBytes(cron+cmd), crc64.MakeTable(crc64.ISO)))
115+
}
116+
117+
func addcmd(bot int64, c *cmd) error {
118+
mu.Lock()
119+
defer mu.Unlock()
120+
eid, err := process.CronTab.AddFunc(c.Cron, inject(bot, []byte(c.Cmd)))
121+
if err != nil {
122+
return err
123+
}
124+
entries[c.ID] = eid
125+
return db.Insert(strconv.FormatInt(bot, 36), c)
126+
}
127+
128+
func rmcmd(bot int64, cron string) error {
129+
c := &cmd{}
130+
mu.Lock()
131+
defer mu.Unlock()
132+
bots := strconv.FormatInt(bot, 36)
133+
err := db.FindFor(bots, c, "WHERE cron='"+cron+"'", func() error {
134+
eid, ok := entries[c.ID]
135+
if ok {
136+
process.CronTab.Remove(eid)
137+
delete(entries, c.ID)
138+
}
139+
return nil
140+
})
141+
db.Del(bots, "WHERE cron='"+cron+"'")
142+
return err
143+
}

plugin/job/model.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package job
2+
3+
import sql "github.com/FloatTech/sqlite"
4+
5+
type cmd struct {
6+
ID int64 `db:"id"`
7+
Cron string `db:"cron"`
8+
Cmd string `db:"cmd"`
9+
}
10+
11+
var db = &sql.Sqlite{}

plugin/reborn/load.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package reborn
33
import (
44
"encoding/json"
55

6+
"github.com/FloatTech/zbputils/control/order"
67
"github.com/FloatTech/zbputils/file"
78
)
89

910
// load 加载rate数据
1011
func load(area *rate, jsonfile string) error {
12+
defer order.DoneOnExit()()
1113
data, err := file.GetLazyData(jsonfile, true, true)
1214
if err != nil {
1315
return err

0 commit comments

Comments
 (0)