Skip to content

Commit

Permalink
规范接口命名&&增加自动删脏数据
Browse files Browse the repository at this point in the history
  • Loading branch information
Cha0sIDL committed Aug 2, 2023
1 parent e601c6e commit 26e15d0
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 35 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ import (

// _ "github.com/FloatTech/ZeroBot-Plugin/plugin/wtf" // 鬼东西
// 其他
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/arknights" // 明日方舟公招tag查询
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/chinesebqb" // 搜表情包
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/arknights" // 明日方舟公招tag查询

//_ "github.com/FloatTech/ZeroBot-Plugin/plugin/chinesebqb" // 搜表情包

_ "github.com/FloatTech/ZeroBot-Plugin/plugin/jx3" // 剑网相关

Expand Down
24 changes: 11 additions & 13 deletions plugin/active/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (

"github.com/FloatTech/ZeroBot-Plugin/config"
"github.com/FloatTech/ZeroBot-Plugin/nlp"
"github.com/FloatTech/ZeroBot-Plugin/plugin/chinesebqb"

"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
zero "github.com/wdvxdr1123/ZeroBot"
Expand Down Expand Up @@ -61,17 +59,17 @@ func init() {
return util.Rand(1, 100) < getActive(ctx) && zero.OnlyGroup(ctx)
}).SetBlock(false).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
if zero.HasPicture(ctx) {
b, err := chinesebqb.Bdb.Pick()
if err != nil {
return
}
ctx.SendChain(message.Image(b.URL))
} else {
msg := ctx.ExtractPlainText()
r := lo.Sample([]aireply.AIReply{aireply.NewXiaoAi(aireply.XiaoAiURL, aireply.XiaoAiBotName), nlp.NewTencent(nlp.BotName)})
ctx.SendChain(message.Text(r.TalkPlain(ctx.Event.UserID, msg, zero.BotConfig.NickName[0])))
}
//if zero.HasPicture(ctx) {
// b, err := chinesebqb.Bdb.Pick()
// if err != nil {
// return
// }
// ctx.SendChain(message.Image(b.URL))
//} else {
msg := ctx.ExtractPlainText()
r := lo.Sample([]aireply.AIReply{aireply.NewXiaoAi(aireply.XiaoAiURL, aireply.XiaoAiBotName), nlp.NewTencent(nlp.BotName)})
ctx.SendChain(message.Text(r.TalkPlain(ctx.Event.UserID, msg, zero.BotConfig.NickName[0])))
//}
})
en.OnPrefix("复读").SetBlock(true).Handle(
func(ctx *zero.Ctx) {
Expand Down
38 changes: 32 additions & 6 deletions plugin/jx3/jx3.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ func init() {
_, err := c.AddFunc("0 5 * * *", func() {
err := updateTalk()
if err != nil {
log.Errorln("updateTalk error", err)
return
}
cleanOldData()

Check failure on line 169 in plugin/jx3/jx3.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
})
c.AddFunc("@every 30s", func() { //nolint:errcheck
controls := jdb.isEnable()
Expand Down Expand Up @@ -659,7 +661,7 @@ func init() {
en.OnRegex(`^(?i)骚话(.*)`).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
var t Jokes
jdb.Pick(&t)
jdb.pick(&t)
ctx.SendChain(message.Text(t.Talk))
})
en.OnFullMatch("/roll").SetBlock(true).
Expand Down Expand Up @@ -824,7 +826,7 @@ func daily(ctx *zero.Ctx, server string) {
msg += "--------------------------------\n"
msg += "数据来源JXBOX和推栏"
ctx.SendChain(message.Text(msg))
jdb.Insert(&Daily{ //nolint:errcheck
jdb.insert(&Daily{ //nolint:errcheck
Server: server,
DailyTask: msg,
Time: carbon.CreateFromTime(7, 0, 0).Timestamp(),
Expand Down Expand Up @@ -1160,7 +1162,7 @@ func updateTalk() error {
return err
}
for _, talkData := range gjson.Get(jsonData, "data.list").Array() {
jdb.Insert(&Jokes{ //nolint:errcheck
jdb.insert(&Jokes{ //nolint:errcheck
ID: talkData.Get("id").Int(),
Talk: talkData.Get("content").String(),
})
Expand All @@ -1173,6 +1175,30 @@ func updateTalk() error {
}
}

func cleanOldData() error {
controls := make([]jxControl, 0)
err := jdb.findAll(&controls)
if err != nil {
return err
}
zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {
grps := lo.Associate(ctx.GetGroupList().Array(), func(item gjson.Result) (int64, string) {
return item.Get("group_id").Int(), ""
})
for _, c := range controls {
if _, ok := grps[c.GroupID]; !ok {
log.Errorln("delete grp data", c)
//err := jdb.delete("gid = ?", &jxControl{}, c.GroupID)
if err != nil {
log.Errorln("delete grp err", err)
}
}
}
return true
})
return nil
}

func priceData2line(price map[string][]map[string]interface{}, datapath string) string {
var tmp []map[string]interface{}
for _, val := range price {
Expand Down Expand Up @@ -1317,7 +1343,7 @@ func checkServer(ctx *zero.Ctx, grpList []GroupList) {

func news(ctx *zero.Ctx, grpList []GroupList) {
msg := make([]News, 0, 5)
count, _ := jdb.Count(&News{})
count, _ := jdb.count(&News{})
doc, _ := htmlquery.LoadURL("https://jx3.xoyo.com/allnews/")
li := htmlquery.Find(doc, "/html/body/div[5]/div/div/div[2]/div/div[3]/div[2]/div/div/ul/li")
for _, node := range li {
Expand All @@ -1329,7 +1355,7 @@ func news(ctx *zero.Ctx, grpList []GroupList) {
if !strings.Contains(href, "https://jx3.xoyo.com") {
href = "https://jx3.xoyo.com" + href
}
canFind := jdb.CanFind("id = ?", &News{}, href)
canFind := jdb.canFind("id = ?", &News{}, href)
data := News{
ID: href,
Date: date,
Expand All @@ -1339,7 +1365,7 @@ func news(ctx *zero.Ctx, grpList []GroupList) {
if canFind {
continue
}
err := jdb.Insert(&data)
err := jdb.insert(&data)
if err != nil {
continue
}
Expand Down
7 changes: 6 additions & 1 deletion plugin/jx3/jx3api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ func (ws *wsClient) listen() {
if t == websocket.TextMessage {
rsp := gjson.Parse(helper.BytesToString(payload))
log.Println("收到JXApi推送", helper.BytesToString(payload))
sendNotice(rsp)
go sendNotice(rsp)
}
}
}

func sendNotice(payload gjson.Result) {
var rsp []message.MessageSegment
now := time.Now().Hour()
if now >= 0 && now < 6 { //十二点之后不响应
return
}
zero.RangeBot(func(id int64, ctx *zero.Ctx) bool {
controls := jdb.isEnable()
log.Println("sendNotice controls ", controls, "data", payload.Get("data.server"))
Expand All @@ -66,6 +70,7 @@ func sendNotice(payload gjson.Result) {
if server, ok := controls[grp]; ok {
switch payload.Get("action").Int() {

Check failure on line 71 in plugin/jx3/jx3api.go

View workflow job for this annotation

GitHub Actions / lint

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case 2004:
log.Println("sendNotice grp ", controls[grp], "data", payload.Get("data.server").String(), "grp", grp)
if server == payload.Get("data.server").String() || payload.Get("data.server").String() == "-" {
rsp =
[]message.MessageSegment{
Expand Down
22 changes: 16 additions & 6 deletions plugin/jx3/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (jdb *jx3db) getAdventure(name string) Adventure {
}

func (jdb *jx3db) updateAdventure(data *Adventure) {
jdb.Insert(data) //nolint:errcheck
jdb.insert(data) //nolint:errcheck
}

func (jdb *jx3db) findDaily(server string) (daily Daily) {
Expand All @@ -218,29 +218,39 @@ func (jdb *jx3db) findDaily(server string) (daily Daily) {
return
}

func (jdb *jx3db) Pick(out interface{}) (data interface{}) {
func (jdb *jx3db) pick(out interface{}) (data interface{}) {
db := (*gorm.DB)(jdb)
db.Order("random()").Take(&out)
return out
}

func (jdb *jx3db) Insert(value interface{}) error {
func (jdb *jx3db) insert(value interface{}) error {
db := (*gorm.DB)(jdb)
return db.Clauses(clause.OnConflict{UpdateAll: true}).Create(value).Error
}

func (jdb *jx3db) Find(query, out interface{}, args ...interface{}) error {
func (jdb *jx3db) find(query, out interface{}, args ...interface{}) error {
db := (*gorm.DB)(jdb)
return db.Where(query, args...).First(out).Error
}

func (jdb *jx3db) Count(value interface{}) (num int64, err error) {
func (jdb *jx3db) findAll(query interface{}, args ...interface{}) error {
db := (*gorm.DB)(jdb)
return db.Find(query, args...).Error
}

func (jdb *jx3db) delete(query, value interface{}, args ...interface{}) error {

Check failure on line 242 in plugin/jx3/model.go

View workflow job for this annotation

GitHub Actions / lint

func `(*jx3db).delete` is unused (unused)
db := (*gorm.DB)(jdb)
return db.Where(query, args).Delete(value).Error
}

func (jdb *jx3db) count(value interface{}) (num int64, err error) {
db := (*gorm.DB)(jdb)
err = db.Model(value).Count(&num).Error
return
}

func (jdb *jx3db) CanFind(query, out interface{}, args ...interface{}) bool {
func (jdb *jx3db) canFind(query, out interface{}, args ...interface{}) bool {
db := (*gorm.DB)(jdb)
err := db.Where(query, args...).First(out).Error
return !errors.Is(err, gorm.ErrRecordNotFound)
Expand Down
6 changes: 3 additions & 3 deletions plugin/jx3/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func init() {
en.OnFullMatchGroup([]string{"我报的团", "我的报名"}, zero.OnlyGroup).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
var sTeam []Team
err := jdb.Find("groupId = ?", &sTeam, ctx.Event.GroupID)
err := jdb.find("groupId = ?", &sTeam, ctx.Event.GroupID)
if err != nil {
ctx.SendChain(message.Text("Err:", err))
}
Expand All @@ -129,7 +129,7 @@ func init() {
en.OnFullMatchGroup([]string{"我的开团"}, zero.OnlyGroup).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
var sTeam []Team
err := jdb.Find("leaderId = ? and groupId = ?", &sTeam, ctx.Event.UserID, ctx.Event.GroupID)
err := jdb.find("leaderId = ? and groupId = ?", &sTeam, ctx.Event.UserID, ctx.Event.GroupID)
if err != nil {
ctx.SendChain(message.Text("Err:", err))
return
Expand All @@ -144,7 +144,7 @@ func init() {
en.OnFullMatchGroup([]string{"查看全部团队"}, zero.OnlyGroup).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
var sTeam []Team
err := jdb.Find("groupId = ?", &sTeam, ctx.Event.GroupID)
err := jdb.find("groupId = ?", &sTeam, ctx.Event.GroupID)
if err != nil {
ctx.SendChain(message.Text("Err:", err))
return
Expand Down
4 changes: 2 additions & 2 deletions plugin/jx3/tuilan.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func attributes(ctx *zero.Ctx, datapath string) {
var user User
zone := normServer[1]
server = normServer[0]
err := jdb.Find("id = ?", &user, name+"_"+chatServer[server])
err := jdb.find("id = ?", &user, name+"_"+chatServer[server])
if err != nil {
ctx.SendChain(message.Text("没有查找到这个角色呢,试着在世界频道说句话试试吧~"))
return
Expand Down Expand Up @@ -131,7 +131,7 @@ func indicator(ctx *zero.Ctx, datapath string) {
zone := normServer[1]
server = normServer[0]
var user User
err := jdb.Find("id = ?", &user, name+"_"+chatServer[server])
err := jdb.find("id = ?", &user, name+"_"+chatServer[server])
gameRoleID := gjson.Parse(user.Data).Get("body.msg.0.sRoleId").String()
if err != nil {
ctx.SendChain(message.Text("没有查找到这个角色呢,试着在世界频道说句话试试吧~"))
Expand Down
2 changes: 1 addition & 1 deletion plugin/jx3/userWs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (ws *userWsClient) listen() {
continue
}
rw.Lock()
jdb.Insert(&User{ //nolint:errcheck
jdb.insert(&User{ //nolint:errcheck
ID: roleName + "_" + server,
Data: binutils.BytesToString(payload),
})
Expand Down
2 changes: 1 addition & 1 deletion plugin/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ func init() { // 插件主体
ctx.SendChain(message.Text("取消失败, 信息: ", rsp.Msg, "解释: ", rsp.Wording))
}
})
engine.OnFullMatchGroup([]string{"菜单", "使用手册", "使用说明"}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
engine.OnFullMatchGroup([]string{"菜单", "使用手册", "使用说明", "help"}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
ctx.SendChain(message.Text("https://www.yuque.com/docs/share/34aee3c7-defc-4f29-b45a-1c7f8f4ab535?# 《ZeroBot使用手册》"))
})
engine.OnFullMatch("谁在窥屏", zero.OnlyGroup, zero.AdminPermission).SetBlock(true).Handle(func(ctx *zero.Ctx) {
Expand Down

0 comments on commit 26e15d0

Please sign in to comment.