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:
himawari 2021-12-08 13:12:40 +08:00 committed by GitHub
parent 9fca69302b
commit a8a3c48e6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 188 additions and 18 deletions

View File

@ -152,8 +152,8 @@ zerobot -h -t token -u url [-d|w] [-g 监听地址:端口] qq1 qq2 qq3 ...
- [x] 求签|占卜 - [x] 求签|占卜
- **bilibili** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_bilibili"` - **bilibili** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_bilibili"`
- [x] >vup info [名字|uid] - [x] >vup info [名字|uid]
- [x] >user info [名字|uid] - [x] >user info [名字|uid]
- [x] /开启粉丝日报 - [x] /开启粉丝日报
- **嘉然** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_diana"` - **嘉然** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_diana"`
- [x] 小作文 - [x] 小作文
- [x] 发大病 - [x] 发大病
@ -176,15 +176,15 @@ zerobot -h -t token -u url [-d|w] [-g 监听地址:端口] qq1 qq2 qq3 ...
- 注:不同群添加后不会重叠 - 注:不同群添加后不会重叠
- **minecraft** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_minecraft"` - **minecraft** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_minecraft"`
- [x] /mcstart xxx - [x] /mcstart xxx
- [x] /mcstop xxx - [x] /mcstop xxx
- [x] /mclist servername - [x] /mclist servername
- 注:此功能实现依赖[MCSManager](https://github.com/Suwings/MCSManager)项目对服务器的管理apimc服务器如果没有在该管理平台部署此功能无效 - 注:此功能实现依赖[MCSManager](https://github.com/Suwings/MCSManager)项目对服务器的管理apimc服务器如果没有在该管理平台部署此功能无效
- **炉石** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_hs"` - **炉石** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_hs"`
- [x] 搜卡[xxxx] - [x] 搜卡[xxxx]
- [x] [卡组代码xxx] - [x] [卡组代码xxx]
- 注更多搜卡指令参数https://hs.fbigame.com/misc/searchhelp - 注更多搜卡指令参数https://hs.fbigame.com/misc/searchhelp
- **青云客** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_qingyunke"` - **青云客** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_qingyunke"`
- [x] @Bot 任意文本(任意一句话回复) - [x] @Bot 任意文本(任意一句话回复)
- **关键字搜图** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_image_finder"` - **关键字搜图** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_image_finder"`
- [x] 来张 [xxx] - [x] 来张 [xxx]
- **拼音首字母释义工具** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_nbnhhsh"` - **拼音首字母释义工具** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_nbnhhsh"`
@ -199,6 +199,9 @@ zerobot -h -t token -u url [-d|w] [-g 监听地址:端口] qq1 qq2 qq3 ...
- **vtb语录** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_vtb_quotation"` - **vtb语录** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_vtb_quotation"`
- [x] vtb语录 - [x] vtb语录
- [x] 随机vtb - [x] 随机vtb
- **书评** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_book_review"`
- [x] 书评[关键字]
- [x] 随机书评
- **TODO...** - **TODO...**
## 使用方法 ## 使用方法

23
main.go
View File

@ -28,17 +28,18 @@ import (
// 娱乐类 // 娱乐类
// _ "github.com/FloatTech/ZeroBot-Plugin/plugin_wtf" // 鬼东西 // _ "github.com/FloatTech/ZeroBot-Plugin/plugin_wtf" // 鬼东西
_ "github.com/FloatTech/ZeroBot-Plugin-Gif" // 制图 _ "github.com/FloatTech/ZeroBot-Plugin-Gif" // 制图
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_ai_false" // 服务器监控 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_ai_false" // 服务器监控
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_choose" // 选择困难症帮手 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_book_review" // 哀伤雪刃吧推书记录
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_fortune" // 运势 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_choose" // 选择困难症帮手
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_hs" // 炉石 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_fortune" // 运势
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_minecraft" // MCSManager _ "github.com/FloatTech/ZeroBot-Plugin/plugin_hs" // 炉石
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_moyu" // 摸鱼 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_minecraft" // MCSManager
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_music" // 点歌 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_moyu" // 摸鱼
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_omikuji" // 浅草寺求签 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_music" // 点歌
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_reborn" // 投胎 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_omikuji" // 浅草寺求签
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_shindan" // 测定 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_reborn" // 投胎
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_shindan" // 测定
// b站相关 // b站相关
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_bilibili" // 查询b站用户信息 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_bilibili" // 查询b站用户信息

View 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()
})
}

View 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)
}
}()
}

View 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()
}

View File

@ -413,7 +413,7 @@ func init() { // 插件主体
if ctx.Event.NoticeType == "group_decrease" { if ctx.Event.NoticeType == "group_decrease" {
userid := ctx.Event.UserID userid := ctx.Event.UserID
nickname := ctx.GetStrangerInfo(userid, false).Get("nickname").String() nickname := ctx.GetStrangerInfo(userid, false).Get("nickname").String()
ctx.SendChain(message.Text(nickname, "(", userid, ")", "离开了我们~")) ctx.SendChain(message.Text(nickname, "(", userid, ")", "离开了我们..."))
} }
}) })
// 设置欢迎语 // 设置欢迎语

View File

@ -102,7 +102,7 @@ func (sdb *SleepDB) GetUp(groupId, userId int64) (position int, sleepTime time.D
sleepTime = now.Sub(st.SleepTime) sleepTime = now.Sub(st.SleepTime)
db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).Update( db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).Update(
map[string]interface{}{ map[string]interface{}{
"get_up_time": now, "sleep_time": now,
}) })
} }
db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", groupId, now, today).Count(&position) db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", groupId, now, today).Count(&position)