Skip to content

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

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 24 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ zerobot -h -t token -u url [-d|w] [-g 监听地址:端口] qq1 qq2 qq3 ...
- [x] 早安|晚安
- **浅草寺求签** `import _ github.com/FloatTech/ZeroBot-Plugin/plugin_omikuji`
- [x] 求签|占卜
- [x] 解签
- **bilibili** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_bilibili"`
- [x] >vup info [名字|uid]
- [x] >user info [名字|uid]
Expand Down
65 changes: 65 additions & 0 deletions plugin_omikuji/data.go
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions plugin_omikuji/model.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 37 additions & 8 deletions plugin_omikuji/sensou.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,60 @@ package omikuji
import (
"fmt"
"math/rand"
"strconv"
"time"

"github.com/FloatTech/ZeroBot-Plugin/control"
log "github.com/sirupsen/logrus"

zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"

"github.com/FloatTech/ZeroBot-Plugin/control"
)

const (
bed = "https://codechina.csdn.net/u011570312/senso-ji-omikuji/-/raw/main/%d_%d.jpg"
)

func init() { // 插件主体
rand.Seed(time.Now().UnixNano())
control.Register("omikuji", &control.Options{
var (
engine = control.Register("omikuji", &control.Options{
DisableOnDefault: false,
Help: "浅草寺求签\n" +
"- 求签|占卜",
}).OnFullMatchGroup([]string{"求签", "占卜"}).SetPriority(10).SetBlock(true).
"- 求签|占卜\n- 解签",
})
)

func init() { // 插件主体

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
ctx.SendChain(
message.At(ctx.Event.UserID),
message.At(userId),
message.Image(fmt.Sprintf(bed, miku, 0)),
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),
)
})
}
8 changes: 6 additions & 2 deletions plugin_sleep_manage/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ func (SleepManage) TableName() string {
func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.Duration) {
db := (*gorm.DB)(sdb)
now := time.Now()

today := now.Add(-time.Hour*time.Duration(3+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
var today time.Time
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{
GroupId: groupId,
UserId: userId,
Expand Down
3 changes: 2 additions & 1 deletion plugin_vtb_quotation/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package vtbquotation

import (
"io"
"log"
"net/http"
"os"

log "github.com/sirupsen/logrus"

"github.com/FloatTech/ZeroBot-Plugin/utils/file"
"github.com/FloatTech/ZeroBot-Plugin/utils/process"
)
Expand Down
21 changes: 9 additions & 12 deletions plugin_vtb_quotation/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ func (vdb *VtbDB) GetVtbList() (uidList []string) {
logrus.Errorln(err)
return
}
// logrus.Println(string(bytes))

vtbListStr, err := strconv.Unquote(strings.Replace(strconv.Quote(string(bytes)), `\\u`, `\u`, -1))
if err != nil {
logrus.Errorln(err)
return
}
// logrus.Println(vtbListStr)

count := gjson.Get(vtbListStr, "#").Int()
for i := int64(0); i < count; i++ {
item := gjson.Get(vtbListStr, strconv.FormatInt(i, 10))
Expand All @@ -235,9 +235,9 @@ func (vdb *VtbDB) GetVtbList() (uidList []string) {
FirstCategoryUid: item.Get("uid").String(),
}
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 {
// error handling...

if gorm.IsRecordNotFoundError(err) {
db.Debug().Model(&FirstCategory{}).Create(&fc) // newUser not user
}
Expand All @@ -253,7 +253,6 @@ func (vdb *VtbDB) GetVtbList() (uidList []string) {
uidList = append(uidList, fc.FirstCategoryUid)
}

// logrus.Println(uidList)
return uidList
}

Expand All @@ -280,13 +279,13 @@ func (vdb *VtbDB) StoreVtb(uid string) {
logrus.Errorln(err)
return
}
//logrus.Println(string(bytes))

vtbStr, err := strconv.Unquote(strings.Replace(strconv.Quote(string(bytes)), `\\u`, `\u`, -1))
if err != nil {
logrus.Errorln(err)
return
}
// logrus.Println(vtbListStr)

secondCount := gjson.Get(vtbStr, "data.voices.#").Int()
logrus.Println("二级品类一共有", secondCount)
for secondIndex := int64(0); secondIndex < secondCount; secondIndex++ {
Expand All @@ -299,8 +298,7 @@ func (vdb *VtbDB) StoreVtb(uid string) {
SecondCategoryDescription: secondItem.Get("categoryDescription.zh-CN").String(),
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 {
// error handling...
if gorm.IsRecordNotFoundError(err) {
Expand Down Expand Up @@ -329,11 +327,10 @@ func (vdb *VtbDB) StoreVtb(uid string) {
ThirdCategoryAuthor: thirdItem.Get("author").String(),
}
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 = ?",
uid, secondIndex, thirdIndex).First(&tc).Error; err != nil {
// error handling...

if gorm.IsRecordNotFoundError(err) {
db.Debug().Model(&ThirdCategory{}).Create(&tc) // newUser not user
}
Expand Down