mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-13 02:30:26 +00:00
🎨 优化目录结构
This commit is contained in:
63
plugin/moyu/holiday_test.go
Normal file
63
plugin/moyu/holiday_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package moyu
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
reg "github.com/fumiama/go-registry"
|
||||
)
|
||||
|
||||
var sr = reg.NewRegedit("reilia.fumiama.top:32664", "fumiama", "--")
|
||||
|
||||
func TestGetHoliday(t *testing.T) {
|
||||
registry.Connect()
|
||||
h := GetHoliday("元旦")
|
||||
registry.Close()
|
||||
t.Fatal(h)
|
||||
}
|
||||
|
||||
func TestSetHoliday(t *testing.T) {
|
||||
err := sr.Connect()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = SetHoliday("元旦", 1, 2023, 1, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = SetHoliday("春节", 7, 2023, 1, 21)
|
||||
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))
|
||||
}
|
||||
59
plugin/moyu/nowork.go
Normal file
59
plugin/moyu/nowork.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package moyu
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
reg "github.com/fumiama/go-registry"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Holiday 节日
|
||||
type Holiday struct {
|
||||
name string
|
||||
date time.Time
|
||||
dur time.Duration
|
||||
}
|
||||
|
||||
// NewHoliday 节日名 天数 年 月 日
|
||||
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}
|
||||
}
|
||||
|
||||
var registry = reg.NewRegReader("reilia.fumiama.top:32664", "fumiama")
|
||||
|
||||
// GetHoliday 从 reg 服务器获取节日
|
||||
func GetHoliday(name string) *Holiday {
|
||||
var dur, year int
|
||||
var month time.Month
|
||||
var day int
|
||||
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)
|
||||
return NewHoliday(name, dur, year, month, day)
|
||||
}
|
||||
|
||||
// String 获取两个时间相差
|
||||
func (h *Holiday) String() string {
|
||||
d := time.Until(h.date)
|
||||
switch {
|
||||
case d >= 0:
|
||||
return "距离" + h.name + "还有: " + strconv.FormatFloat(d.Hours()/24.0, 'f', 2, 64) + "天!"
|
||||
case d+h.dur >= 0:
|
||||
return "好好享受 " + h.name + " 假期吧!"
|
||||
default:
|
||||
return "今年 " + h.name + " 假期已过"
|
||||
}
|
||||
}
|
||||
|
||||
func weekend() string {
|
||||
t := time.Now().Weekday()
|
||||
if t == time.Sunday || t == time.Saturday {
|
||||
return "好好享受周末吧!"
|
||||
}
|
||||
return fmt.Sprintf("距离周末还有:%d天!", 5-t)
|
||||
}
|
||||
69
plugin/moyu/run.go
Normal file
69
plugin/moyu/run.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Package moyu 摸鱼
|
||||
package moyu
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
control "github.com/FloatTech/zbputils/control"
|
||||
"github.com/FloatTech/zbputils/process"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
|
||||
"github.com/FloatTech/zbputils/control/order"
|
||||
)
|
||||
|
||||
func init() { // 插件主体
|
||||
control.Register("moyu", order.AcquirePrio(), &control.Options{
|
||||
DisableOnDefault: true,
|
||||
Help: "moyu\n" +
|
||||
"- /启用 moyu\n" +
|
||||
"- /禁用 moyu",
|
||||
})
|
||||
|
||||
// 定时任务每天10点执行一次
|
||||
_, err := process.CronTab.AddFunc("0 10 * * *", func() { sendNotice() })
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取数据拼接消息链并发送
|
||||
func sendNotice() {
|
||||
m, ok := control.Lookup("moyu")
|
||||
if ok {
|
||||
if registry.Connect() != nil {
|
||||
return
|
||||
}
|
||||
msg := message.Message{
|
||||
message.Text(time.Now().Format("2006-01-02")),
|
||||
message.Text("上午好,摸鱼人!\n工作再累,一定不要忘记摸鱼哦!有事没事起身去茶水间,去厕所,去廊道走走别老在工位上坐着,钱是老板的,但命是自己的。\n"),
|
||||
message.Text(weekend()),
|
||||
message.Text("\n"),
|
||||
message.Text(GetHoliday("元旦")),
|
||||
message.Text("\n"),
|
||||
message.Text(GetHoliday("春节")),
|
||||
message.Text("\n"),
|
||||
message.Text(GetHoliday("清明节")),
|
||||
message.Text("\n"),
|
||||
message.Text(GetHoliday("劳动节")),
|
||||
message.Text("\n"),
|
||||
message.Text(GetHoliday("端午节")),
|
||||
message.Text("\n"),
|
||||
message.Text(GetHoliday("中秋节")),
|
||||
message.Text("\n"),
|
||||
message.Text(GetHoliday("国庆节")),
|
||||
message.Text("\n"),
|
||||
message.Text("上班是帮老板赚钱,摸鱼是赚老板的钱!最后,祝愿天下所有摸鱼人,都能愉快的渡过每一天…"),
|
||||
}
|
||||
_ = registry.Close()
|
||||
zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {
|
||||
for _, g := range ctx.GetGroupList().Array() {
|
||||
grp := g.Get("group_id").Int()
|
||||
if m.IsEnabledIn(grp) {
|
||||
ctx.SendGroupMessage(grp, msg)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user