This commit is contained in:
源文雨 2022-03-20 18:49:20 +08:00
parent 6e2b10b788
commit 2250adf854
2 changed files with 11 additions and 4 deletions

View File

@ -281,7 +281,7 @@ func init() { // 插件主体
dateStrs := ctx.State["regex_matched"].([]string) dateStrs := ctx.State["regex_matched"].([]string)
ts := timer.GetFilledTimer(dateStrs, ctx.Event.SelfID, ctx.Event.GroupID, false) ts := timer.GetFilledTimer(dateStrs, ctx.Event.SelfID, ctx.Event.GroupID, false)
if ts.En() { if ts.En() {
go clock.RegisterTimer(ts, true) go clock.RegisterTimer(ts, true, false)
ctx.SendChain(message.Text("记住了~")) ctx.SendChain(message.Text("记住了~"))
} else { } else {
ctx.SendChain(message.Text("参数非法:" + ts.Alert)) ctx.SendChain(message.Text("参数非法:" + ts.Alert))
@ -304,7 +304,7 @@ func init() { // 插件主体
} }
logrus.Debugln("[manager] cron:", dateStrs[1]) logrus.Debugln("[manager] cron:", dateStrs[1])
ts := timer.GetFilledCronTimer(dateStrs[1], alert, url, ctx.Event.SelfID, ctx.Event.GroupID) ts := timer.GetFilledCronTimer(dateStrs[1], alert, url, ctx.Event.SelfID, ctx.Event.GroupID)
if clock.RegisterTimer(ts, true) { if clock.RegisterTimer(ts, true, false) {
ctx.SendChain(message.Text("记住了~")) ctx.SendChain(message.Text("记住了~"))
} else { } else {
ctx.SendChain(message.Text("参数非法:" + ts.Alert)) ctx.SendChain(message.Text("参数非法:" + ts.Alert))

View File

@ -8,6 +8,7 @@ import (
"time" "time"
sql "github.com/FloatTech/sqlite" sql "github.com/FloatTech/sqlite"
"github.com/FloatTech/zbputils/process"
"github.com/fumiama/cron" "github.com/fumiama/cron"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
@ -47,7 +48,7 @@ func NewClock(db *sql.Sqlite) (c Clock) {
} }
// RegisterTimer 注册计时器 // RegisterTimer 注册计时器
func (c *Clock) RegisterTimer(ts *Timer, save bool) bool { func (c *Clock) RegisterTimer(ts *Timer, save, isinit bool) bool {
var key uint32 var key uint32
if save { if save {
key = ts.GetTimerID() key = ts.GetTimerID()
@ -62,6 +63,9 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
logrus.Println("[群管]注册计时器", key) logrus.Println("[群管]注册计时器", key)
if ts.Cron != "" { if ts.Cron != "" {
var ctx *zero.Ctx var ctx *zero.Ctx
if isinit {
process.GlobalInitMutex.Lock()
}
if ts.SelfID != 0 { if ts.SelfID != 0 {
ctx = zero.GetBot(ts.SelfID) ctx = zero.GetBot(ts.SelfID)
} else { } else {
@ -71,6 +75,9 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
return false return false
}) })
} }
if isinit {
process.GlobalInitMutex.Unlock()
}
eid, err := c.cron.AddFunc(ts.Cron, func() { ts.sendmsg(ts.GrpID, ctx) }) eid, err := c.cron.AddFunc(ts.Cron, func() { ts.sendmsg(ts.GrpID, ctx) })
if err == nil { if err == nil {
c.entmu.Lock() c.entmu.Lock()
@ -187,7 +194,7 @@ func (c *Clock) loadTimers(db *sql.Sqlite) {
var t Timer var t Timer
_ = c.db.FindFor("timer", &t, "", func() error { _ = c.db.FindFor("timer", &t, "", func() error {
tescape := t tescape := t
go c.RegisterTimer(&tescape, false) go c.RegisterTimer(&tescape, false, true)
return nil return nil
}) })
} }