Skip to content

Commit a363623

Browse files
committed
✏️ drop pb in timer
1 parent b871573 commit a363623

File tree

11 files changed

+258
-121
lines changed

11 files changed

+258
-121
lines changed

plugin_b14/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package b14coder base16384 与 tea 加解密
12
package b14coder
23

34
import (

plugin_diana/data/migrate/text.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package main
22

33
import (
44
"crypto/md5"
5+
"encoding/binary"
56
"fmt"
67
"os"
7-
"unsafe"
88

99
"github.com/RomiChan/protobuf/proto"
1010
"github.com/wdvxdr1123/ZeroBot/utils/helper"
@@ -37,7 +37,7 @@ func main() {
3737
}
3838
for _, d := range compo.Array {
3939
s := md5.Sum(helper.StringToBytes(d))
40-
i := *(*int64)(unsafe.Pointer(&s))
40+
i := int64(binary.LittleEndian.Uint64(s[:8]))
4141
fmt.Printf("[Diana]id: %d\n", i)
4242
err = db.Insert("text", &Text{
4343
Id: i,

plugin_diana/data/text.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package data
33

44
import (
55
"crypto/md5"
6+
"encoding/binary"
67
"os"
7-
"unsafe"
88

99
log "github.com/sirupsen/logrus"
1010
"github.com/wdvxdr1123/ZeroBot/utils/helper"
@@ -56,8 +56,8 @@ func LoadText() error {
5656
// AddText 添加小作文
5757
func AddText(txt string) error {
5858
s := md5.Sum(helper.StringToBytes(txt))
59-
i := *(*int64)(unsafe.Pointer(&s))
60-
return db.Insert("text", &Text{Id: i, Data: txt})
59+
i := binary.LittleEndian.Uint64(s[:8])
60+
return db.Insert("text", &Text{Id: int64(i), Data: txt})
6161
}
6262

6363
// RandText 随机小作文

plugin_manager/manager.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ func init() { // 插件主体
259259
engine.OnRegex(`^在(.{1,2})月(.{1,3}日|每?周.?)的(.{1,3})点(.{1,3})分时(用.+)?提醒大家(.*)`, zero.AdminPermission, zero.OnlyGroup).SetBlock(true).SetPriority(40).
260260
Handle(func(ctx *zero.Ctx) {
261261
dateStrs := ctx.State["regex_matched"].([]string)
262-
ts := timer.GetFilledTimer(dateStrs, ctx.Event.SelfID, false)
262+
ts := timer.GetFilledTimer(dateStrs, ctx.Event.SelfID, ctx.Event.GroupID, false)
263263
if ts.En() {
264-
go clock.RegisterTimer(ts, ctx.Event.GroupID, true)
264+
go clock.RegisterTimer(ts, true)
265265
ctx.SendChain(message.Text("记住了~"))
266266
} else {
267267
ctx.SendChain(message.Text("参数非法:" + ts.Alert))
@@ -283,8 +283,8 @@ func init() { // 插件主体
283283
return
284284
}
285285
logrus.Debugln("[manager] cron:", dateStrs[1])
286-
ts := timer.GetFilledCronTimer(dateStrs[1], alert, url, ctx.Event.SelfID)
287-
if clock.RegisterTimer(ts, ctx.Event.GroupID, true) {
286+
ts := timer.GetFilledCronTimer(dateStrs[1], alert, url, ctx.Event.SelfID, ctx.Event.GroupID)
287+
if clock.RegisterTimer(ts, true) {
288288
ctx.SendChain(message.Text("记住了~"))
289289
} else {
290290
ctx.SendChain(message.Text("参数非法:" + ts.Alert))
@@ -294,8 +294,8 @@ func init() { // 插件主体
294294
engine.OnRegex(`^取消在(.{1,2})月(.{1,3}日|每?周.?)的(.{1,3})点(.{1,3})分的提醒`, zero.AdminPermission, zero.OnlyGroup).SetBlock(true).SetPriority(40).
295295
Handle(func(ctx *zero.Ctx) {
296296
dateStrs := ctx.State["regex_matched"].([]string)
297-
ts := timer.GetFilledTimer(dateStrs, ctx.Event.SelfID, true)
298-
ti := ts.GetTimerInfo(ctx.Event.GroupID)
297+
ts := timer.GetFilledTimer(dateStrs, ctx.Event.SelfID, ctx.Event.GroupID, true)
298+
ti := ts.GetTimerID()
299299
ok := clock.CancelTimer(ti)
300300
if ok {
301301
ctx.SendChain(message.Text("取消成功~"))
@@ -307,8 +307,8 @@ func init() { // 插件主体
307307
engine.OnRegex(`^取消在"(.*)"的提醒`, zero.AdminPermission, zero.OnlyGroup).SetBlock(true).SetPriority(40).
308308
Handle(func(ctx *zero.Ctx) {
309309
dateStrs := ctx.State["regex_matched"].([]string)
310-
ts := timer.Timer{Cron: dateStrs[1]}
311-
ti := ts.GetTimerInfo(ctx.Event.GroupID)
310+
ts := timer.Timer{Cron: dateStrs[1], GrpId: ctx.Event.GroupID}
311+
ti := ts.GetTimerID()
312312
ok := clock.CancelTimer(ti)
313313
if ok {
314314
ctx.SendChain(message.Text("取消成功~"))

plugin_manager/timer/parse.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
11
package timer
22

33
import (
4+
"crypto/md5"
5+
"encoding/binary"
46
"fmt"
57
"strconv"
68
"strings"
79
"time"
810
"unicode"
911

1012
"github.com/sirupsen/logrus"
13+
"github.com/wdvxdr1123/ZeroBot/utils/helper"
1114
)
1215

1316
// GetTimerInfo 获得标准化定时字符串
14-
func (ts *Timer) GetTimerInfo(grp int64) string {
17+
func (ts *Timer) GetTimerInfo() string {
1518
if ts.Cron != "" {
16-
return fmt.Sprintf("[%d]%s", grp, ts.Cron)
19+
return fmt.Sprintf("[%d]%s", ts.GrpId, ts.Cron)
1720
}
18-
return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", grp, 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())
22+
}
23+
24+
// GetTimerInfo 获得标准化 ID
25+
func (ts *Timer) GetTimerID() uint32 {
26+
key := ts.GetTimerInfo()
27+
m := md5.Sum(helper.StringToBytes(key))
28+
return binary.LittleEndian.Uint32(m[:4])
1929
}
2030

2131
// GetFilledCronTimer 获得以cron填充好的ts
22-
func GetFilledCronTimer(croncmd string, alert string, img string, botqq int64) *Timer {
32+
func GetFilledCronTimer(croncmd string, alert string, img string, botqq, gid int64) *Timer {
2333
var ts Timer
2434
ts.Alert = alert
2535
ts.Cron = croncmd
2636
ts.Url = img
2737
ts.Selfid = botqq
38+
ts.GrpId = gid
2839
return &ts
2940
}
3041

3142
// GetFilledTimer 获得填充好的ts
32-
func GetFilledTimer(dateStrs []string, botqq int64, matchDateOnly bool) *Timer {
43+
func GetFilledTimer(dateStrs []string, botqq, grp int64, matchDateOnly bool) *Timer {
3344
monthStr := []rune(dateStrs[1])
3445
dayWeekStr := []rune(dateStrs[2])
3546
hourStr := []rune(dateStrs[3])
@@ -43,25 +54,26 @@ func GetFilledTimer(dateStrs []string, botqq int64, matchDateOnly bool) *Timer {
4354
}
4455
ts.SetMonth(mon)
4556
lenOfDW := len(dayWeekStr)
46-
if lenOfDW == 4 { // 包括末尾的"日"
57+
switch {
58+
case lenOfDW == 4: // 包括末尾的"日"
4759
dayWeekStr = []rune{dayWeekStr[0], dayWeekStr[2]} // 去除中间的十
4860
d := chineseNum2Int(dayWeekStr)
4961
if (d != -1 && d <= 0) || d > 31 { // 日期非法
5062
ts.Alert = "日期非法1!"
5163
return &ts
5264
}
5365
ts.SetDay(d)
54-
} else if dayWeekStr[lenOfDW-1] == rune('日') { // xx日
66+
case dayWeekStr[lenOfDW-1] == rune('日'): // xx日
5567
dayWeekStr = dayWeekStr[:lenOfDW-1]
5668
d := chineseNum2Int(dayWeekStr)
5769
if (d != -1 && d <= 0) || d > 31 { // 日期非法
5870
ts.Alert = "日期非法2!"
5971
return &ts
6072
}
6173
ts.SetDay(d)
62-
} else if dayWeekStr[0] == rune('每') { // 每周
74+
case dayWeekStr[0] == rune('每'): // 每周
6375
ts.SetWeek(-1)
64-
} else { // 周x
76+
default: // 周x
6577
w := chineseNum2Int(dayWeekStr[1:])
6678
if w == 7 { // 周天是0
6779
w = 0
@@ -105,6 +117,7 @@ func GetFilledTimer(dateStrs []string, botqq int64, matchDateOnly bool) *Timer {
105117
ts.SetEn(true)
106118
}
107119
ts.Selfid = botqq
120+
ts.GrpId = grp
108121
return &ts
109122
}
110123

@@ -116,13 +129,14 @@ func chineseNum2Int(rs []rune) int {
116129
if unicode.IsDigit(rs[0]) { // 默认可能存在的第二位也为int
117130
r, _ = strconv.Atoi(string(rs))
118131
} else {
119-
if rs[0] == mai {
132+
switch {
133+
case rs[0] == mai:
120134
if l == 2 {
121135
r = -chineseChar2Int(rs[1])
122136
}
123-
} else if l == 1 {
137+
case l == 1:
124138
r = chineseChar2Int(rs[0])
125-
} else {
139+
default:
126140
ten := chineseChar2Int(rs[0])
127141
if ten != 10 {
128142
ten *= 10

plugin_manager/timer/sleep.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ func (ts *Timer) nextWakeTime() (date time.Time) {
5656
} else {
5757
stable |= 0x8
5858
}
59-
if d < 0 {
59+
switch {
60+
case d < 0:
6061
d = date.Day()
61-
} else if d > 0 {
62+
case d > 0:
6263
stable |= 0x4
63-
} else {
64+
default:
6465
d = date.Day()
6566
if w >= 0 {
6667
stable |= 0x2
@@ -148,14 +149,14 @@ func (ts *Timer) nextWakeTime() (date time.Time) {
148149
return date
149150
}
150151

151-
func (ts *Timer) judgeHM(grp int64) {
152+
func (ts *Timer) judgeHM() {
152153
if ts.Hour() < 0 || ts.Hour() == time.Now().Hour() {
153154
if ts.Minute() < 0 || ts.Minute() == time.Now().Minute() {
154155
if ts.Selfid != 0 {
155-
ts.sendmsg(grp, zero.GetBot(ts.Selfid))
156+
ts.sendmsg(ts.GrpId, zero.GetBot(ts.Selfid))
156157
} else {
157158
zero.RangeBot(func(id int64, ctx *zero.Ctx) (_ bool) {
158-
ts.sendmsg(grp, ctx)
159+
ts.sendmsg(ts.GrpId, ctx)
159160
return
160161
})
161162
}

plugin_manager/timer/timer.db.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package timer
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
7+
)
8+
9+
type Timer struct {
10+
Id uint32 `db:"id"`
11+
En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"`
12+
Selfid int64 `db:"sid"`
13+
GrpId int64 `db:"gid"`
14+
Alert string `db:"alert"`
15+
Cron string `db:"cron"`
16+
Url string `db:"url"`
17+
}
18+
19+
func (t *Timer) InsertInto(db *sql.Sqlite) error {
20+
return db.Insert("timer", t)
21+
}
22+
23+
func getTimerFrom(db *sql.Sqlite, id uint32) (t Timer, err error) {
24+
err = db.Find("timer", &t, "where id = "+strconv.Itoa(int(id)))
25+
return
26+
}

0 commit comments

Comments
 (0)