Skip to content

Commit

Permalink
✏️ 迁移到 utils/sql
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Dec 8, 2021
1 parent a8a3c48 commit 7fc4a64
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 100 deletions.
31 changes: 5 additions & 26 deletions plugin_book_review/book_review.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,27 @@
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{
func init() {
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()
b := getBookReviewByKeyword(ctx.State["regex_matched"].([]string)[1])
ctx.SendChain(message.Text(b.BookReview))
})

engine.OnFullMatch("随机书评").SetBlock(true).
Handle(func(ctx *zero.Ctx) {
db, err := model.Open(dbfile)
if err != nil {
log.Errorln(err)
return
}
br := db.GetRandomBookReview()
br := getRandomBookReview()
ctx.SendChain(message.Text(br.BookReview))
db.Close()
})

}
10 changes: 9 additions & 1 deletion plugin_book_review/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import (

"github.com/FloatTech/ZeroBot-Plugin/utils/file"
"github.com/FloatTech/ZeroBot-Plugin/utils/process"
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
)

const dbpath = "data/BookReview/"
const dbfile = dbpath + "bookreview.db"
const dburl = "https://codechina.csdn.net/anto_july/bookreview/-/raw/master/bookreview.db"

var db = &sql.Sqlite{DBPath: dbfile}

// 加载数据库
func init() {
go func() {
Expand All @@ -34,12 +39,15 @@ func init() {
data, err := io.ReadAll(resp.Body)
if err == nil && len(data) > 0 {
_, _ = f.Write(data)
return
}
panic(err)
}
}
panic(err)
}
err := db.Create("book_review", &book{})
if err != nil {
panic(err)
}
}()
}
16 changes: 16 additions & 0 deletions plugin_book_review/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package plugin_book_review

type book struct {
BookReview string `db:"book_review"`
}

// 暂时随机选择一个书评
func getBookReviewByKeyword(keyword string) (b book) {
db.Find("book_review", &b, "where book_review LIKE %"+keyword+"%")
return
}

func getRandomBookReview() (b book) {
db.Pick("book_review", &b)
return
}
73 changes: 0 additions & 73 deletions plugin_book_review/model/model.go

This file was deleted.

0 comments on commit 7fc4a64

Please sign in to comment.