mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 22:00:11 +08:00
新增解签指令和修复sleep的时间判断 (#86)
This commit is contained in:
parent
5645fa0168
commit
b1852a1de6
@ -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]
|
||||
|
||||
65
plugin_omikuji/data.go
Normal file
65
plugin_omikuji/data.go
Normal 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
14
plugin_omikuji/model.go
Normal 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
|
||||
}
|
||||
@ -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),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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"
|
||||
)
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user