diff --git a/README.md b/README.md index 0eaee60514..2399b09742 100644 --- a/README.md +++ b/README.md @@ -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] diff --git a/plugin_omikuji/data.go b/plugin_omikuji/data.go new file mode 100644 index 0000000000..e54a79cee1 --- /dev/null +++ b/plugin_omikuji/data.go @@ -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) + }() + +} diff --git a/plugin_omikuji/model.go b/plugin_omikuji/model.go new file mode 100644 index 0000000000..1c1b85f93e --- /dev/null +++ b/plugin_omikuji/model.go @@ -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 +} diff --git a/plugin_omikuji/sensou.go b/plugin_omikuji/sensou.go index 7da07449b7..6b7b71f6d3 100644 --- a/plugin_omikuji/sensou.go +++ b/plugin_omikuji/sensou.go @@ -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), + ) + }) } diff --git a/plugin_sleep_manage/model/model.go b/plugin_sleep_manage/model/model.go index 19f9076249..eeaf061eef 100644 --- a/plugin_sleep_manage/model/model.go +++ b/plugin_sleep_manage/model/model.go @@ -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, diff --git a/plugin_vtb_quotation/data.go b/plugin_vtb_quotation/data.go index 1821440a67..cb8fed869d 100644 --- a/plugin_vtb_quotation/data.go +++ b/plugin_vtb_quotation/data.go @@ -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" ) diff --git a/plugin_vtb_quotation/model/model.go b/plugin_vtb_quotation/model/model.go index 9fbead5e64..2e258d743f 100644 --- a/plugin_vtb_quotation/model/model.go +++ b/plugin_vtb_quotation/model/model.go @@ -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)) @@ -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 } @@ -253,7 +253,6 @@ func (vdb *VtbDB) GetVtbList() (uidList []string) { uidList = append(uidList, fc.FirstCategoryUid) } - // logrus.Println(uidList) return uidList } @@ -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++ { @@ -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) { @@ -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 }