新增解签指令和修复sleep的时间判断 (#86)

This commit is contained in:
himawari 2021-12-13 12:29:21 +08:00 committed by GitHub
parent 5645fa0168
commit b1852a1de6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 134 additions and 23 deletions

View File

@ -150,6 +150,7 @@ zerobot -h -t token -u url [-d|w] [-g 监听地址:端口] qq1 qq2 qq3 ...
- [x] 早安|晚安 - [x] 早安|晚安
- **浅草寺求签** `import _ github.com/FloatTech/ZeroBot-Plugin/plugin_omikuji` - **浅草寺求签** `import _ github.com/FloatTech/ZeroBot-Plugin/plugin_omikuji`
- [x] 求签|占卜 - [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]

65
plugin_omikuji/data.go Normal file
View File

@ -0,0 +1,65 @@
package omikuji
import (
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
"io"
"net/http"
"os"
log "github.com/sirupsen/logrus"
"github.com/FloatTech/ZeroBot-Plugin/utils/process"
)
const (
dbpath = "data/omikuji/"
dbfile = dbpath + "signature.db"
dburl = "https://codechina.csdn.net/anto_july/bookreview/-/raw/master/signature.db?inline=false"
)
var db = &sql.Sqlite{DBPath: dbfile}
func init() {
go func() {
defer func() {
if err := recover(); err != nil {
log.Println(err)
}
}()
process.SleepAbout1sTo2s()
_ = 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 {
panic(err)
}
defer resp.Body.Close()
if resp.ContentLength > 0 {
log.Printf("[omikuji]从镜像下载数据库%d字节...", resp.ContentLength)
data, err := io.ReadAll(resp.Body)
if err == nil && len(data) > 0 {
_, _ = f.Write(data)
} else {
panic(err)
}
}
}
err := db.Create("signature", &signature{})
if err != nil {
panic(err)
}
n, err := db.Count("signature")
if err != nil {
panic(err)
}
log.Printf("[signature]读取%d条签文", n)
}()
}

14
plugin_omikuji/model.go Normal file
View File

@ -0,0 +1,14 @@
package omikuji
import "strconv"
type signature struct {
Id uint64 `db:"id"`
Text string `db:"text"`
}
// 返回一个解签
func getSignatureById(id int) (s signature) {
db.Find("signature", &s, "where id = "+strconv.Itoa(id))
return
}

View File

@ -4,31 +4,60 @@ package omikuji
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"strconv"
"time" "time"
"github.com/FloatTech/ZeroBot-Plugin/control"
log "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/control"
) )
const ( const (
bed = "https://codechina.csdn.net/u011570312/senso-ji-omikuji/-/raw/main/%d_%d.jpg" bed = "https://codechina.csdn.net/u011570312/senso-ji-omikuji/-/raw/main/%d_%d.jpg"
) )
func init() { // 插件主体 var (
rand.Seed(time.Now().UnixNano()) engine = control.Register("omikuji", &control.Options{
control.Register("omikuji", &control.Options{
DisableOnDefault: false, DisableOnDefault: false,
Help: "浅草寺求签\n" + Help: "浅草寺求签\n" +
"- 求签|占卜", "- 求签|占卜\n- 解签",
}).OnFullMatchGroup([]string{"求签", "占卜"}).SetPriority(10).SetBlock(true). })
)
func init() { // 插件主体
engine.OnFullMatchGroup([]string{"求签", "占卜"}).SetPriority(10).SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
userId := ctx.Event.UserID
today, err := strconv.ParseInt(time.Now().Format("20060102"), 10, 64)
if err != nil {
log.Errorln("string转化为int64格式有问题:", err)
}
seed := userId + today
rand.Seed(seed)
miku := rand.Intn(100) + 1 miku := rand.Intn(100) + 1
ctx.SendChain( ctx.SendChain(
message.At(ctx.Event.UserID), message.At(userId),
message.Image(fmt.Sprintf(bed, miku, 0)), message.Image(fmt.Sprintf(bed, miku, 0)),
message.Image(fmt.Sprintf(bed, miku, 1)), message.Image(fmt.Sprintf(bed, miku, 1)),
) )
}) })
engine.OnFullMatchGroup([]string{"解签"}).SetPriority(10).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
userId := ctx.Event.UserID
today, err := strconv.ParseInt(time.Now().Format("20060102"), 10, 64)
if err != nil {
log.Errorln("string转化为int64格式有问题:", err)
}
seed := userId + today
rand.Seed(seed)
miku := rand.Intn(100) + 1
s := getSignatureById(miku)
ctx.SendChain(
message.At(userId),
message.Text(s.Text),
)
})
} }

View File

@ -58,8 +58,12 @@ func (SleepManage) TableName() string {
func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.Duration) { func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.Duration) {
db := (*gorm.DB)(sdb) db := (*gorm.DB)(sdb)
now := time.Now() now := time.Now()
var today time.Time
today := now.Add(-time.Hour*time.Duration(3+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second())) if now.Hour() >= 21 {
today = now.Add(-time.Hour*time.Duration(-21+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
} else if now.Hour() <= 3 {
today = now.Add(-time.Hour*time.Duration(3+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
}
st := SleepManage{ st := SleepManage{
GroupId: groupId, GroupId: groupId,
UserId: userId, UserId: userId,

View File

@ -2,10 +2,11 @@ package vtbquotation
import ( import (
"io" "io"
"log"
"net/http" "net/http"
"os" "os"
log "github.com/sirupsen/logrus"
"github.com/FloatTech/ZeroBot-Plugin/utils/file" "github.com/FloatTech/ZeroBot-Plugin/utils/file"
"github.com/FloatTech/ZeroBot-Plugin/utils/process" "github.com/FloatTech/ZeroBot-Plugin/utils/process"
) )

View File

@ -216,13 +216,13 @@ func (vdb *VtbDB) GetVtbList() (uidList []string) {
logrus.Errorln(err) logrus.Errorln(err)
return return
} }
// logrus.Println(string(bytes))
vtbListStr, err := strconv.Unquote(strings.Replace(strconv.Quote(string(bytes)), `\\u`, `\u`, -1)) vtbListStr, err := strconv.Unquote(strings.Replace(strconv.Quote(string(bytes)), `\\u`, `\u`, -1))
if err != nil { if err != nil {
logrus.Errorln(err) logrus.Errorln(err)
return return
} }
// logrus.Println(vtbListStr)
count := gjson.Get(vtbListStr, "#").Int() count := gjson.Get(vtbListStr, "#").Int()
for i := int64(0); i < count; i++ { for i := int64(0); i < count; i++ {
item := gjson.Get(vtbListStr, strconv.FormatInt(i, 10)) item := gjson.Get(vtbListStr, strconv.FormatInt(i, 10))
@ -235,9 +235,9 @@ func (vdb *VtbDB) GetVtbList() (uidList []string) {
FirstCategoryUid: item.Get("uid").String(), FirstCategoryUid: item.Get("uid").String(),
} }
logrus.Println(fc) logrus.Println(fc)
//db.Model(FirstCategory{}).Where("first_category_uid = ?", fc.FirstCategoryUid).FirstOrCreate(&fc)
if err := db.Debug().Model(&FirstCategory{}).Where("first_category_uid = ?", fc.FirstCategoryUid).First(&fc).Error; err != nil { if err := db.Debug().Model(&FirstCategory{}).Where("first_category_uid = ?", fc.FirstCategoryUid).First(&fc).Error; err != nil {
// error handling...
if gorm.IsRecordNotFoundError(err) { if gorm.IsRecordNotFoundError(err) {
db.Debug().Model(&FirstCategory{}).Create(&fc) // newUser not user db.Debug().Model(&FirstCategory{}).Create(&fc) // newUser not user
} }
@ -253,7 +253,6 @@ func (vdb *VtbDB) GetVtbList() (uidList []string) {
uidList = append(uidList, fc.FirstCategoryUid) uidList = append(uidList, fc.FirstCategoryUid)
} }
// logrus.Println(uidList)
return uidList return uidList
} }
@ -280,13 +279,13 @@ func (vdb *VtbDB) StoreVtb(uid string) {
logrus.Errorln(err) logrus.Errorln(err)
return return
} }
//logrus.Println(string(bytes))
vtbStr, err := strconv.Unquote(strings.Replace(strconv.Quote(string(bytes)), `\\u`, `\u`, -1)) vtbStr, err := strconv.Unquote(strings.Replace(strconv.Quote(string(bytes)), `\\u`, `\u`, -1))
if err != nil { if err != nil {
logrus.Errorln(err) logrus.Errorln(err)
return return
} }
// logrus.Println(vtbListStr)
secondCount := gjson.Get(vtbStr, "data.voices.#").Int() secondCount := gjson.Get(vtbStr, "data.voices.#").Int()
logrus.Println("二级品类一共有", secondCount) logrus.Println("二级品类一共有", secondCount)
for secondIndex := int64(0); secondIndex < secondCount; secondIndex++ { for secondIndex := int64(0); secondIndex < secondCount; secondIndex++ {
@ -299,8 +298,7 @@ func (vdb *VtbDB) StoreVtb(uid string) {
SecondCategoryDescription: secondItem.Get("categoryDescription.zh-CN").String(), SecondCategoryDescription: secondItem.Get("categoryDescription.zh-CN").String(),
FirstCategoryUid: uid, FirstCategoryUid: uid,
} }
// logrus.Println(sc)
// db.Model(SecondCategory{}).Where("first_category_uid = ? and second_category_index = ?", uid, secondIndex).FirstOrCreate(&sc)
if err := db.Debug().Model(&SecondCategory{}).Where("first_category_uid = ? and second_category_index = ?", uid, secondIndex).First(&sc).Error; err != nil { if err := db.Debug().Model(&SecondCategory{}).Where("first_category_uid = ? and second_category_index = ?", uid, secondIndex).First(&sc).Error; err != nil {
// error handling... // error handling...
if gorm.IsRecordNotFoundError(err) { if gorm.IsRecordNotFoundError(err) {
@ -329,11 +327,10 @@ func (vdb *VtbDB) StoreVtb(uid string) {
ThirdCategoryAuthor: thirdItem.Get("author").String(), ThirdCategoryAuthor: thirdItem.Get("author").String(),
} }
logrus.Println(tc) logrus.Println(tc)
//db.Model(ThirdCategory{}).Where("first_category_uid = ? and second_category_index = ? and third_category_index = ?",
// uid, secondIndex, thirdIndex).FirstOrCreate(&tc)
if err := db.Debug().Model(&ThirdCategory{}).Where("first_category_uid = ? and second_category_index = ? and third_category_index = ?", if err := db.Debug().Model(&ThirdCategory{}).Where("first_category_uid = ? and second_category_index = ? and third_category_index = ?",
uid, secondIndex, thirdIndex).First(&tc).Error; err != nil { uid, secondIndex, thirdIndex).First(&tc).Error; err != nil {
// error handling...
if gorm.IsRecordNotFoundError(err) { if gorm.IsRecordNotFoundError(err) {
db.Debug().Model(&ThirdCategory{}).Create(&tc) // newUser not user db.Debug().Model(&ThirdCategory{}).Create(&tc) // newUser not user
} }