mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 02:00:24 +00:00
新增解签指令和修复sleep的时间判断 (#86)
This commit is contained in:
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),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user