✏️ 修复 manager 定时器错误

同时 make lint happy
This commit is contained in:
fumiama 2021-12-26 14:07:05 +08:00
parent 1682c321d3
commit cb44758036
14 changed files with 74 additions and 50 deletions

View File

@ -1,3 +1,4 @@
// Package bookreview 书评
package bookreview package bookreview
import ( import (

View File

@ -7,11 +7,11 @@ type book struct {
// 暂时随机选择一个书评 // 暂时随机选择一个书评
func getBookReviewByKeyword(keyword string) (b book) { func getBookReviewByKeyword(keyword string) (b book) {
db.Find("bookreview", &b, "where bookreview LIKE '%"+keyword+"%'") _ = db.Find("bookreview", &b, "where bookreview LIKE '%"+keyword+"%'")
return return
} }
func getRandomBookReview() (b book) { func getRandomBookReview() (b book) {
db.Pick("bookreview", &b) _ = db.Pick("bookreview", &b)
return return
} }

View File

@ -20,7 +20,10 @@ func initChatList(postinit func()) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
json.Unmarshal(data, &kimomap) err = json.Unmarshal(data, &kimomap)
if err != nil {
panic(err)
}
for k := range kimomap { for k := range kimomap {
chatList = append(chatList, k) chatList = append(chatList, k)
} }

View File

@ -66,11 +66,15 @@ func init() {
if ok { if ok {
c, ok := control.Lookup("fortune") c, ok := control.Lookup("fortune")
if ok { if ok {
c.SetData(gid, int64(i)&0xff) err = c.SetData(gid, int64(i)&0xff)
if err != nil {
ctx.SendChain(message.Text("设置失败:", err))
return
}
ctx.SendChain(message.Text("设置成功~")) ctx.SendChain(message.Text("设置成功~"))
return return
} }
ctx.SendChain(message.Text("设置失败!")) ctx.SendChain(message.Text("设置失败: 找不到插件"))
return return
} }
ctx.SendChain(message.Text("没有这个底图哦~")) ctx.SendChain(message.Text("没有这个底图哦~"))

View File

@ -314,7 +314,7 @@ func init() { // 插件主体
engine.OnRegex(`^取消在"(.*)"的提醒`, zero.AdminPermission, zero.OnlyGroup).SetBlock(true).SetPriority(40). engine.OnRegex(`^取消在"(.*)"的提醒`, zero.AdminPermission, zero.OnlyGroup).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
dateStrs := ctx.State["regex_matched"].([]string) 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() ti := ts.GetTimerID()
ok := clock.CancelTimer(ti) ok := clock.CancelTimer(ti)
if ok { if ok {

View File

@ -8,9 +8,9 @@ import (
func (ts *Timer) sendmsg(grp int64, ctx *zero.Ctx) { func (ts *Timer) sendmsg(grp int64, ctx *zero.Ctx) {
ctx.Event = new(zero.Event) ctx.Event = new(zero.Event)
ctx.Event.GroupID = grp ctx.Event.GroupID = grp
if ts.Url == "" { if ts.URL == "" {
ctx.SendChain(atall, message.Text(ts.Alert)) ctx.SendChain(atall, message.Text(ts.Alert))
} else { } 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"))
} }
} }

View File

@ -16,9 +16,9 @@ import (
// GetTimerInfo 获得标准化定时字符串 // GetTimerInfo 获得标准化定时字符串
func (ts *Timer) GetTimerInfo() string { func (ts *Timer) GetTimerInfo() string {
if ts.Cron != "" { 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 // GetTimerID 获得标准化 ID
@ -33,9 +33,9 @@ func GetFilledCronTimer(croncmd string, alert string, img string, botqq, gid int
var ts Timer var ts Timer
ts.Alert = alert ts.Alert = alert
ts.Cron = croncmd ts.Cron = croncmd
ts.Url = img ts.URL = img
ts.Selfid = botqq ts.SelfID = botqq
ts.GrpId = gid ts.GrpID = gid
return &ts return &ts
} }
@ -105,10 +105,10 @@ func GetFilledTimer(dateStrs []string, botqq, grp int64, matchDateOnly bool) *Ti
if !matchDateOnly { if !matchDateOnly {
urlStr := dateStrs[5] urlStr := dateStrs[5]
if urlStr != "" { // 是图片url if urlStr != "" { // 是图片url
ts.Url = urlStr[3:] // utf-8下用为3字节 ts.URL = urlStr[3:] // utf-8下用为3字节
logrus.Println("[群管]" + ts.Url) logrus.Println("[群管]" + ts.URL)
if !strings.HasPrefix(ts.Url, "http") { if !strings.HasPrefix(ts.URL, "http") {
ts.Url = "illegal" ts.URL = "illegal"
logrus.Println("[群管]url非法") logrus.Println("[群管]url非法")
return &ts return &ts
} }
@ -116,8 +116,8 @@ func GetFilledTimer(dateStrs []string, botqq, grp int64, matchDateOnly bool) *Ti
ts.Alert = dateStrs[6] ts.Alert = dateStrs[6]
ts.SetEn(true) ts.SetEn(true)
} }
ts.Selfid = botqq ts.SelfID = botqq
ts.GrpId = grp ts.GrpID = grp
return &ts return &ts
} }

View File

@ -152,11 +152,11 @@ func (ts *Timer) nextWakeTime() (date time.Time) {
func (ts *Timer) judgeHM() { func (ts *Timer) judgeHM() {
if ts.Hour() < 0 || ts.Hour() == time.Now().Hour() { if ts.Hour() < 0 || ts.Hour() == time.Now().Hour() {
if ts.Minute() < 0 || ts.Minute() == time.Now().Minute() { if ts.Minute() < 0 || ts.Minute() == time.Now().Minute() {
if ts.Selfid != 0 { if ts.SelfID != 0 {
ts.sendmsg(ts.GrpId, zero.GetBot(ts.Selfid)) ts.sendmsg(ts.GrpID, zero.GetBot(ts.SelfID))
} else { } else {
zero.RangeBot(func(id int64, ctx *zero.Ctx) (_ bool) { zero.RangeBot(func(id int64, ctx *zero.Ctx) (_ bool) {
ts.sendmsg(ts.GrpId, ctx) ts.sendmsg(ts.GrpID, ctx)
return return
}) })
} }

View File

@ -6,15 +6,16 @@ import (
// Timer 计时器 // Timer 计时器
type Timer struct { type Timer struct {
Id uint32 `db:"id"` ID uint32 `db:"id"`
En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"` En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"`
Selfid int64 `db:"sid"` SelfID int64 `db:"sid"`
GrpId int64 `db:"gid"` GrpID int64 `db:"gid"`
Alert string `db:"alert"` Alert string `db:"alert"`
Cron string `db:"cron"` Cron string `db:"cron"`
Url string `db:"url"` URL string `db:"url"`
} }
// InsertInto 插入自身
func (t *Timer) InsertInto(db *sql.Sqlite) error { func (t *Timer) InsertInto(db *sql.Sqlite) error {
return db.Insert("timer", t) return db.Insert("timer", t)
} }

View File

@ -12,10 +12,10 @@ import (
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
"github.com/FloatTech/ZeroBot-Plugin/utils/sql" "github.com/FloatTech/ZeroBot-Plugin/utils/sql"
) )
// Clock 时钟
type Clock struct { type Clock struct {
db *sql.Sqlite db *sql.Sqlite
timers *(map[uint32]*Timer) timers *(map[uint32]*Timer)
@ -37,10 +37,12 @@ var (
} }
) )
// NewClock 添加一个新时钟
func NewClock(db *sql.Sqlite) (c Clock) { func NewClock(db *sql.Sqlite) (c Clock) {
c.loadTimers(db) c.loadTimers(db)
c.cron = cron.New() c.cron = cron.New()
c.entries = make(map[uint32]cron.EntryID) c.entries = make(map[uint32]cron.EntryID)
c.timers = &map[uint32]*Timer{}
c.cron.Start() c.cron.Start()
return return
} }
@ -50,9 +52,9 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
var key uint32 var key uint32
if save { if save {
key = ts.GetTimerID() key = ts.GetTimerID()
ts.Id = key ts.ID = key
} else { } else {
key = ts.Id key = ts.ID
} }
t, ok := c.GetTimer(key) t, ok := c.GetTimer(key)
if t != ts && ok { // 避免重复注册定时器 if t != ts && ok { // 避免重复注册定时器
@ -61,16 +63,16 @@ 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 ts.Selfid != 0 { if ts.SelfID != 0 {
ctx = zero.GetBot(ts.Selfid) ctx = zero.GetBot(ts.SelfID)
} else { } else {
zero.RangeBot(func(id int64, c *zero.Ctx) bool { zero.RangeBot(func(id int64, c *zero.Ctx) bool {
ctx = c ctx = c
ts.Selfid = id ts.SelfID = id
return false 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 { if err == nil {
c.entmu.Lock() c.entmu.Lock()
c.entries[key] = eid c.entries[key] = eid
@ -135,7 +137,7 @@ func (c *Clock) ListTimers(grpID int64) []string {
c.timersmu.RLock() c.timersmu.RLock()
keys := make([]string, 0, len(*c.timers)) keys := make([]string, 0, len(*c.timers))
for _, v := range *c.timers { for _, v := range *c.timers {
if v.GrpId == grpID { if v.GrpID == grpID {
k := v.GetTimerInfo() k := v.GetTimerInfo()
start := strings.Index(k, "]") start := strings.Index(k, "]")
msg := strings.ReplaceAll(k[start+1:]+"\n", "-1", "每") msg := strings.ReplaceAll(k[start+1:]+"\n", "-1", "每")
@ -147,11 +149,11 @@ func (c *Clock) ListTimers(grpID int64) []string {
} }
c.timersmu.RUnlock() c.timersmu.RUnlock()
return keys return keys
} else {
return nil
} }
return nil
} }
// GetTimer 获得定时器
func (c *Clock) GetTimer(key uint32) (t *Timer, ok bool) { func (c *Clock) GetTimer(key uint32) (t *Timer, ok bool) {
c.timersmu.RLock() c.timersmu.RLock()
t, ok = (*c.timers)[key] t, ok = (*c.timers)[key]
@ -159,25 +161,24 @@ func (c *Clock) GetTimer(key uint32) (t *Timer, ok bool) {
return return
} }
// AddTimer 添加定时器
func (c *Clock) AddTimer(t *Timer) (err error) { func (c *Clock) AddTimer(t *Timer) (err error) {
c.timersmu.Lock() c.timersmu.Lock()
(*c.timers)[t.Id] = t (*c.timers)[t.ID] = t
err = c.db.Insert("timer", t) err = c.db.Insert("timer", t)
c.timersmu.Unlock() c.timersmu.Unlock()
return return
} }
func (c *Clock) loadTimers(db *sql.Sqlite) { func (c *Clock) loadTimers(db *sql.Sqlite) {
if file.IsExist(db.DBPath) {
c.db = db c.db = db
err := c.db.Create("timer", &Timer{}) err := c.db.Create("timer", &Timer{})
if err == nil { if err == nil {
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)
return nil return nil
}) })
} }
}
} }

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -22,3 +23,11 @@ func TestNextWakeTime(t *testing.T) {
t.Log(t1) t.Log(t1)
t.Fail() 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()
}

View File

@ -81,7 +81,11 @@ func init() {
url := ctx.State["image_url"].([]string)[0] url := ctx.State["image_url"].([]string)[0]
grpfolder := base + "/" + strconv.FormatInt(ctx.Event.GroupID, 36) grpfolder := base + "/" + strconv.FormatInt(ctx.Event.GroupID, 36)
if file.IsNotExist(grpfolder) { if file.IsNotExist(grpfolder) {
os.Mkdir(grpfolder, 0755) err = os.Mkdir(grpfolder, 0755)
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("错误:", err.Error()))
return
}
} }
err = file.DownloadTo(url, grpfolder+"/"+name, true) err = file.DownloadTo(url, grpfolder+"/"+name, true)
if err == nil { if err == nil {

View File

@ -3,7 +3,7 @@ package omikuji
import "strconv" import "strconv"
type kuji struct { type kuji struct {
Id uint8 `db:"id"` ID uint8 `db:"id"`
Text string `db:"text"` Text string `db:"text"`
} }

View File

@ -16,6 +16,7 @@ var (
) )
// DownloadTo 下载到路径 // DownloadTo 下载到路径
//nolint: bodyclose
func DownloadTo(url, file string, chkcrt bool) error { func DownloadTo(url, file string, chkcrt bool) error {
var resp *http.Response var resp *http.Response
var err error var err error