✏️ 优化 moyu,升级依赖

This commit is contained in:
fumiama
2021-11-22 13:41:55 +08:00
parent c1717c71bd
commit a3fbe5d747
4 changed files with 100 additions and 168 deletions

View File

@@ -1,146 +1,127 @@
package moyu
package NewHoliday
import (
"bytes"
"encoding/json"
"fmt"
"os"
"strconv"
"time"
"github.com/FloatTech/ZeroBot-Plugin/control"
"github.com/robfig/cron"
"github.com/fumiama/cron"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/control"
)
//开启的群
type Kq struct {
GroupID []int64 `json:"群号"` //群号
}
//默认
var MY = Kq{
GroupID: []int64{639865824},
}
func init() { // 插件主体
os.MkdirAll("config/", 0777)
_, err := os.Stat(`config/moyu.json`)
if err == nil {
f, _ := os.Open("config/moyu.json")
defer f.Close()
err := json.NewDecoder(f).Decode(&MY)
if err != nil {
fmt.Print("config.json格式错误! 请检查!\n")
time.Sleep(5 * time.Second)
return
}
} else {
fp, _ := os.Create("config/moyu.json")
defer fp.Close()
data, _ := json.Marshal(MY)
var out bytes.Buffer
json.Indent(&out, data, "", "\t")
out.WriteTo(fp)
}
registerNotice() // 开启提醒
FansDaily() // 开启提醒
engine := control.Register("moyu", &control.Options{
DisableOnDefault: false,
control.Register("moyu", &control.Options{
DisableOnDefault: true,
Help: "moyu\n" +
"- 删除提醒\n" +
"- 添加提醒\n",
})
engine.OnRegex(`^(添加|删除)提醒$`).
SetBlock(true).SetPriority(20).Handle(func(ctx *zero.Ctx) {
fp, _ := os.Create("config/moyu.json")
defer fp.Close()
for i, v := range MY.GroupID {
if v == ctx.Event.GroupID {
MY.GroupID = append(MY.GroupID[:i], MY.GroupID[i+1:]...)
break
"- 添加摸鱼提醒\n" +
"- 删除摸鱼提醒\n",
}).OnFullMatch("删除摸鱼提醒", zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(20).
Handle(func(ctx *zero.Ctx) {
m, ok := control.Lookup("moyu")
if ok {
if m.IsEnabledIn(ctx.Event.GroupID) {
m.Disable(ctx.Event.GroupID)
ctx.Send(message.Text("删除成功!"))
} else {
ctx.Send(message.Text("未启用!"))
}
} else {
ctx.Send(message.Text("找不到该服务!"))
}
}
if ctx.State["regex_matched"].([]string)[1] == "添加" {
MY.GroupID = append(MY.GroupID, ctx.Event.GroupID)
ctx.Send(message.Text("添加成功!"))
} else {
ctx.Send(message.Text("删除成功!"))
}
data, _ := json.Marshal(MY)
var out bytes.Buffer
json.Indent(&out, data, "", "\t")
out.WriteTo(fp)
})
})
zero.OnFullMatch("添加摸鱼提醒", zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(20).
Handle(func(ctx *zero.Ctx) {
m, ok := control.Lookup("moyu")
if ok {
if m.IsEnabledIn(ctx.Event.GroupID) {
ctx.Send(message.Text("已启用!"))
} else {
m.Enable(ctx.Event.GroupID)
ctx.Send(message.Text("添加成功!"))
}
} else {
ctx.Send(message.Text("找不到该服务!"))
}
})
}
// 定时任务每天10点执行一次
func FansDaily() {
func registerNotice() {
c := cron.New()
_ = c.AddFunc("0 0 10 * * ?", func() { fansData() })
c.Start()
_, err := c.AddFunc("0 0 10 * * ?", func() { sendNotice() })
if err != nil {
c.Start()
}
}
// 获取数据拼接消息链并发送
func fansData() {
for _, v := range MY.GroupID {
func sendNotice() {
m, ok := control.Lookup("moyu")
if ok {
zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {
ctx.SendGroupMessage(
v,
[]message.MessageSegment{
message.Text(time.Now().Format("2006-01-02") +
"上午好,摸鱼人!\n工作再累一定不要忘记摸鱼哦有事没事起身去茶水间去厕所去廊道走走别老在工位上坐着钱是老板的,但命是自己的。" +
Zm() + "\n" +
Moyu("元旦", 2022, 1, 1) + "\n" +
Moyu("春节", 2022, 1, 31) + "\n" +
Moyu("清明节", 2022, 4, 3) + "\n" +
Moyu("劳动节", 2022, 4, 30) + "\n" +
Moyu("端午节", 2022, 6, 3) + "\n" +
Moyu("中秋节", 2022, 9, 10) + "\n" +
Moyu("国庆节", 2022, 10, 1) + "\n" +
"\n\n上班是帮老板赚钱摸鱼是赚老板的钱最后祝愿天下所有摸鱼人都能愉快的渡过每一天…",
),
},
)
for _, g := range ctx.GetGroupList().Array() {
grp := g.Get("group_id").Int()
if m.IsEnabledIn(grp) {
ctx.SendGroupMessage(grp,
[]message.MessageSegment{
message.Text(time.Now().Format("2006-01-02")),
message.Text("上午好,摸鱼人!\n工作再累一定不要忘记摸鱼哦有事没事起身去茶水间去厕所去廊道走走别老在工位上坐着钱是老板的,但命是自己的。"),
message.Text(weekend()),
message.Text("\n"),
message.Text(NewHoliday("元旦", 2022, 1, 1)),
message.Text("\n"),
message.Text(NewHoliday("节", 2022, 1, 31)),
message.Text("\n"),
message.Text(NewHoliday("清明节", 2022, 4, 3)),
message.Text("\n"),
message.Text(NewHoliday("劳动节", 2022, 4, 30)),
message.Text("\n"),
message.Text(NewHoliday("端午节", 2022, 6, 3)),
message.Text("\n"),
message.Text(NewHoliday("中秋节", 2022, 9, 10)),
message.Text("\n"),
message.Text(NewHoliday("国庆节", 2022, 10, 1)),
message.Text("\n"),
message.Text("\n\n上班是帮老板赚钱摸鱼是赚老板的钱最后祝愿天下所有摸鱼人都能愉快的渡过每一天…"),
},
)
}
}
return true
})
}
}
type holiday struct {
name string
date time.Time
}
func NewHoliday(name string, year int, month time.Month, day int) holiday {
return holiday{name: name, date: time.Date(year, month, day, 0, 0, 0, 0, time.Local)}
}
// 获取两个时间相差
func Moyu(text string, year int, month time.Month, day int) string {
currentTime := time.Now()
t1 := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
t2 := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, time.Local)
tt := int(t1.Sub(t2).Hours() / 24)
if tt >= 0 {
return "距离" + text + "还有: " + strconv.Itoa(tt) + " 天!"
func (h holiday) String() string {
d := time.Until(h.date)
if d >= 0 {
return "距离" + h.name + "还有: " + d.String()
} else {
return "好好享受 " + text + " 假期吧!"
return "好好享受 " + h.name + " 假期吧!"
}
}
func Zm() string {
t := time.Now().Weekday().String()
switch {
case t == "Sunday":
func weekend() string {
t := time.Now().Weekday()
switch t {
case time.Sunday, time.Saturday:
return "\n好好享受周末吧"
case t == "Monday":
return "\n距离周末还有:4天"
case t == "Tuesday":
return "\n距离周末还有:3天"
case t == "Wednesday":
return "\n距离周末还有:2天"
case t == "Thursday":
return "\n距离周末还有:1天"
case t == "Friday":
return "\n距离周末还有:0天"
default:
return "\n好好享受周末吧"
return fmt.Sprintf("\n距离周末还有:%d天", 5-t)
}
}