-
Notifications
You must be signed in to change notification settings - Fork 2k
fix:修复sql错误 #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix:修复sql错误 #85
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f28fb95
Merge pull request #1 from FloatTech/master
guohuiyuan e4a6c1d
Merge pull request #2 from FloatTech/master
guohuiyuan fb47540
fix:捕获协程异常
guohuiyuan 1f3a4cf
Merge pull request #3 from FloatTech/master
guohuiyuan bb73024
feat:添加睡眠时间
af3c848
Merge pull request #4 from FloatTech/master
guohuiyuan ac2c450
feat:添加睡眠管理功能
guohuiyuan 9cf2761
fix:修改对标时间
guohuiyuan f25e8a0
Merge pull request #5 from FloatTech/master
guohuiyuan 490aa6a
fix:修复sql错误
guohuiyuan 95e5a0c
feat:添加书评功能
guohuiyuan 5ce856b
Merge remote-tracking branch 'origin/master'
818c9fa
Merge pull request #1 from FloatTech/master
guohuiyuan 73a3453
Merge remote-tracking branch 'origin/master'
b1e1a57
fix:调整格式
45d2ca6
fix:去掉recover
074793e
feat:更新文档
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.