mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 10:10:25 +00:00
feat:添加舔狗日记和修复睡眠管理的问题 (#152)
* feat:添加舔狗日记和修复睡眠管理的问题 * fix:修lint * fix:修改铅笔小说网址 * fix:修改铅笔小说网址
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
// Package model 睡眠管理数据库
|
||||
package model
|
||||
package sleepmanage
|
||||
|
||||
import (
|
||||
"os"
|
||||
@@ -10,11 +9,14 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// SleepDB 睡眠数据库
|
||||
type SleepDB gorm.DB
|
||||
// sdb 睡眠数据库全局变量
|
||||
var sdb *sleepdb
|
||||
|
||||
// Initialize 初始化
|
||||
func Initialize(dbpath string) *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) {
|
||||
// 生成文件
|
||||
@@ -29,27 +31,18 @@ func Initialize(dbpath string) *SleepDB {
|
||||
panic(err)
|
||||
}
|
||||
gdb.AutoMigrate(&SleepManage{})
|
||||
return (*SleepDB)(gdb)
|
||||
}
|
||||
|
||||
// Open 打开
|
||||
func Open(dbpath string) (*SleepDB, error) {
|
||||
db, err := gorm.Open("sqlite3", dbpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return (*SleepDB)(db), nil
|
||||
return (*sleepdb)(gdb)
|
||||
}
|
||||
|
||||
// Close 关闭
|
||||
func (sdb *SleepDB) Close() error {
|
||||
func (sdb *sleepdb) Close() error {
|
||||
db := (*gorm.DB)(sdb)
|
||||
return db.Close()
|
||||
}
|
||||
|
||||
// SleepManage 睡眠信息
|
||||
type SleepManage struct {
|
||||
gorm.Model
|
||||
ID uint `gorm:"primary_key"`
|
||||
GroupID int64 `gorm:"column:group_id"`
|
||||
UserID int64 `gorm:"column:user_id"`
|
||||
SleepTime time.Time `gorm:"column:sleep_time"`
|
||||
@@ -60,8 +53,8 @@ func (SleepManage) TableName() string {
|
||||
return "sleep_manage"
|
||||
}
|
||||
|
||||
// Sleep 更新睡眠时间
|
||||
func (sdb *SleepDB) Sleep(gid, uid int64) (position int, awakeTime time.Duration) {
|
||||
// sleep 更新睡眠时间
|
||||
func (sdb *sleepdb) sleep(gid, uid int64) (position int, awakeTime time.Duration) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
now := time.Now()
|
||||
var today time.Time
|
||||
@@ -92,8 +85,8 @@ func (sdb *SleepDB) Sleep(gid, uid int64) (position int, awakeTime time.Duration
|
||||
return position, awakeTime
|
||||
}
|
||||
|
||||
// GetUp 更新起床时间
|
||||
func (sdb *SleepDB) GetUp(gid, uid int64) (position int, sleepTime time.Duration) {
|
||||
// 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()))
|
||||
@@ -12,8 +12,6 @@ import (
|
||||
control "github.com/FloatTech/zbputils/control"
|
||||
|
||||
"github.com/FloatTech/zbputils/control/order"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/plugin/sleep_manage/model"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -22,15 +20,13 @@ func init() {
|
||||
Help: "sleepmanage\n- 早安\n- 晚安",
|
||||
PrivateDataFolder: "sleep",
|
||||
})
|
||||
dbfile := engine.DataFolder() + "manage.db"
|
||||
go func() {
|
||||
sdb = initialize(engine.DataFolder() + "manage.db")
|
||||
log.Println("[sleepmanage]加载sleepmanage数据库")
|
||||
}()
|
||||
engine.OnFullMatch("早安", isMorning, zero.OnlyGroup).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
db, err := model.Open(dbfile)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
position, getUpTime := db.GetUp(ctx.Event.GroupID, ctx.Event.UserID)
|
||||
position, getUpTime := sdb.getUp(ctx.Event.GroupID, ctx.Event.UserID)
|
||||
log.Println(position, getUpTime)
|
||||
hour, minute, second := timeDuration(getUpTime)
|
||||
if (hour == 0 && minute == 0 && second == 0) || hour >= 24 {
|
||||
@@ -38,16 +34,10 @@ func init() {
|
||||
} else {
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(fmt.Sprintf("早安成功!你的睡眠时长为%d时%d分%d秒,你是今天第%d个起床的", hour, minute, second, position)))
|
||||
}
|
||||
db.Close()
|
||||
})
|
||||
engine.OnFullMatch("晚安", isEvening, zero.OnlyGroup).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
db, err := model.Open(dbfile)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
position, sleepTime := db.Sleep(ctx.Event.GroupID, ctx.Event.UserID)
|
||||
position, sleepTime := sdb.sleep(ctx.Event.GroupID, ctx.Event.UserID)
|
||||
log.Println(position, sleepTime)
|
||||
hour, minute, second := timeDuration(sleepTime)
|
||||
if (hour == 0 && minute == 0 && second == 0) || hour >= 24 {
|
||||
@@ -55,7 +45,6 @@ func init() {
|
||||
} else {
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(fmt.Sprintf("晚安成功!你的清醒时长为%d时%d分%d秒,你是今天第%d个睡觉的", hour, minute, second, position)))
|
||||
}
|
||||
db.Close()
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user