Skip to content

Commit cb44758

Browse files
committed
✏️ 修复 manager 定时器错误
同时 make lint happy
1 parent 1682c32 commit cb44758

File tree

14 files changed

+74
-50
lines changed

14 files changed

+74
-50
lines changed

plugin_book_review/book_review.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package bookreview 书评
12
package bookreview
23

34
import (

plugin_book_review/model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ type book struct {
77

88
// 暂时随机选择一个书评
99
func getBookReviewByKeyword(keyword string) (b book) {
10-
db.Find("bookreview", &b, "where bookreview LIKE '%"+keyword+"%'")
10+
_ = db.Find("bookreview", &b, "where bookreview LIKE '%"+keyword+"%'")
1111
return
1212
}
1313

1414
func getRandomBookReview() (b book) {
15-
db.Pick("bookreview", &b)
15+
_ = db.Pick("bookreview", &b)
1616
return
1717
}

plugin_chat/data.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ func initChatList(postinit func()) {
2020
if err != nil {
2121
panic(err)
2222
}
23-
json.Unmarshal(data, &kimomap)
23+
err = json.Unmarshal(data, &kimomap)
24+
if err != nil {
25+
panic(err)
26+
}
2427
for k := range kimomap {
2528
chatList = append(chatList, k)
2629
}

plugin_fortune/fortune.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ func init() {
6666
if ok {
6767
c, ok := control.Lookup("fortune")
6868
if ok {
69-
c.SetData(gid, int64(i)&0xff)
69+
err = c.SetData(gid, int64(i)&0xff)
70+
if err != nil {
71+
ctx.SendChain(message.Text("设置失败:", err))
72+
return
73+
}
7074
ctx.SendChain(message.Text("设置成功~"))
7175
return
7276
}
73-
ctx.SendChain(message.Text("设置失败!"))
77+
ctx.SendChain(message.Text("设置失败: 找不到插件"))
7478
return
7579
}
7680
ctx.SendChain(message.Text("没有这个底图哦~"))

plugin_manager/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func init() { // 插件主体
314314
engine.OnRegex(`^取消在"(.*)"的提醒`, zero.AdminPermission, zero.OnlyGroup).SetBlock(true).SetPriority(40).
315315
Handle(func(ctx *zero.Ctx) {
316316
dateStrs := ctx.State["regex_matched"].([]string)
317-
ts := timer.Timer{Cron: dateStrs[1], GrpId: ctx.Event.GroupID}
317+
ts := timer.Timer{Cron: dateStrs[1], GrpID: ctx.Event.GroupID}
318318
ti := ts.GetTimerID()
319319
ok := clock.CancelTimer(ti)
320320
if ok {

plugin_manager/timer/msg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
func (ts *Timer) sendmsg(grp int64, ctx *zero.Ctx) {
99
ctx.Event = new(zero.Event)
1010
ctx.Event.GroupID = grp
11-
if ts.Url == "" {
11+
if ts.URL == "" {
1212
ctx.SendChain(atall, message.Text(ts.Alert))
1313
} else {
14-
ctx.SendChain(atall, message.Text(ts.Alert), message.Image(ts.Url).Add("cache", "0"))
14+
ctx.SendChain(atall, message.Text(ts.Alert), message.Image(ts.URL).Add("cache", "0"))
1515
}
1616
}

plugin_manager/timer/parse.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import (
1616
// GetTimerInfo 获得标准化定时字符串
1717
func (ts *Timer) GetTimerInfo() string {
1818
if ts.Cron != "" {
19-
return fmt.Sprintf("[%d]%s", ts.GrpId, ts.Cron)
19+
return fmt.Sprintf("[%d]%s", ts.GrpID, ts.Cron)
2020
}
21-
return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", ts.GrpId, ts.Month(), ts.Day(), ts.Week(), ts.Hour(), ts.Minute())
21+
return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", ts.GrpID, ts.Month(), ts.Day(), ts.Week(), ts.Hour(), ts.Minute())
2222
}
2323

2424
// GetTimerID 获得标准化 ID
@@ -33,9 +33,9 @@ func GetFilledCronTimer(croncmd string, alert string, img string, botqq, gid int
3333
var ts Timer
3434
ts.Alert = alert
3535
ts.Cron = croncmd
36-
ts.Url = img
37-
ts.Selfid = botqq
38-
ts.GrpId = gid
36+
ts.URL = img
37+
ts.SelfID = botqq
38+
ts.GrpID = gid
3939
return &ts
4040
}
4141

@@ -105,19 +105,19 @@ func GetFilledTimer(dateStrs []string, botqq, grp int64, matchDateOnly bool) *Ti
105105
if !matchDateOnly {
106106
urlStr := dateStrs[5]
107107
if urlStr != "" { // 是图片url
108-
ts.Url = urlStr[3:] // utf-8下用为3字节
109-
logrus.Println("[群管]" + ts.Url)
110-
if !strings.HasPrefix(ts.Url, "http") {
111-
ts.Url = "illegal"
108+
ts.URL = urlStr[3:] // utf-8下用为3字节
109+
logrus.Println("[群管]" + ts.URL)
110+
if !strings.HasPrefix(ts.URL, "http") {
111+
ts.URL = "illegal"
112112
logrus.Println("[群管]url非法!")
113113
return &ts
114114
}
115115
}
116116
ts.Alert = dateStrs[6]
117117
ts.SetEn(true)
118118
}
119-
ts.Selfid = botqq
120-
ts.GrpId = grp
119+
ts.SelfID = botqq
120+
ts.GrpID = grp
121121
return &ts
122122
}
123123

plugin_manager/timer/sleep.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ func (ts *Timer) nextWakeTime() (date time.Time) {
152152
func (ts *Timer) judgeHM() {
153153
if ts.Hour() < 0 || ts.Hour() == time.Now().Hour() {
154154
if ts.Minute() < 0 || ts.Minute() == time.Now().Minute() {
155-
if ts.Selfid != 0 {
156-
ts.sendmsg(ts.GrpId, zero.GetBot(ts.Selfid))
155+
if ts.SelfID != 0 {
156+
ts.sendmsg(ts.GrpID, zero.GetBot(ts.SelfID))
157157
} else {
158158
zero.RangeBot(func(id int64, ctx *zero.Ctx) (_ bool) {
159-
ts.sendmsg(ts.GrpId, ctx)
159+
ts.sendmsg(ts.GrpID, ctx)
160160
return
161161
})
162162
}

plugin_manager/timer/timer.db.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66

77
// Timer 计时器
88
type Timer struct {
9-
Id uint32 `db:"id"`
9+
ID uint32 `db:"id"`
1010
En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"`
11-
Selfid int64 `db:"sid"`
12-
GrpId int64 `db:"gid"`
11+
SelfID int64 `db:"sid"`
12+
GrpID int64 `db:"gid"`
1313
Alert string `db:"alert"`
1414
Cron string `db:"cron"`
15-
Url string `db:"url"`
15+
URL string `db:"url"`
1616
}
1717

18+
// InsertInto 插入自身
1819
func (t *Timer) InsertInto(db *sql.Sqlite) error {
1920
return db.Insert("timer", t)
2021
}

plugin_manager/timer/timer.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
zero "github.com/wdvxdr1123/ZeroBot"
1313
"github.com/wdvxdr1123/ZeroBot/message"
1414

15-
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
1615
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
1716
)
1817

18+
// Clock 时钟
1919
type Clock struct {
2020
db *sql.Sqlite
2121
timers *(map[uint32]*Timer)
@@ -37,10 +37,12 @@ var (
3737
}
3838
)
3939

40+
// NewClock 添加一个新时钟
4041
func NewClock(db *sql.Sqlite) (c Clock) {
4142
c.loadTimers(db)
4243
c.cron = cron.New()
4344
c.entries = make(map[uint32]cron.EntryID)
45+
c.timers = &map[uint32]*Timer{}
4446
c.cron.Start()
4547
return
4648
}
@@ -50,9 +52,9 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
5052
var key uint32
5153
if save {
5254
key = ts.GetTimerID()
53-
ts.Id = key
55+
ts.ID = key
5456
} else {
55-
key = ts.Id
57+
key = ts.ID
5658
}
5759
t, ok := c.GetTimer(key)
5860
if t != ts && ok { // 避免重复注册定时器
@@ -61,16 +63,16 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
6163
logrus.Println("[群管]注册计时器", key)
6264
if ts.Cron != "" {
6365
var ctx *zero.Ctx
64-
if ts.Selfid != 0 {
65-
ctx = zero.GetBot(ts.Selfid)
66+
if ts.SelfID != 0 {
67+
ctx = zero.GetBot(ts.SelfID)
6668
} else {
6769
zero.RangeBot(func(id int64, c *zero.Ctx) bool {
6870
ctx = c
69-
ts.Selfid = id
71+
ts.SelfID = id
7072
return false
7173
})
7274
}
73-
eid, err := c.cron.AddFunc(ts.Cron, func() { ts.sendmsg(ts.GrpId, ctx) })
75+
eid, err := c.cron.AddFunc(ts.Cron, func() { ts.sendmsg(ts.GrpID, ctx) })
7476
if err == nil {
7577
c.entmu.Lock()
7678
c.entries[key] = eid
@@ -135,7 +137,7 @@ func (c *Clock) ListTimers(grpID int64) []string {
135137
c.timersmu.RLock()
136138
keys := make([]string, 0, len(*c.timers))
137139
for _, v := range *c.timers {
138-
if v.GrpId == grpID {
140+
if v.GrpID == grpID {
139141
k := v.GetTimerInfo()
140142
start := strings.Index(k, "]")
141143
msg := strings.ReplaceAll(k[start+1:]+"\n", "-1", "每")
@@ -147,37 +149,36 @@ func (c *Clock) ListTimers(grpID int64) []string {
147149
}
148150
c.timersmu.RUnlock()
149151
return keys
150-
} else {
151-
return nil
152152
}
153+
return nil
153154
}
154155

156+
// GetTimer 获得定时器
155157
func (c *Clock) GetTimer(key uint32) (t *Timer, ok bool) {
156158
c.timersmu.RLock()
157159
t, ok = (*c.timers)[key]
158160
c.timersmu.RUnlock()
159161
return
160162
}
161163

164+
// AddTimer 添加定时器
162165
func (c *Clock) AddTimer(t *Timer) (err error) {
163166
c.timersmu.Lock()
164-
(*c.timers)[t.Id] = t
167+
(*c.timers)[t.ID] = t
165168
err = c.db.Insert("timer", t)
166169
c.timersmu.Unlock()
167170
return
168171
}
169172

170173
func (c *Clock) loadTimers(db *sql.Sqlite) {
171-
if file.IsExist(db.DBPath) {
172-
c.db = db
173-
err := c.db.Create("timer", &Timer{})
174-
if err == nil {
175-
var t Timer
176-
c.db.FindFor("timer", &t, "", func() error {
177-
tescape := t
178-
go c.RegisterTimer(&tescape, false)
179-
return nil
180-
})
181-
}
174+
c.db = db
175+
err := c.db.Create("timer", &Timer{})
176+
if err == nil {
177+
var t Timer
178+
_ = c.db.FindFor("timer", &t, "", func() error {
179+
tescape := t
180+
go c.RegisterTimer(&tescape, false)
181+
return nil
182+
})
182183
}
183184
}

0 commit comments

Comments
 (0)