Skip to content

Commit

Permalink
群管增加定时器保存功能
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Jun 6, 2021
1 parent 84b0525 commit 42b62f0
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 72 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
data/chat/*
data/SetuTime/cache/*
data/chat
data/SetuTime/cache
data/manager
main.exe
.DS_Store
10 changes: 6 additions & 4 deletions api/pixiv/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http"
"os"
"strings"

"github.com/Yiwen-Chan/ZeroBot-Plugin/api/utils"
)

// urlCache 缓存并返回缓存路径
Expand All @@ -20,9 +22,9 @@ func (this *Illust) PixivPicDown(path string) (savePath string, err error) {
url = strings.ReplaceAll(url, "_p0", "_p0_master1200")
url = strings.ReplaceAll(url, ".png", ".jpg")
// 文件名为url的hash值
savePath = path + Int2Str(pid) + ".jpg"
savePath = path + utils.Int2Str(pid) + ".jpg"
// 文件存在或文件大小大于10kb
if PathExists(savePath) && FileSize(savePath) > 10240 {
if utils.PathExists(savePath) && utils.FileSize(savePath) > 10240 {
return savePath, nil
}

Expand Down Expand Up @@ -80,9 +82,9 @@ func (this *Illust) RmPic(path string) (err error) {
url = strings.ReplaceAll(url, "_p0", "_p0_master1200")
url = strings.ReplaceAll(url, ".png", ".jpg")
// 文件名为url的hash值
savePath := path + Int2Str(pid) + ".jpg"
savePath := path + utils.Int2Str(pid) + ".jpg"
// 文件存在或文件大小大于10kb
if PathExists(savePath) {
if utils.PathExists(savePath) {
return os.Remove(savePath)
} else {
return nil
Expand Down
106 changes: 74 additions & 32 deletions manager/timer.go → api/timer/timer.go
Original file line number Diff line number Diff line change
@@ -1,64 +1,87 @@
package manager
package timer

import (
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"unicode"

tm "github.com/Yiwen-Chan/ZeroBot-Plugin/api/timer"
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/msgext"
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/utils"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)

type TimeStamp = tm.Timer
type (
TimeStamp = Timer
Ctx = zero.Ctx
)

var (
//记录每个定时器以便取消
timersmap tm.TimersMap
timers = timersmap.Timers
timersmap TimersMap
Timers *(map[string]*Timer)
//定时器存储位置
BOTPATH = utils.PathExecute() // 当前bot运行目录
DATAPATH = BOTPATH + "data/manager/" // 数据目录
PBFILE = DATAPATH + "timers.pb"
)

func init() {
utils.CreatePath(DATAPATH)
go func() {
time.Sleep(time.Second)
utils.CreatePath(DATAPATH)
loadTimers()
Timers = &timersmap.Timers
}()
}

func timer(ts TimeStamp, onTimeReached func()) {
key := getTimerInfo(&ts)
fmt.Printf("[群管]注册计时器: %s\n", key)
t, ok := timers[key]
if ok { //避免重复注册定时器
func judgeHM(ts *TimeStamp) {
if ts.Hour < 0 || ts.Hour == int32(time.Now().Hour()) {
if ts.Minute < 0 || ts.Minute == int32(time.Now().Minute()) {
zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {
ctx.Event = new(zero.Event)
ctx.Event.GroupID = int64(ts.Grpid)
if ts.Url == "" {
ctx.SendChain(msgext.AtAll(), message.Text(ts.Alert))
} else {
ctx.SendChain(msgext.AtAll(), message.Text(ts.Alert), msgext.ImageNoCache(ts.Url))
}
return false
})
}
}
}

func RegisterTimer(ts *TimeStamp, save bool) {
key := GetTimerInfo(ts)
t, ok := (*Timers)[key]
if t != ts && ok { //避免重复注册定时器
t.Enable = false
}
timers[key] = &ts
saveTimers()
judgeHM := func() {
if ts.Hour < 0 || ts.Hour == int32(time.Now().Hour()) {
if ts.Minute < 0 || ts.Minute == int32(time.Now().Minute()) {
onTimeReached()
}
}
(*Timers)[key] = ts
if save {
SaveTimers()
}
fmt.Printf("[群管]注册计时器[%t]%s\n", ts.Enable, key)
for ts.Enable {
if ts.Month < 0 || ts.Month == int32(time.Now().Month()) {
if ts.Day < 0 || ts.Day == int32(time.Now().Day()) {
judgeHM()
judgeHM(ts)
} else if ts.Day == 0 {
if ts.Week < 0 || ts.Week == int32(time.Now().Weekday()) {
judgeHM()
judgeHM(ts)
}
}
}
time.Sleep(time.Minute)
}
}

func saveTimers() error {
func SaveTimers() error {
data, err := timersmap.Marshal()
if err != nil {
return err
Expand All @@ -76,13 +99,32 @@ func saveTimers() error {
}
}

func loadTimers() {
if utils.PathExists(PBFILE) {
f, err := os.Open(PBFILE)
if err == nil {
data, err1 := io.ReadAll(f)
if err1 == nil {
if len(data) > 0 {
timersmap.Unmarshal(data)
for _, t := range timersmap.Timers {
go RegisterTimer(t, false)
}
return
}
}
}
}
timersmap.Timers = make(map[string]*Timer)
}

//获得标准化定时字符串
func getTimerInfo(ts *TimeStamp) string {
func GetTimerInfo(ts *TimeStamp) string {
return fmt.Sprintf("%d月%d日%d周%d:%d", ts.Month, ts.Day, ts.Week, ts.Hour, ts.Minute)
}

//获得填充好的ts
func getFilledTimeStamp(dateStrs []string, matchDateOnly bool) TimeStamp {
func GetFilledTimeStamp(dateStrs []string, matchDateOnly bool) *TimeStamp {
monthStr := []rune(dateStrs[1])
dayWeekStr := []rune(dateStrs[2])
hourStr := []rune(dateStrs[3])
Expand All @@ -92,22 +134,22 @@ func getFilledTimeStamp(dateStrs []string, matchDateOnly bool) TimeStamp {
ts.Month = chineseNum2Int(monthStr)
if (ts.Month != -1 && ts.Month <= 0) || ts.Month > 12 { //月份非法
fmt.Println("[群管]月份非法!")
return ts
return &ts
}
lenOfDW := len(dayWeekStr)
if lenOfDW == 4 { //包括末尾的"日"
dayWeekStr = []rune{dayWeekStr[0], dayWeekStr[2]} //去除中间的十
ts.Day = chineseNum2Int(dayWeekStr)
if (ts.Day != -1 && ts.Day <= 0) || ts.Day > 31 { //日期非法
fmt.Println("[群管]日期非法1!")
return ts
return &ts
}
} else if dayWeekStr[lenOfDW-1] == rune('日') { //xx日
dayWeekStr = dayWeekStr[:lenOfDW-1]
ts.Day = chineseNum2Int(dayWeekStr)
if (ts.Day != -1 && ts.Day <= 0) || ts.Day > 31 { //日期非法
fmt.Println("[群管]日期非法2!")
return ts
return &ts
}
} else if dayWeekStr[0] == rune('每') { //每周
ts.Week = -1
Expand All @@ -119,7 +161,7 @@ func getFilledTimeStamp(dateStrs []string, matchDateOnly bool) TimeStamp {
if ts.Week < 0 || ts.Week > 6 { //星期非法
ts.Week = -11
fmt.Println("[群管]星期非法!")
return ts
return &ts
}
}
if len(hourStr) == 3 {
Expand All @@ -128,15 +170,15 @@ func getFilledTimeStamp(dateStrs []string, matchDateOnly bool) TimeStamp {
ts.Hour = chineseNum2Int(hourStr)
if ts.Hour < -1 || ts.Hour > 23 { //小时非法
fmt.Println("[群管]小时非法!")
return ts
return &ts
}
if len(minuteStr) == 3 {
minuteStr = []rune{minuteStr[0], minuteStr[2]} //去除中间的十
}
ts.Minute = chineseNum2Int(minuteStr)
if ts.Minute < -1 || ts.Minute > 59 { //分钟非法
fmt.Println("[群管]分钟非法!")
return ts
return &ts
}
if !matchDateOnly {
urlStr := dateStrs[5]
Expand All @@ -146,13 +188,13 @@ func getFilledTimeStamp(dateStrs []string, matchDateOnly bool) TimeStamp {
if !strings.HasPrefix(ts.Url, "http") {
ts.Url = "illegal"
fmt.Println("[群管]url非法!")
return ts
return &ts
}
}
ts.Alert = dateStrs[6]
ts.Enable = true
}
return ts
return &ts
}

//汉字数字转int,仅支持-10~99,最多两位数,其中"每"解释为-1,"每两"为-2,以此类推
Expand Down
72 changes: 54 additions & 18 deletions api/timer/timer.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/timer/timer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ message Timer {
sint32 week = 6;
sint32 hour = 7;
sint32 minute = 8;
uint64 grpid = 9;
}

message TimersMap {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
CommandPrefix: "/",
SuperUsers: os.Args[1:], // 必须修改,否则无权限
Driver: []zero.Driver{
driver.NewWebSocketClient("127.0.0.1", "6700", ""),
driver.NewWebSocketClient("ws://192.168.8.1:6700/", ""),
},
})
// 帮助
Expand Down
Loading

0 comments on commit 42b62f0

Please sign in to comment.