🎨 🔥 优化代码结构

This commit is contained in:
fumiama
2022-02-15 20:50:49 +08:00
parent 121403231a
commit 4ca5038ae9
52 changed files with 441 additions and 795 deletions

View File

@@ -1,21 +1,12 @@
package jandan
import (
"os"
"sync"
sql "github.com/FloatTech/sqlite"
"github.com/FloatTech/zbputils/file"
"github.com/FloatTech/zbputils/process"
log "github.com/sirupsen/logrus"
"github.com/FloatTech/zbputils/control/order"
)
const dbpath = "data/Jandan/"
const dbfile = dbpath + "pics.db"
var db = &sql.Sqlite{DBPath: dbfile}
var db = &sql.Sqlite{}
var mu sync.RWMutex
type picture struct {
@@ -31,22 +22,3 @@ func getRandomPicture() (u string, err error) {
u = p.URL
return
}
// 加载数据库
func init() {
go func() {
defer order.DoneOnExit()()
process.SleepAbout1sTo2s()
_ = os.MkdirAll(dbpath, 0755)
_, _ = file.GetLazyData(dbfile, false, false)
err := db.Create("picture", &picture{})
if err != nil {
panic(err)
}
n, err := db.Count("picture")
if err != nil {
panic(err)
}
log.Printf("[jandan]读取%d张图片", n)
}()
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/FloatTech/zbputils/binary"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/file"
"github.com/antchfx/htmlquery"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
@@ -25,8 +26,25 @@ func init() {
engine := control.Register("jandan", order.AcquirePrio(), &control.Options{
DisableOnDefault: false,
Help: "煎蛋网无聊图\n- 来份屌图\n- 更新屌图\n",
PublicDataFolder: "Jandan",
})
go func() {
dbpath := engine.DataFolder()
db.DBPath = dbpath + "pics.db"
defer order.DoneOnExit()()
_, _ = file.GetLazyData(db.DBPath, false, false)
err := db.Create("picture", &picture{})
if err != nil {
panic(err)
}
n, err := db.Count("picture")
if err != nil {
panic(err)
}
logrus.Printf("[jandan]读取%d张图片", n)
}()
engine.OnFullMatch("来份屌图").SetBlock(true).
Handle(func(ctx *zero.Ctx) {
u, err := getRandomPicture()