群管增加取消提醒

This commit is contained in:
fumiama 2021-05-09 11:03:21 +08:00
parent eda51bd549
commit e9da9be361
4 changed files with 76 additions and 38 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
data/chat/* data/chat/*
data/SetuTime/cache/* data/SetuTime/cache/*
main.exe main.exe
.DS_Store

View File

@ -47,6 +47,8 @@
- [x] *退群通知 - [x] *退群通知
- [x] 在[月份]月[日期]日的[小时]点[分钟]分时(用[url])提醒大家[消息] - [x] 在[月份]月[日期]日的[小时]点[分钟]分时(用[url])提醒大家[消息]
- [x] 在[月份]月[每周or周几]的[小时]点[分钟]分时(用[url])提醒大家[消息] - [x] 在[月份]月[每周or周几]的[小时]点[分钟]分时(用[url])提醒大家[消息]
- [x] 取消在[月份]月[日期]日的[小时]点[分钟]分的提醒
- [x] 取消在[月份]月[每周or周几]的[小时]点[分钟]分的提醒
- [ ] 同意入群请求 - [ ] 同意入群请求
- [ ] 同意好友请求 - [ ] 同意好友请求
- [ ] 撤回[@xxx] [xxx] - [ ] 撤回[@xxx] [xxx]

View File

@ -231,45 +231,10 @@ func init() { // 插件主体
return return
}) })
// 定时提醒 // 定时提醒
zero.OnRegex(`^在(.{1,2})月(.{1,3}日|每?周.?)的(.{1,3})点(.{1,3})分时(用.+)?提醒大家(.*)`, zero.AdminPermission).SetBlock(true).SetPriority(40). zero.OnRegex(`^在(.{1,2})月(.{1,3}日|每?周.?)的(.{1,3})点(.{1,3})分时(用.+)?提醒大家(.*)`, zero.SuperUserPermission).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)
monthStr := []rune(dateStrs[1]) ts := getFilledTimeStamp(dateStrs, false)
dayWeekStr := []rune(dateStrs[2])
hourStr := []rune(dateStrs[3])
minuteStr := []rune(dateStrs[4])
urlStr := dateStrs[5]
var ts TimeStamp
ts.month = chineseNum2Int(monthStr)
lenOfDW := len(dayWeekStr)
if lenOfDW == 4 { //包括末尾的"日"
dayWeekStr = []rune{dayWeekStr[0], dayWeekStr[2]} //去除中间的十
ts.day = chineseNum2Int(dayWeekStr)
} else if dayWeekStr[lenOfDW-1] == rune('日') { //xx日
dayWeekStr = dayWeekStr[:lenOfDW-1]
ts.day = chineseNum2Int(dayWeekStr)
} else if dayWeekStr[0] == rune('每') { //每周
ts.week = -1
} else { //周x
ts.week = chineseNum2Int(dayWeekStr[1:])
}
if len(hourStr) == 3 {
hourStr = []rune{hourStr[0], hourStr[2]} //去除中间的十
}
ts.hour = chineseNum2Int(hourStr)
if len(minuteStr) == 3 {
minuteStr = []rune{minuteStr[0], minuteStr[2]} //去除中间的十
}
ts.minute = chineseNum2Int(minuteStr)
if urlStr != "" { //是图片url
ts.url = urlStr[3:] //utf-8下用为3字节
if !strings.HasPrefix(ts.url, "http") {
ctx.Send("图片url非法")
return
}
}
ts.alert = dateStrs[6]
ts.enable = true
go timer(ts, func() { go timer(ts, func() {
if ts.url == "" { if ts.url == "" {
ctx.SendChain(AtAll(), message.Text(ts.alert)) ctx.SendChain(AtAll(), message.Text(ts.alert))
@ -277,6 +242,21 @@ func init() { // 插件主体
ctx.SendChain(AtAll(), message.Text(ts.alert), ImageNoCache(ts.url)) ctx.SendChain(AtAll(), message.Text(ts.alert), ImageNoCache(ts.url))
} }
}) })
ctx.Send("记住了~")
return
})
// 取消定时
zero.OnRegex(`^取消在(.{1,2})月(.{1,3}日|每?周.?)的(.{1,3})点(.{1,3})分的提醒`, zero.SuperUserPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
dateStrs := ctx.State["regex_matched"].([]string)
ts := getFilledTimeStamp(dateStrs, true)
t, ok := timers[getTimerInfo(&ts)]
if ok {
t.enable = false
ctx.Send("取消成功~")
} else {
ctx.Send("没有这个定时器哦~")
}
return return
}) })
// 入群欢迎 // 入群欢迎

View File

@ -3,6 +3,7 @@ package manager
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
"unicode" "unicode"
@ -20,8 +21,13 @@ type TimeStamp struct {
minute int8 minute int8
} }
//记录每个定时器以便取消
var timers = make(map[string]*TimeStamp)
func timer(ts TimeStamp, onTimeReached func()) { func timer(ts TimeStamp, onTimeReached func()) {
fmt.Printf("注册计时器: %d月%d日%d周%d:%d触发\n", ts.month, ts.day, ts.week, ts.hour, ts.minute) key := getTimerInfo(&ts)
fmt.Printf("注册计时器: %s\n", key)
timers[key] = &ts
judgeHM := func() { judgeHM := func() {
if ts.hour < 0 || ts.hour == int8(time.Now().Hour()) { if ts.hour < 0 || ts.hour == int8(time.Now().Hour()) {
if ts.minute < 0 || ts.minute == int8(time.Now().Minute()) { if ts.minute < 0 || ts.minute == int8(time.Now().Minute()) {
@ -43,6 +49,55 @@ func timer(ts TimeStamp, onTimeReached func()) {
} }
} }
//获得标准化定时字符串
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 {
monthStr := []rune(dateStrs[1])
dayWeekStr := []rune(dateStrs[2])
hourStr := []rune(dateStrs[3])
minuteStr := []rune(dateStrs[4])
var ts TimeStamp
ts.month = chineseNum2Int(monthStr)
lenOfDW := len(dayWeekStr)
if lenOfDW == 4 { //包括末尾的"日"
dayWeekStr = []rune{dayWeekStr[0], dayWeekStr[2]} //去除中间的十
ts.day = chineseNum2Int(dayWeekStr)
} else if dayWeekStr[lenOfDW-1] == rune('日') { //xx日
dayWeekStr = dayWeekStr[:lenOfDW-1]
ts.day = chineseNum2Int(dayWeekStr)
} else if dayWeekStr[0] == rune('每') { //每周
ts.week = -1
} else { //周x
ts.week = chineseNum2Int(dayWeekStr[1:])
}
if len(hourStr) == 3 {
hourStr = []rune{hourStr[0], hourStr[2]} //去除中间的十
}
ts.hour = chineseNum2Int(hourStr)
if len(minuteStr) == 3 {
minuteStr = []rune{minuteStr[0], minuteStr[2]} //去除中间的十
}
ts.minute = chineseNum2Int(minuteStr)
if !matchDateOnly {
urlStr := dateStrs[5]
if urlStr != "" { //是图片url
ts.url = urlStr[3:] //utf-8下用为3字节
if !strings.HasPrefix(ts.url, "http") {
ts.url = "illeagal"
return ts
}
}
ts.alert = dateStrs[6]
ts.enable = true
}
return ts
}
//汉字数字转int仅支持-1099最多两位数其中"每"解释为-1"每两"为-2以此类推 //汉字数字转int仅支持-1099最多两位数其中"每"解释为-1"每两"为-2以此类推
func chineseNum2Int(rs []rune) int8 { func chineseNum2Int(rs []rune) int8 {
r := -1 r := -1