mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 10:10:25 +00:00
fix:修复sql错误 (#85)
* fix:捕获协程异常 * feat:添加睡眠时间 * feat:添加睡眠管理功能 * fix:修改对标时间 * fix:修复sql错误 * feat:添加书评功能 * fix:调整格式 * fix:去掉recover * feat:更新文档 Co-authored-by: Guohuiyuan <haibaraguo@yeahka.com>
This commit is contained in:
48
plugin_book_review/book_review.go
Normal file
48
plugin_book_review/book_review.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package plugin_book_review
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/control"
|
||||
"github.com/FloatTech/ZeroBot-Plugin/plugin_book_review/model"
|
||||
)
|
||||
|
||||
const dbpath = "data/BookReview/"
|
||||
const dbfile = dbpath + "bookreview.db"
|
||||
|
||||
var (
|
||||
engine = control.Register("bookreview", &control.Options{
|
||||
DisableOnDefault: false,
|
||||
Help: "哀伤雪刃推书记录\n- 书评[xxx]\n- 随机书评",
|
||||
})
|
||||
)
|
||||
|
||||
func init() {
|
||||
engine.OnRegex("^书评(.{1,25})$").SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
db, err := model.Open(dbfile)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
BookReviewList := db.GetBookReviewByKeyword(ctx.State["regex_matched"].([]string)[1])
|
||||
ctx.SendChain(message.Text(BookReviewList.BookReview))
|
||||
db.Close()
|
||||
})
|
||||
|
||||
engine.OnFullMatch("随机书评").SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
db, err := model.Open(dbfile)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
return
|
||||
}
|
||||
br := db.GetRandomBookReview()
|
||||
ctx.SendChain(message.Text(br.BookReview))
|
||||
db.Close()
|
||||
})
|
||||
|
||||
}
|
||||
45
plugin_book_review/data.go
Normal file
45
plugin_book_review/data.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package plugin_book_review
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/process"
|
||||
)
|
||||
|
||||
const dburl = "https://codechina.csdn.net/anto_july/bookreview/-/raw/master/bookreview.db"
|
||||
|
||||
// 加载数据库
|
||||
func init() {
|
||||
go func() {
|
||||
process.SleepAbout1sTo2s()
|
||||
// os.RemoveAll(dbpath)
|
||||
_ = os.MkdirAll(dbpath, 0755)
|
||||
if !file.IsExist(dbfile) { // 如果没有数据库,则从 url 下载
|
||||
f, err := os.Create(dbfile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
resp, err := http.Get(dburl)
|
||||
|
||||
if err == nil {
|
||||
defer resp.Body.Close()
|
||||
if resp.ContentLength > 0 {
|
||||
log.Printf("[bookreview]从镜像下载数据库%d字节...", resp.ContentLength)
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err == nil && len(data) > 0 {
|
||||
_, _ = f.Write(data)
|
||||
return
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
73
plugin_book_review/model/model.go
Normal file
73
plugin_book_review/model/model.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/logoove/sqlite"
|
||||
)
|
||||
|
||||
type BrDB gorm.DB
|
||||
|
||||
func Initialize(dbpath string) *BrDB {
|
||||
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(&BookReview{})
|
||||
return (*BrDB)(gdb)
|
||||
}
|
||||
|
||||
func Open(dbpath string) (*BrDB, error) {
|
||||
db, err := gorm.Open("sqlite3", dbpath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return (*BrDB)(db), nil
|
||||
}
|
||||
}
|
||||
|
||||
type BookReview struct {
|
||||
gorm.Model
|
||||
BookReview string `gorm:"column:book_review"`
|
||||
}
|
||||
|
||||
func (BookReview) TableName() string {
|
||||
return "book_review"
|
||||
}
|
||||
|
||||
// 暂时随机选择一个书评
|
||||
func (brdb *BrDB) GetBookReviewByKeyword(keyword string) (BookReviewList BookReview) {
|
||||
db := (*gorm.DB)(brdb)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
var count int
|
||||
db.Debug().Model(&BookReview{}).Where("book_review LIKE ?", "%"+keyword+"%").Count(&count).Offset(rand.Intn(count)).Take(&BookReviewList)
|
||||
log.Println(BookReviewList)
|
||||
return BookReviewList
|
||||
}
|
||||
|
||||
func (brdb *BrDB) GetRandomBookReview() (bookReview BookReview) {
|
||||
db := (*gorm.DB)(brdb)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
var count int
|
||||
db.Debug().Model(&BookReview{}).Count(&count).Offset(rand.Intn(count)).Take(&bookReview)
|
||||
return bookReview
|
||||
}
|
||||
|
||||
func (brdb *BrDB) Close() error {
|
||||
db := (*gorm.DB)(brdb)
|
||||
return db.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user