mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 02:00:24 +00:00
✏️ 修复 manager 定时器错误
同时 make lint happy
This commit is contained in:
@@ -314,7 +314,7 @@ func init() { // 插件主体
|
||||
engine.OnRegex(`^取消在"(.*)"的提醒`, zero.AdminPermission, zero.OnlyGroup).SetBlock(true).SetPriority(40).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
dateStrs := ctx.State["regex_matched"].([]string)
|
||||
ts := timer.Timer{Cron: dateStrs[1], GrpId: ctx.Event.GroupID}
|
||||
ts := timer.Timer{Cron: dateStrs[1], GrpID: ctx.Event.GroupID}
|
||||
ti := ts.GetTimerID()
|
||||
ok := clock.CancelTimer(ti)
|
||||
if ok {
|
||||
|
||||
@@ -8,9 +8,9 @@ import (
|
||||
func (ts *Timer) sendmsg(grp int64, ctx *zero.Ctx) {
|
||||
ctx.Event = new(zero.Event)
|
||||
ctx.Event.GroupID = grp
|
||||
if ts.Url == "" {
|
||||
if ts.URL == "" {
|
||||
ctx.SendChain(atall, message.Text(ts.Alert))
|
||||
} else {
|
||||
ctx.SendChain(atall, message.Text(ts.Alert), message.Image(ts.Url).Add("cache", "0"))
|
||||
ctx.SendChain(atall, message.Text(ts.Alert), message.Image(ts.URL).Add("cache", "0"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@ import (
|
||||
// GetTimerInfo 获得标准化定时字符串
|
||||
func (ts *Timer) GetTimerInfo() string {
|
||||
if ts.Cron != "" {
|
||||
return fmt.Sprintf("[%d]%s", ts.GrpId, ts.Cron)
|
||||
return fmt.Sprintf("[%d]%s", ts.GrpID, ts.Cron)
|
||||
}
|
||||
return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", ts.GrpId, ts.Month(), ts.Day(), ts.Week(), ts.Hour(), ts.Minute())
|
||||
return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", ts.GrpID, ts.Month(), ts.Day(), ts.Week(), ts.Hour(), ts.Minute())
|
||||
}
|
||||
|
||||
// GetTimerID 获得标准化 ID
|
||||
@@ -33,9 +33,9 @@ func GetFilledCronTimer(croncmd string, alert string, img string, botqq, gid int
|
||||
var ts Timer
|
||||
ts.Alert = alert
|
||||
ts.Cron = croncmd
|
||||
ts.Url = img
|
||||
ts.Selfid = botqq
|
||||
ts.GrpId = gid
|
||||
ts.URL = img
|
||||
ts.SelfID = botqq
|
||||
ts.GrpID = gid
|
||||
return &ts
|
||||
}
|
||||
|
||||
@@ -105,10 +105,10 @@ func GetFilledTimer(dateStrs []string, botqq, grp int64, matchDateOnly bool) *Ti
|
||||
if !matchDateOnly {
|
||||
urlStr := dateStrs[5]
|
||||
if urlStr != "" { // 是图片url
|
||||
ts.Url = urlStr[3:] // utf-8下用为3字节
|
||||
logrus.Println("[群管]" + ts.Url)
|
||||
if !strings.HasPrefix(ts.Url, "http") {
|
||||
ts.Url = "illegal"
|
||||
ts.URL = urlStr[3:] // utf-8下用为3字节
|
||||
logrus.Println("[群管]" + ts.URL)
|
||||
if !strings.HasPrefix(ts.URL, "http") {
|
||||
ts.URL = "illegal"
|
||||
logrus.Println("[群管]url非法!")
|
||||
return &ts
|
||||
}
|
||||
@@ -116,8 +116,8 @@ func GetFilledTimer(dateStrs []string, botqq, grp int64, matchDateOnly bool) *Ti
|
||||
ts.Alert = dateStrs[6]
|
||||
ts.SetEn(true)
|
||||
}
|
||||
ts.Selfid = botqq
|
||||
ts.GrpId = grp
|
||||
ts.SelfID = botqq
|
||||
ts.GrpID = grp
|
||||
return &ts
|
||||
}
|
||||
|
||||
|
||||
@@ -152,11 +152,11 @@ func (ts *Timer) nextWakeTime() (date time.Time) {
|
||||
func (ts *Timer) judgeHM() {
|
||||
if ts.Hour() < 0 || ts.Hour() == time.Now().Hour() {
|
||||
if ts.Minute() < 0 || ts.Minute() == time.Now().Minute() {
|
||||
if ts.Selfid != 0 {
|
||||
ts.sendmsg(ts.GrpId, zero.GetBot(ts.Selfid))
|
||||
if ts.SelfID != 0 {
|
||||
ts.sendmsg(ts.GrpID, zero.GetBot(ts.SelfID))
|
||||
} else {
|
||||
zero.RangeBot(func(id int64, ctx *zero.Ctx) (_ bool) {
|
||||
ts.sendmsg(ts.GrpId, ctx)
|
||||
ts.sendmsg(ts.GrpID, ctx)
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,15 +6,16 @@ import (
|
||||
|
||||
// Timer 计时器
|
||||
type Timer struct {
|
||||
Id uint32 `db:"id"`
|
||||
ID uint32 `db:"id"`
|
||||
En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"`
|
||||
Selfid int64 `db:"sid"`
|
||||
GrpId int64 `db:"gid"`
|
||||
SelfID int64 `db:"sid"`
|
||||
GrpID int64 `db:"gid"`
|
||||
Alert string `db:"alert"`
|
||||
Cron string `db:"cron"`
|
||||
Url string `db:"url"`
|
||||
URL string `db:"url"`
|
||||
}
|
||||
|
||||
// InsertInto 插入自身
|
||||
func (t *Timer) InsertInto(db *sql.Sqlite) error {
|
||||
return db.Insert("timer", t)
|
||||
}
|
||||
|
||||
@@ -12,10 +12,10 @@ import (
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
||||
)
|
||||
|
||||
// Clock 时钟
|
||||
type Clock struct {
|
||||
db *sql.Sqlite
|
||||
timers *(map[uint32]*Timer)
|
||||
@@ -37,10 +37,12 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// NewClock 添加一个新时钟
|
||||
func NewClock(db *sql.Sqlite) (c Clock) {
|
||||
c.loadTimers(db)
|
||||
c.cron = cron.New()
|
||||
c.entries = make(map[uint32]cron.EntryID)
|
||||
c.timers = &map[uint32]*Timer{}
|
||||
c.cron.Start()
|
||||
return
|
||||
}
|
||||
@@ -50,9 +52,9 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
|
||||
var key uint32
|
||||
if save {
|
||||
key = ts.GetTimerID()
|
||||
ts.Id = key
|
||||
ts.ID = key
|
||||
} else {
|
||||
key = ts.Id
|
||||
key = ts.ID
|
||||
}
|
||||
t, ok := c.GetTimer(key)
|
||||
if t != ts && ok { // 避免重复注册定时器
|
||||
@@ -61,16 +63,16 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
|
||||
logrus.Println("[群管]注册计时器", key)
|
||||
if ts.Cron != "" {
|
||||
var ctx *zero.Ctx
|
||||
if ts.Selfid != 0 {
|
||||
ctx = zero.GetBot(ts.Selfid)
|
||||
if ts.SelfID != 0 {
|
||||
ctx = zero.GetBot(ts.SelfID)
|
||||
} else {
|
||||
zero.RangeBot(func(id int64, c *zero.Ctx) bool {
|
||||
ctx = c
|
||||
ts.Selfid = id
|
||||
ts.SelfID = id
|
||||
return false
|
||||
})
|
||||
}
|
||||
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 {
|
||||
c.entmu.Lock()
|
||||
c.entries[key] = eid
|
||||
@@ -135,7 +137,7 @@ func (c *Clock) ListTimers(grpID int64) []string {
|
||||
c.timersmu.RLock()
|
||||
keys := make([]string, 0, len(*c.timers))
|
||||
for _, v := range *c.timers {
|
||||
if v.GrpId == grpID {
|
||||
if v.GrpID == grpID {
|
||||
k := v.GetTimerInfo()
|
||||
start := strings.Index(k, "]")
|
||||
msg := strings.ReplaceAll(k[start+1:]+"\n", "-1", "每")
|
||||
@@ -147,11 +149,11 @@ func (c *Clock) ListTimers(grpID int64) []string {
|
||||
}
|
||||
c.timersmu.RUnlock()
|
||||
return keys
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTimer 获得定时器
|
||||
func (c *Clock) GetTimer(key uint32) (t *Timer, ok bool) {
|
||||
c.timersmu.RLock()
|
||||
t, ok = (*c.timers)[key]
|
||||
@@ -159,25 +161,24 @@ func (c *Clock) GetTimer(key uint32) (t *Timer, ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// AddTimer 添加定时器
|
||||
func (c *Clock) AddTimer(t *Timer) (err error) {
|
||||
c.timersmu.Lock()
|
||||
(*c.timers)[t.Id] = t
|
||||
(*c.timers)[t.ID] = t
|
||||
err = c.db.Insert("timer", t)
|
||||
c.timersmu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Clock) loadTimers(db *sql.Sqlite) {
|
||||
if file.IsExist(db.DBPath) {
|
||||
c.db = db
|
||||
err := c.db.Create("timer", &Timer{})
|
||||
if err == nil {
|
||||
var t Timer
|
||||
c.db.FindFor("timer", &t, "", func() error {
|
||||
tescape := t
|
||||
go c.RegisterTimer(&tescape, false)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
c.db = db
|
||||
err := c.db.Create("timer", &Timer{})
|
||||
if err == nil {
|
||||
var t Timer
|
||||
_ = c.db.FindFor("timer", &t, "", func() error {
|
||||
tescape := t
|
||||
go c.RegisterTimer(&tescape, false)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -22,3 +23,11 @@ func TestNextWakeTime(t *testing.T) {
|
||||
t.Log(t1)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
func TestClock(t *testing.T) {
|
||||
db := &sql.Sqlite{DBPath: "test.db"}
|
||||
c := NewClock(db)
|
||||
c.AddTimer(GetFilledTimer([]string{"", "12", "-1", "12", "0", "", "test"}, 0, 0, false))
|
||||
t.Log(c.ListTimers(0))
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user