mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-06 15:20:22 +00:00
✨ 插件名称自动注册
This commit is contained in:
112
plugin/sleepmanage/model.go
Normal file
112
plugin/sleepmanage/model.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package sleepmanage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// sdb 睡眠数据库全局变量
|
||||
var sdb *sleepdb
|
||||
|
||||
// sleepdb 睡眠数据库结构体
|
||||
type sleepdb gorm.DB
|
||||
|
||||
// initialize 初始化
|
||||
func initialize(dbpath string) *sleepdb {
|
||||
var err error
|
||||
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
|
||||
// 生成文件
|
||||
f, err := os.Create(dbpath)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer f.Close()
|
||||
}
|
||||
gdb, err := gorm.Open("sqlite3", dbpath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
gdb.AutoMigrate(&SleepManage{})
|
||||
return (*sleepdb)(gdb)
|
||||
}
|
||||
|
||||
// Close 关闭
|
||||
func (sdb *sleepdb) Close() error {
|
||||
db := (*gorm.DB)(sdb)
|
||||
return db.Close()
|
||||
}
|
||||
|
||||
// SleepManage 睡眠信息
|
||||
type SleepManage struct {
|
||||
ID uint `gorm:"primary_key"`
|
||||
GroupID int64 `gorm:"column:group_id"`
|
||||
UserID int64 `gorm:"column:user_id"`
|
||||
SleepTime time.Time `gorm:"column:sleep_time"`
|
||||
}
|
||||
|
||||
// TableName 表名
|
||||
func (SleepManage) TableName() string {
|
||||
return "sleep_manage"
|
||||
}
|
||||
|
||||
// sleep 更新睡眠时间
|
||||
func (sdb *sleepdb) sleep(gid, uid int64) (position int, awakeTime time.Duration) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
now := time.Now()
|
||||
var today time.Time
|
||||
if now.Hour() >= 21 {
|
||||
today = now.Add(-time.Hour*time.Duration(-21+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
|
||||
} else if now.Hour() <= 3 {
|
||||
today = now.Add(-time.Hour*time.Duration(3+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
|
||||
}
|
||||
st := SleepManage{
|
||||
GroupID: gid,
|
||||
UserID: uid,
|
||||
SleepTime: now,
|
||||
}
|
||||
if err := db.Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).First(&st).Error; err != nil {
|
||||
// error handling...
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
db.Model(&SleepManage{}).Create(&st) // newUser not user
|
||||
}
|
||||
} else {
|
||||
log.Debugln("sleeptime为", st)
|
||||
awakeTime = now.Sub(st.SleepTime)
|
||||
db.Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).Update(
|
||||
map[string]any{
|
||||
"sleep_time": now,
|
||||
})
|
||||
}
|
||||
db.Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", gid, now, today).Count(&position)
|
||||
return position, awakeTime
|
||||
}
|
||||
|
||||
// getUp 更新起床时间
|
||||
func (sdb *sleepdb) getUp(gid, uid int64) (position int, sleepTime time.Duration) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
now := time.Now()
|
||||
today := now.Add(-time.Hour*time.Duration(-6+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
|
||||
st := SleepManage{
|
||||
GroupID: gid,
|
||||
UserID: uid,
|
||||
SleepTime: now,
|
||||
}
|
||||
if err := db.Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).First(&st).Error; err != nil {
|
||||
// error handling...
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
db.Model(&SleepManage{}).Create(&st) // newUser not user
|
||||
}
|
||||
} else {
|
||||
log.Debugln("sleeptime为", st)
|
||||
sleepTime = now.Sub(st.SleepTime)
|
||||
db.Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).Update(
|
||||
map[string]any{
|
||||
"sleep_time": now,
|
||||
})
|
||||
}
|
||||
db.Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", gid, now, today).Count(&position)
|
||||
return position, sleepTime
|
||||
}
|
||||
67
plugin/sleepmanage/sleep_manage.go
Normal file
67
plugin/sleepmanage/sleep_manage.go
Normal file
@@ -0,0 +1,67 @@
|
||||
// Package sleepmanage 睡眠管理
|
||||
package sleepmanage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
|
||||
ctrl "github.com/FloatTech/zbpctrl"
|
||||
"github.com/FloatTech/zbputils/control"
|
||||
)
|
||||
|
||||
func init() {
|
||||
engine := control.AutoRegister(&ctrl.Options[*zero.Ctx]{
|
||||
DisableOnDefault: false,
|
||||
Brief: "睡眠小助手",
|
||||
Help: "- 早安\n- 晚安",
|
||||
PrivateDataFolder: "sleep",
|
||||
})
|
||||
go func() {
|
||||
sdb = initialize(engine.DataFolder() + "manage.db")
|
||||
}()
|
||||
engine.OnFullMatch("早安", isMorning, zero.OnlyGroup).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
position, getUpTime := sdb.getUp(ctx.Event.GroupID, ctx.Event.UserID)
|
||||
log.Debugln(position, getUpTime)
|
||||
hour, minute, second := timeDuration(getUpTime)
|
||||
if (hour == 0 && minute == 0 && second == 0) || hour >= 24 {
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(fmt.Sprintf("早安成功!你是今天第%d个起床的", position)))
|
||||
} else {
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(fmt.Sprintf("早安成功!你的睡眠时长为%d时%d分%d秒,你是今天第%d个起床的", hour, minute, second, position)))
|
||||
}
|
||||
})
|
||||
engine.OnFullMatch("晚安", isEvening, zero.OnlyGroup).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
position, sleepTime := sdb.sleep(ctx.Event.GroupID, ctx.Event.UserID)
|
||||
log.Debugln(position, sleepTime)
|
||||
hour, minute, second := timeDuration(sleepTime)
|
||||
if (hour == 0 && minute == 0 && second == 0) || hour >= 24 {
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(fmt.Sprintf("晚安成功!你是今天第%d个睡觉的", position)))
|
||||
} else {
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(fmt.Sprintf("晚安成功!你的清醒时长为%d时%d分%d秒,你是今天第%d个睡觉的", hour, minute, second, position)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func timeDuration(time time.Duration) (hour, minute, second int64) {
|
||||
hour = int64(time) / (1000 * 1000 * 1000 * 60 * 60)
|
||||
minute = (int64(time) - hour*(1000*1000*1000*60*60)) / (1000 * 1000 * 1000 * 60)
|
||||
second = (int64(time) - hour*(1000*1000*1000*60*60) - minute*(1000*1000*1000*60)) / (1000 * 1000 * 1000)
|
||||
return hour, minute, second
|
||||
}
|
||||
|
||||
// 只统计6点到12点的早安
|
||||
func isMorning(*zero.Ctx) bool {
|
||||
now := time.Now().Hour()
|
||||
return now >= 6 && now <= 12
|
||||
}
|
||||
|
||||
// 只统计21点到凌晨3点的晚安
|
||||
func isEvening(*zero.Ctx) bool {
|
||||
now := time.Now().Hour()
|
||||
return now >= 21 || now <= 3
|
||||
}
|
||||
Reference in New Issue
Block a user