✏️ 优化代码结构

This commit is contained in:
fumiama
2021-12-21 22:09:43 +08:00
parent b953385bc5
commit 1d3a61386d
13 changed files with 136 additions and 364 deletions

View File

@@ -2,11 +2,6 @@
package chat
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"io/ioutil"
"math/rand"
"strconv"
"time"
@@ -14,14 +9,13 @@ import (
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
"github.com/FloatTech/ZeroBot-Plugin/control"
)
const (
dbpath = "data/chat/"
dbfile = dbpath + "kimo.json"
dbpath = "data/Chat/"
dbfile = dbpath + "kimoi.json"
prio = 10
)
@@ -31,7 +25,8 @@ var (
DisableOnDefault: false,
Help: "chat\n- [BOT名字]\n- [戳一戳BOT]\n- 空调开\n- 空调关\n- 群温度\n- 设置温度[正整数]\n- mua|啾咪|摸|上你|傻|裸|贴|老婆|抱|亲|一下|咬|操|123|进去|调教|搓|让|捏|挤|略|呐|原味|胖次|内裤|内衣|衣服|ghs|批|憨批|kkp|咕|骚|喜欢|suki|好き|看|不能|砸了|透|口我|草我|自慰|onani|オナニー|炸了|色图|涩图|告白|对不起|回来|吻|软|壁咚|掰开|女友|是|喵|嗷呜|叫|拜|佬|awsl|臭|香|腿|张开|脚|脸|头发|手|pr|舔|小穴|腰|诶嘿嘿|可爱|扭蛋|鼻|眼|色气|推|床|举|手冲|饿|变|敲|爬|怕|冲|射|不穿|迫害|猫粮|揪尾巴|薄荷|早|晚安|揉|榨|掐|胸|奶子|欧派|嫩|蹭|牵手|握手|拍照|w|睡不着|欧尼酱|哥|爱你|过来|自闭|打不过|么么哒|很懂|膝枕|累了|安慰|洗澡|一起睡觉|一起|多大|姐姐|糖|嗦|牛子|🐂子|🐮子|嫌弃|紧|baka|笨蛋|插|插进来|屁股|翘|翘起来|抬|抬起|爸|傲娇|rua|咕噜咕噜|咕噜|上床|做爱|吃掉|吃|揪|种草莓|种草|掀|妹|病娇|嘻",
})
chatList = []string{}
kimomap = make(kimo, 256)
chatList = make([]string, 0, 256)
)
func init() { // 插件主体
@@ -116,29 +111,13 @@ func init() { // 插件主体
))
}
})
initCharList()
engine.OnFullMatchGroup(chatList, zero.OnlyToMe).SetBlock(true).SetPriority(prio).Handle(
func(ctx *zero.Ctx) {
rand.Seed(time.Now().Unix())
kimo, err := ioutil.ReadFile(dbfile)
if err != nil {
log.Println("err:", err)
}
key := ctx.MessageString()
count := gjson.Get(helper.BytesToString(kimo), key+".#").Int()
text := gjson.Get(helper.BytesToString(kimo), fmt.Sprintf("%s.%d", key, rand.Intn(int(count)))).String()
ctx.SendChain(message.At(ctx.Event.UserID), message.Text(text))
})
}
func initCharList() {
kimo, err := ioutil.ReadFile(dbfile)
if err != nil {
log.Println("err:", err)
}
//打开文件
gjson.Get(helper.BytesToString(kimo), "@this").ForEach(func(key, value gjson.Result) bool {
chatList = append(chatList, key.String())
return true
initChatList(func() {
engine.OnFullMatchGroup(chatList, zero.OnlyToMe).SetBlock(true).SetPriority(prio).Handle(
func(ctx *zero.Ctx) {
key := ctx.MessageString()
val := *kimomap[key]
text := val[rand.Intn(len(val))]
ctx.SendChain(message.At(ctx.Event.UserID), message.Text(text))
})
})
}

View File

@@ -1,17 +1,27 @@
package chat
import (
"encoding/json"
"os"
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
"github.com/FloatTech/ZeroBot-Plugin/utils/process"
)
// 加载数据库
func init() {
type kimo = map[string]*[]string
func initChatList(postinit func()) {
go func() {
process.SleepAbout1sTo2s()
_ = os.MkdirAll(dbpath, 0755)
_, _ = file.GetLazyData(dbfile, false, true)
data, err := file.GetLazyData(dbfile, true, true)
if err != nil {
panic(err)
}
json.Unmarshal(data, &kimomap)
for k := range kimomap {
chatList = append(chatList, k)
}
postinit()
}()
}