mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 10:10:25 +00:00
✏️ 优化代码结构
This commit is contained in:
@@ -3,15 +3,36 @@ package funny
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/process"
|
||||
)
|
||||
|
||||
type joke struct {
|
||||
ID uint32 `db:"id"`
|
||||
Text string `db:"text"`
|
||||
}
|
||||
|
||||
const (
|
||||
dbpath = "data/Funny/"
|
||||
dbfile = dbpath + "jokes.db"
|
||||
)
|
||||
|
||||
// 加载数据库
|
||||
func init() {
|
||||
go func() {
|
||||
process.SleepAbout1sTo2s()
|
||||
_ = os.MkdirAll(dbpath, 0755)
|
||||
_, _ = file.GetLazyData(dbfile, false, true)
|
||||
_, err := file.GetLazyData(dbfile, false, true)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = db.Create("jokes", &joke{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c, _ := db.Count("jokes")
|
||||
logrus.Infoln("[funny]加载", c, "个笑话")
|
||||
}()
|
||||
}
|
||||
|
||||
33
plugin_funny/data_test.go
Normal file
33
plugin_funny/data_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package funny
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/binary"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
||||
"github.com/wdvxdr1123/ZeroBot/utils/helper"
|
||||
)
|
||||
|
||||
func TestFillData(t *testing.T) {
|
||||
data, err := os.ReadFile("laugh.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db := &sql.Sqlite{DBPath: "jokes.db"}
|
||||
err = db.Create("jokes", &joke{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jokes := strings.Split(helper.BytesToString(data), "\n")
|
||||
for _, j := range jokes {
|
||||
s := md5.Sum(helper.StringToBytes(j))
|
||||
db.Insert("jokes", &joke{ID: binary.LittleEndian.Uint32(s[:4]), Text: j})
|
||||
}
|
||||
err = db.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -1,25 +1,16 @@
|
||||
package funny
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/control"
|
||||
log "github.com/sirupsen/logrus"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/extension/rate"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
"github.com/wdvxdr1123/ZeroBot/utils/helper"
|
||||
)
|
||||
|
||||
const (
|
||||
qqReg = `\d+`
|
||||
dbpath = "data/funny/"
|
||||
dbfile = dbpath + "laugh.txt"
|
||||
"github.com/FloatTech/ZeroBot-Plugin/control"
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -29,35 +20,28 @@ var (
|
||||
"- 讲个笑话[@xxx]|讲个笑话[qq号]\n",
|
||||
})
|
||||
limit = rate.NewManager(time.Minute, 20)
|
||||
db = &sql.Sqlite{DBPath: dbfile}
|
||||
)
|
||||
|
||||
func init() {
|
||||
engine.OnPrefix("讲个笑话").SetBlock(true).FirstPriority().Handle(func(ctx *zero.Ctx) {
|
||||
if !limit.Load(ctx.Event.GroupID).Acquire() {
|
||||
ctx.SendChain(message.Text("请稍后重试0x0..."))
|
||||
return
|
||||
}
|
||||
var uid int64
|
||||
var text string
|
||||
reg := regexp.MustCompile(qqReg)
|
||||
// 获取名字
|
||||
name := ctx.State["args"].(string)
|
||||
if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" {
|
||||
uid, _ = strconv.ParseInt(ctx.Event.Message[1].Data["qq"], 10, 64)
|
||||
} else if reg.MatchString(ctx.Event.RawMessage) {
|
||||
result := reg.FindAllString(ctx.Event.RawMessage, -1)
|
||||
uid, _ = strconv.ParseInt(result[0], 10, 64)
|
||||
} else if uid == 0 {
|
||||
uid = ctx.Event.UserID
|
||||
qq, _ := strconv.ParseInt(ctx.Event.Message[1].Data["qq"], 10, 64)
|
||||
name = ctx.GetGroupMemberInfo(ctx.Event.GroupID, qq, false).Get("nickname").Str
|
||||
} else if name == "" {
|
||||
name = ctx.Event.Sender.NickName
|
||||
}
|
||||
si := ctx.GetStrangerInfo(uid, false)
|
||||
nickname := si.Get("nickname").String()
|
||||
laugh, err := ioutil.ReadFile(dbfile)
|
||||
var j joke
|
||||
err := db.Pick("jokes", &j)
|
||||
if err != nil {
|
||||
log.Println("err:", err)
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
return
|
||||
}
|
||||
laughList := strings.Split(helper.BytesToString(laugh), "\n")
|
||||
rand.Seed(time.Now().Unix())
|
||||
text = laughList[rand.Intn(len(laughList))]
|
||||
text = strings.Replace(text, "%name", nickname, -1)
|
||||
ctx.SendChain(message.Text(text))
|
||||
ctx.SendChain(message.Text(strings.ReplaceAll(j.Text, "%name", name)))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user