mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 13:59:39 +08:00
✏️ 修改摸鱼节日为动态变化
This commit is contained in:
parent
4147f1056a
commit
ae41bdb7f8
64
plugin_moyu/holiday_test.go
Normal file
64
plugin_moyu/holiday_test.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package moyu
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
reg "github.com/fumiama/go-registry"
|
||||||
|
)
|
||||||
|
|
||||||
|
var sr = reg.NewRegedit("reilia.eastasia.azurecontainer.io:32664", "fumiama", "--")
|
||||||
|
|
||||||
|
func TestGetHoliday(t *testing.T) {
|
||||||
|
registry.Connect()
|
||||||
|
holidaymap = make(map[string]*Holiday)
|
||||||
|
h := GetHoliday("元旦")
|
||||||
|
registry.Close()
|
||||||
|
t.Fatal(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetHoliday(t *testing.T) {
|
||||||
|
err := sr.Connect()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = SetHoliday("元旦", 1, 2022, 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = SetHoliday("春节", 7, 2022, 1, 31)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = SetHoliday("清明节", 1, 2022, 4, 3)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = SetHoliday("劳动节", 1, 2022, 4, 30)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = SetHoliday("端午节", 1, 2022, 6, 3)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = SetHoliday("中秋节", 1, 2022, 9, 10)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = SetHoliday("国庆节", 7, 2022, 10, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sr.Close()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetHoliday(name string, dur, year int, month time.Month, day int) error {
|
||||||
|
return sr.Set("holiday/"+name, fmt.Sprintf("%d_%d_%d_%d", dur, year, month, day))
|
||||||
|
}
|
||||||
@ -4,6 +4,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
reg "github.com/fumiama/go-registry"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Holiday 节日
|
// Holiday 节日
|
||||||
@ -18,6 +21,31 @@ func NewHoliday(name string, dur, year int, month time.Month, day int) *Holiday
|
|||||||
return &Holiday{name: name, date: time.Date(year, month, day, 0, 0, 0, 0, time.Local), dur: time.Duration(dur) * time.Hour * 24}
|
return &Holiday{name: name, date: time.Date(year, month, day, 0, 0, 0, 0, time.Local), dur: time.Duration(dur) * time.Hour * 24}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
registry = reg.NewRegReader("reilia.eastasia.azurecontainer.io:32664", "fumiama")
|
||||||
|
holidaymap map[string]*Holiday
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetHoliday 从 reg 服务器获取节日
|
||||||
|
func GetHoliday(name string) *Holiday {
|
||||||
|
var dur, year int
|
||||||
|
var month time.Month
|
||||||
|
var day int
|
||||||
|
h, ok := holidaymap[name]
|
||||||
|
if ok {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
ret, err := registry.Get("holiday/" + name)
|
||||||
|
if err != nil {
|
||||||
|
return NewHoliday(name+err.Error(), 0, 0, 0, 0)
|
||||||
|
}
|
||||||
|
fmt.Sscanf(ret, "%d_%d_%d_%d", &dur, &year, &month, &day)
|
||||||
|
logrus.Debugln("[moyu]获取节日:", name, dur, year, month, day)
|
||||||
|
h = NewHoliday(name, dur, year, month, day)
|
||||||
|
holidaymap[name] = h
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
// 获取两个时间相差
|
// 获取两个时间相差
|
||||||
func (h *Holiday) String() string {
|
func (h *Holiday) String() string {
|
||||||
d := time.Until(h.date)
|
d := time.Until(h.date)
|
||||||
|
|||||||
@ -59,6 +59,13 @@ func init() { // 插件主体
|
|||||||
func sendNotice() {
|
func sendNotice() {
|
||||||
m, ok := control.Lookup("moyu")
|
m, ok := control.Lookup("moyu")
|
||||||
if ok {
|
if ok {
|
||||||
|
if holidaymap == nil {
|
||||||
|
ok = false
|
||||||
|
if registry.Connect() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
holidaymap = make(map[string]*Holiday, 32)
|
||||||
|
}
|
||||||
zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {
|
zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {
|
||||||
for _, g := range ctx.GetGroupList().Array() {
|
for _, g := range ctx.GetGroupList().Array() {
|
||||||
grp := g.Get("group_id").Int()
|
grp := g.Get("group_id").Int()
|
||||||
@ -69,19 +76,19 @@ func sendNotice() {
|
|||||||
message.Text("上午好,摸鱼人!\n工作再累,一定不要忘记摸鱼哦!有事没事起身去茶水间,去厕所,去廊道走走别老在工位上坐着,钱是老板的,但命是自己的。\n"),
|
message.Text("上午好,摸鱼人!\n工作再累,一定不要忘记摸鱼哦!有事没事起身去茶水间,去厕所,去廊道走走别老在工位上坐着,钱是老板的,但命是自己的。\n"),
|
||||||
message.Text(weekend()),
|
message.Text(weekend()),
|
||||||
message.Text("\n"),
|
message.Text("\n"),
|
||||||
message.Text(NewHoliday("元旦", 1, 2022, 1, 1)),
|
message.Text(GetHoliday("元旦")),
|
||||||
message.Text("\n"),
|
message.Text("\n"),
|
||||||
message.Text(NewHoliday("春节", 7, 2022, 1, 31)),
|
message.Text(GetHoliday("春节")),
|
||||||
message.Text("\n"),
|
message.Text("\n"),
|
||||||
message.Text(NewHoliday("清明节", 1, 2022, 4, 3)),
|
message.Text(GetHoliday("清明节")),
|
||||||
message.Text("\n"),
|
message.Text("\n"),
|
||||||
message.Text(NewHoliday("劳动节", 1, 2022, 4, 30)),
|
message.Text(GetHoliday("劳动节")),
|
||||||
message.Text("\n"),
|
message.Text("\n"),
|
||||||
message.Text(NewHoliday("端午节", 1, 2022, 6, 3)),
|
message.Text(GetHoliday("端午节")),
|
||||||
message.Text("\n"),
|
message.Text("\n"),
|
||||||
message.Text(NewHoliday("中秋节", 1, 2022, 9, 10)),
|
message.Text(GetHoliday("中秋节")),
|
||||||
message.Text("\n"),
|
message.Text("\n"),
|
||||||
message.Text(NewHoliday("国庆节", 7, 2022, 10, 1)),
|
message.Text(GetHoliday("国庆节")),
|
||||||
message.Text("\n"),
|
message.Text("\n"),
|
||||||
message.Text("上班是帮老板赚钱,摸鱼是赚老板的钱!最后,祝愿天下所有摸鱼人,都能愉快的渡过每一天…"),
|
message.Text("上班是帮老板赚钱,摸鱼是赚老板的钱!最后,祝愿天下所有摸鱼人,都能愉快的渡过每一天…"),
|
||||||
},
|
},
|
||||||
@ -90,5 +97,8 @@ func sendNotice() {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
if !ok {
|
||||||
|
_ = registry.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user