From 4ca5038ae932fde190060cf99f76c97f7ec31ccc Mon Sep 17 00:00:00 2001 From: fumiama Date: Tue, 15 Feb 2022 20:50:49 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20=F0=9F=8E=A8=20?= =?UTF-8?q?=F0=9F=94=A5=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 +- go.sum | 8 ++-- plugin_bilibili_push/bilibili_push.go | 40 ++++++++++++++----- plugin_bilibili_push/data.go | 32 ---------------- plugin_book_review/book_review.go | 28 +++++++++++--- plugin_book_review/data.go | 38 ------------------ plugin_book_review/model.go | 4 ++ plugin_chouxianghua/chouxianghua.go | 28 ++++++++++++-- plugin_chouxianghua/data.go | 40 ------------------- plugin_chouxianghua/model.go | 4 ++ plugin_cpstory/cpstory.go | 21 ++++++++++ plugin_cpstory/data.go | 40 ------------------- plugin_cpstory/model.go | 4 ++ plugin_curse/curse.go | 19 +++++++++ plugin_curse/data.go | 41 -------------------- plugin_curse/model.go | 4 ++ plugin_danbooru/main.go | 14 +++---- plugin_diana/bing.go | 8 ++++ plugin_diana/data/text.go | 53 ++++++++------------------ plugin_drift_bottle/data.go | 1 + plugin_drift_bottle/db.go | 23 ----------- plugin_drift_bottle/main.go | 11 +++++- plugin_fortune/fortune.go | 55 +++++++++++++-------------- plugin_funny/data.go | 41 -------------------- plugin_funny/data_test.go | 33 ---------------- plugin_funny/laugh.go | 32 ++++++++++++++-- plugin_gif/run.go | 17 +++++---- plugin_hs/run.go | 15 ++------ plugin_jandan/data.go | 30 +-------------- plugin_jandan/jandan.go | 18 +++++++++ plugin_manager/manager.go | 15 +++----- plugin_nativesetu/data.go | 24 +----------- plugin_nativesetu/main.go | 19 ++++++--- plugin_nativewife/main.go | 21 ++++------ plugin_omikuji/data.go | 38 ------------------ plugin_omikuji/model.go | 8 +++- plugin_omikuji/sensou.go | 19 +++++++++ plugin_reborn/born.go | 6 +++ plugin_reborn/load.go | 49 +----------------------- plugin_reborn/main.go | 30 +++++++++++++-- plugin_scale/main.go | 13 ++----- plugin_score/data.go | 43 --------------------- plugin_score/model.go | 3 ++ plugin_score/sign_in.go | 24 +++++++++++- plugin_setutime/setu_geter.go | 23 ++++++----- plugin_sleep_manage/data.go | 20 ---------- plugin_sleep_manage/sleep_manage.go | 9 ++--- plugin_thesaurus/chat.go | 41 ++++++++++++-------- plugin_thesaurus/data.go | 36 ------------------ plugin_vtb_quotation/cron.go | 45 ---------------------- plugin_vtb_quotation/data.go | 20 ---------- plugin_vtb_quotation/vtb_quotation.go | 24 +++++++++--- 52 files changed, 441 insertions(+), 795 deletions(-) delete mode 100644 plugin_bilibili_push/data.go delete mode 100644 plugin_book_review/data.go delete mode 100644 plugin_chouxianghua/data.go delete mode 100644 plugin_cpstory/data.go delete mode 100644 plugin_curse/data.go delete mode 100644 plugin_drift_bottle/db.go delete mode 100644 plugin_funny/data.go delete mode 100644 plugin_funny/data_test.go delete mode 100644 plugin_omikuji/data.go delete mode 100644 plugin_score/data.go delete mode 100644 plugin_sleep_manage/data.go delete mode 100644 plugin_thesaurus/data.go delete mode 100644 plugin_vtb_quotation/cron.go delete mode 100644 plugin_vtb_quotation/data.go diff --git a/go.mod b/go.mod index 297faaab..5debe50c 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/FloatTech/ZeroBot-Plugin go 1.17 require ( - github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220215081026-d6f95aaf77df + github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220215114239-bcb083dfeb08 github.com/FloatTech/sqlite v0.1.0 - github.com/FloatTech/zbputils v1.3.0-beta8.0.20220215053220-192931cec3a8 + github.com/FloatTech/zbputils v1.3.0-beta8.0.20220215114138-e858f7fbf9f7 github.com/antchfx/htmlquery v1.2.4 github.com/corona10/goimagehash v1.0.3 github.com/fogleman/gg v1.3.0 diff --git a/go.sum b/go.sum index f9558aac..69c8f40e 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,10 @@ -github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220215081026-d6f95aaf77df h1:HLbcSbk4ReH+f6C+jGur32CP3NqWqGDCUAvcITOFSXU= -github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220215081026-d6f95aaf77df/go.mod h1:Kl18EWeAbw1xxqq/2KgS1pT3PmJv+9vWBeD3TNdIDvE= +github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220215114239-bcb083dfeb08 h1:UcvmuO3WO4ecqpciM0Cr8nxXDQjH66klPMFQNfdr9bk= +github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220215114239-bcb083dfeb08/go.mod h1:HH4BkFvRhMlRG6etlFhilf+Di3Xcejke38VKSZSlaNQ= github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ= github.com/FloatTech/sqlite v0.1.0 h1:Geh6Z+DWZXmyXVOhM0U2oXT7b1Ob6sQtuyjInk+MCUk= github.com/FloatTech/sqlite v0.1.0/go.mod h1:XS0oeSqtH6lFzpPRiDE9fV6+zMcuUF5/6o+tQVO1QZE= -github.com/FloatTech/zbputils v1.3.0-beta8.0.20220215053220-192931cec3a8 h1:BLuZxgk5HcqNmxbieAurWAxbVQfUYwMo+jq6a0IA6tU= -github.com/FloatTech/zbputils v1.3.0-beta8.0.20220215053220-192931cec3a8/go.mod h1:s4RcgaO+X7y/ZkKcRt0b7Dg/AsnMFeS1kVylKGUiMhk= +github.com/FloatTech/zbputils v1.3.0-beta8.0.20220215114138-e858f7fbf9f7 h1:c+nr+Lbpc5kZgFi0WPY3yJxO6gpbvRbZEc4v6eVkQos= +github.com/FloatTech/zbputils v1.3.0-beta8.0.20220215114138-e858f7fbf9f7/go.mod h1:s4RcgaO+X7y/ZkKcRt0b7Dg/AsnMFeS1kVylKGUiMhk= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0= github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc/go.mod h1:OMmITAib6POA37xCichWM0aRnoVpSMZO1rB/G01wrr0= diff --git a/plugin_bilibili_push/bilibili_push.go b/plugin_bilibili_push/bilibili_push.go index e06355fc..878f6639 100644 --- a/plugin_bilibili_push/bilibili_push.go +++ b/plugin_bilibili_push/bilibili_push.go @@ -7,20 +7,21 @@ import ( "fmt" "io" "net/http" + "os" "strconv" "time" - "github.com/FloatTech/zbputils/control" - "github.com/FloatTech/zbputils/ctxext" - "github.com/FloatTech/zbputils/img/text" - "github.com/FloatTech/zbputils/web" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" - "github.com/wdvxdr1123/ZeroBot/utils/helper" + "github.com/FloatTech/zbputils/binary" + "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/control/order" + "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/img/text" + "github.com/FloatTech/zbputils/web" ) const ( @@ -34,6 +35,9 @@ const ( serviceName = "bilibilipush" ) +// bdb bilibili推送数据库 +var bdb *bilibilipushdb + var ( lastTime = map[int64]int64{} typeMsg = map[int64]string{ @@ -65,8 +69,24 @@ func init() { "- 取消动态订阅[uid]\n" + "- 取消直播订阅[uid]\n" + "- 推送列表", + PrivateDataFolder: serviceName, }) + // 加载数据库 + go func() { + dbpath := en.DataFolder() + cachePath := dbpath + "cache/" + dbfile := dbpath + "push.db" + defer order.DoneOnExit()() + _ = os.RemoveAll(en.DataFolder() + "cache") + err := os.MkdirAll(cachePath, 0755) + if err != nil { + panic(err) + } + bdb = initialize(dbfile) + log.Println("[bilibilipush]加载bilibilipush数据库") + }() + en.OnRegex(`^添加订阅\s?(\d+)$`, ctxext.UserOrGrpAdmin).SetBlock(true).Handle(func(ctx *zero.Ctx) { buid, _ := strconv.ParseInt(ctx.State["regex_matched"].([]string)[1], 10, 64) var name string @@ -202,7 +222,7 @@ func init() { if err != nil { log.Errorln("[bilibilipush]:", err) } - if id := ctx.SendChain(message.Image("base64://" + helper.BytesToString(data))); id.ID() == 0 { + if id := ctx.SendChain(message.Image("base64://" + binary.BytesToString(data))); id.ID() == 0 { ctx.SendChain(message.Text("ERROR: 可能被风控了")) } }) @@ -223,8 +243,8 @@ func checkBuid(buid int64) (status int, name string) { if err != nil { log.Errorln("[bilibilipush]:", err) } - status = int(gjson.Get(helper.BytesToString(data), "code").Int()) - name = gjson.Get(helper.BytesToString(data), "data.name").String() + status = int(gjson.Get(binary.BytesToString(data), "code").Int()) + name = gjson.Get(binary.BytesToString(data), "data.name").String() if status == 0 { bdb.insertBilibiliUp(buid, name) upMap[buid] = name @@ -281,7 +301,7 @@ func getUserDynamicCard(buid int64) (cardList []gjson.Result) { if err != nil { log.Errorln("[bilibilipush]:", err) } - cardList = gjson.Get(helper.BytesToString(data), "data.cards").Array() + cardList = gjson.Get(binary.BytesToString(data), "data.cards").Array() return } @@ -306,7 +326,7 @@ func getLiveList(uids ...int64) string { if err != nil { log.Errorln("[bilibilipush]:", err) } - return helper.BytesToString(data) + return binary.BytesToString(data) } func sendDynamic() { diff --git a/plugin_bilibili_push/data.go b/plugin_bilibili_push/data.go deleted file mode 100644 index 784af990..00000000 --- a/plugin_bilibili_push/data.go +++ /dev/null @@ -1,32 +0,0 @@ -package bilibilipush - -import ( - "os" - - "github.com/FloatTech/zbputils/process" - log "github.com/sirupsen/logrus" - - "github.com/FloatTech/zbputils/control/order" -) - -const ( - cachePath = dbpath + "cache/" - dbpath = "data/bilibilipush/" - dbfile = dbpath + "push.db" -) - -// bdb bilibili推送数据库 -var bdb *bilibilipushdb - -// 加载数据库 -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _ = os.MkdirAll(dbpath, 0755) - os.RemoveAll(cachePath) - _ = os.MkdirAll(cachePath, 0755) - bdb = initialize(dbfile) - log.Println("[bilibilipush]加载bilibilipush数据库") - }() -} diff --git a/plugin_book_review/book_review.go b/plugin_book_review/book_review.go index 0379b2e2..7856fc89 100644 --- a/plugin_book_review/book_review.go +++ b/plugin_book_review/book_review.go @@ -5,20 +5,38 @@ import ( log "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" - "github.com/wdvxdr1123/ZeroBot/utils/helper" + "github.com/FloatTech/zbputils/binary" "github.com/FloatTech/zbputils/control" - "github.com/FloatTech/zbputils/img/text" - "github.com/FloatTech/zbputils/control/order" + "github.com/FloatTech/zbputils/file" + "github.com/FloatTech/zbputils/img/text" ) func init() { engine := control.Register("bookreview", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "哀伤雪刃推书记录\n- 书评[xxx]\n- 随机书评", + PublicDataFolder: "BookReview", }) + go func() { + dbpath := engine.DataFolder() + db.DBPath = dbpath + "bookreview.db" + defer order.DoneOnExit()() + // os.RemoveAll(dbpath) + _, _ = file.GetLazyData(db.DBPath, false, true) + err := db.Create("bookreview", &book{}) + if err != nil { + panic(err) + } + n, err := db.Count("bookreview") + if err != nil { + panic(err) + } + log.Printf("[bookreview]读取%d条书评", n) + }() + // 中文、英文、数字但不包括下划线等符号 engine.OnRegex("^书评([\u4E00-\u9FA5A-Za-z0-9]{1,25})$").SetBlock(true). Handle(func(ctx *zero.Ctx) { @@ -27,7 +45,7 @@ func init() { if err != nil { log.Println("err:", err) } - if id := ctx.SendChain(message.Image("base64://" + helper.BytesToString(data))); id.ID() == 0 { + if id := ctx.SendChain(message.Image("base64://" + binary.BytesToString(data))); id.ID() == 0 { ctx.SendChain(message.Text("ERROR: 可能被风控了")) } }) @@ -39,7 +57,7 @@ func init() { if err != nil { log.Println("err:", err) } - if id := ctx.SendChain(message.Image("base64://" + helper.BytesToString(data))); id.ID() == 0 { + if id := ctx.SendChain(message.Image("base64://" + binary.BytesToString(data))); id.ID() == 0 { ctx.SendChain(message.Text("ERROR: 可能被风控了")) } }) diff --git a/plugin_book_review/data.go b/plugin_book_review/data.go deleted file mode 100644 index 95632eae..00000000 --- a/plugin_book_review/data.go +++ /dev/null @@ -1,38 +0,0 @@ -package bookreview - -import ( - "os" - - log "github.com/sirupsen/logrus" - - sql "github.com/FloatTech/sqlite" - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" -) - -const dbpath = "data/BookReview/" -const dbfile = dbpath + "bookreview.db" - -var db = &sql.Sqlite{DBPath: dbfile} - -// 加载数据库 -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - // os.RemoveAll(dbpath) - _ = os.MkdirAll(dbpath, 0755) - _, _ = file.GetLazyData(dbfile, false, true) - err := db.Create("bookreview", &book{}) - if err != nil { - panic(err) - } - n, err := db.Count("bookreview") - if err != nil { - panic(err) - } - log.Printf("[bookreview]读取%d条书评", n) - }() -} diff --git a/plugin_book_review/model.go b/plugin_book_review/model.go index 1998600b..93d39932 100644 --- a/plugin_book_review/model.go +++ b/plugin_book_review/model.go @@ -1,10 +1,14 @@ package bookreview +import sql "github.com/FloatTech/sqlite" + type book struct { ID uint64 `db:"id"` BookReview string `db:"bookreview"` } +var db = &sql.Sqlite{} + // 暂时随机选择一个书评 func getBookReviewByKeyword(keyword string) (b book) { _ = db.Find("bookreview", &b, "where bookreview LIKE '%"+keyword+"%'") diff --git a/plugin_chouxianghua/chouxianghua.go b/plugin_chouxianghua/chouxianghua.go index e570c1e8..a7664e28 100644 --- a/plugin_chouxianghua/chouxianghua.go +++ b/plugin_chouxianghua/chouxianghua.go @@ -2,18 +2,40 @@ package chouxianghua import ( - control "github.com/FloatTech/zbputils/control" + "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" + "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/control/order" + "github.com/FloatTech/zbputils/file" ) func init() { - control.Register("chouxianghua", order.AcquirePrio(), &control.Options{ + en := control.Register("chouxianghua", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "抽象话\n- 抽象翻译xxx", - }).OnRegex("^抽象翻译((\\s|[\\r\\n]|[\\p{Han}\\p{P}A-Za-z0-9])+)$").SetBlock(true). + PublicDataFolder: "ChouXiangHua", + }) + + go func() { + dbpath := en.DataFolder() + db.DBPath = dbpath + "cxh.db" + defer order.DoneOnExit()() + // os.RemoveAll(dbpath) + _, _ = file.GetLazyData(db.DBPath, false, true) + err := db.Create("pinyin", &pinyin{}) + if err != nil { + panic(err) + } + n, err := db.Count("pinyin") + if err != nil { + panic(err) + } + logrus.Printf("[chouxianghua]读取%d条拼音", n) + }() + + en.OnRegex("^抽象翻译((\\s|[\\r\\n]|[\\p{Han}\\p{P}A-Za-z0-9])+)$").SetBlock(true). Handle(func(ctx *zero.Ctx) { r := cx(ctx.State["regex_matched"].([]string)[1]) ctx.SendChain(message.Text(r)) diff --git a/plugin_chouxianghua/data.go b/plugin_chouxianghua/data.go deleted file mode 100644 index 7aa34fe3..00000000 --- a/plugin_chouxianghua/data.go +++ /dev/null @@ -1,40 +0,0 @@ -package chouxianghua - -import ( - "os" - - log "github.com/sirupsen/logrus" - - sql "github.com/FloatTech/sqlite" - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" -) - -const ( - dbpath = "data/ChouXiangHua/" - dbfile = dbpath + "cxh.db" -) - -var db = &sql.Sqlite{DBPath: dbfile} - -// 加载数据库 -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - // os.RemoveAll(dbpath) - _ = os.MkdirAll(dbpath, 0755) - _, _ = file.GetLazyData(dbfile, false, true) - err := db.Create("pinyin", &pinyin{}) - if err != nil { - panic(err) - } - n, err := db.Count("pinyin") - if err != nil { - panic(err) - } - log.Printf("[chouxianghua]读取%d条拼音", n) - }() -} diff --git a/plugin_chouxianghua/model.go b/plugin_chouxianghua/model.go index c12f2e07..e7796a0d 100644 --- a/plugin_chouxianghua/model.go +++ b/plugin_chouxianghua/model.go @@ -1,5 +1,7 @@ package chouxianghua +import sql "github.com/FloatTech/sqlite" + type pinyin struct { Word string `db:"word"` Pronun string `db:"pronunciation"` @@ -9,6 +11,8 @@ type emoji struct { Emoji string `db:"emoji"` } +var db = &sql.Sqlite{} + func getPinyinByWord(word string) string { var p pinyin _ = db.Find("pinyin", &p, "where word = '"+word+"'") diff --git a/plugin_cpstory/cpstory.go b/plugin_cpstory/cpstory.go index f9039cdb..472e6a5a 100644 --- a/plugin_cpstory/cpstory.go +++ b/plugin_cpstory/cpstory.go @@ -4,11 +4,13 @@ package cpstory import ( "strings" + "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/math" "github.com/FloatTech/zbputils/control/order" @@ -18,7 +20,26 @@ func init() { engine := control.Register("cpstory", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "cp短打\n- 组cp[@xxx][@xxx]\n- 磕cp大老师 雪乃", + PublicDataFolder: "CpStory", }) + + go func() { + dbpath := engine.DataFolder() + db.DBPath = dbpath + "cp.db" + defer order.DoneOnExit()() + // os.RemoveAll(dbpath) + _, _ = file.GetLazyData(db.DBPath, false, true) + err := db.Create("cp_story", &cpstory{}) + if err != nil { + panic(err) + } + n, err := db.Count("cp_story") + if err != nil { + panic(err) + } + logrus.Printf("[cpstory]读取%d条故事", n) + }() + engine.OnRegex("^组cp.*?(\\d+).*?(\\d+)", zero.OnlyGroup).SetBlock(true).Handle(func(ctx *zero.Ctx) { cs := getRandomCpStory() gong := ctxext.CardOrNickName(ctx, math.Str2Int64(ctx.State["regex_matched"].([]string)[1])) diff --git a/plugin_cpstory/data.go b/plugin_cpstory/data.go deleted file mode 100644 index a191a559..00000000 --- a/plugin_cpstory/data.go +++ /dev/null @@ -1,40 +0,0 @@ -package cpstory - -import ( - "os" - - log "github.com/sirupsen/logrus" - - sql "github.com/FloatTech/sqlite" - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" -) - -const ( - dbpath = "data/CpStory/" - dbfile = dbpath + "cp.db" -) - -var db = &sql.Sqlite{DBPath: dbfile} - -// 加载数据库 -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - // os.RemoveAll(dbpath) - _ = os.MkdirAll(dbpath, 0755) - _, _ = file.GetLazyData(dbfile, false, true) - err := db.Create("cp_story", &cpstory{}) - if err != nil { - panic(err) - } - n, err := db.Count("cp_story") - if err != nil { - panic(err) - } - log.Printf("[cpstory]读取%d条故事", n) - }() -} diff --git a/plugin_cpstory/model.go b/plugin_cpstory/model.go index e3f86ad8..0eb11de8 100644 --- a/plugin_cpstory/model.go +++ b/plugin_cpstory/model.go @@ -1,5 +1,7 @@ package cpstory +import sql "github.com/FloatTech/sqlite" + type cpstory struct { ID int64 `db:"id"` Gong string `db:"gong"` @@ -7,6 +9,8 @@ type cpstory struct { Story string `db:"story"` } +var db = &sql.Sqlite{} + func getRandomCpStory() (cs cpstory) { _ = db.Pick("cp_story", &cs) return diff --git a/plugin_curse/curse.go b/plugin_curse/curse.go index 2e6d0765..d89124a0 100644 --- a/plugin_curse/curse.go +++ b/plugin_curse/curse.go @@ -2,11 +2,13 @@ package curse import ( + "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/process" "github.com/FloatTech/zbputils/control/order" @@ -21,8 +23,25 @@ func init() { engine := control.Register("curse", order.AcquirePrio(), &control.Options{ DisableOnDefault: true, Help: "骂人(求骂,自卫)\n- 骂我\n- 大力骂我", + PublicDataFolder: "Curse", }) + go func() { + dbpath := engine.DataFolder() + db.DBPath = dbpath + "curse.db" + defer order.DoneOnExit()() + _, err := file.GetLazyData(db.DBPath, false, true) + if err != nil { + panic(err) + } + err = db.Create("curse", &curse{}) + if err != nil { + panic(err) + } + c, _ := db.Count("curse") + logrus.Infoln("[curse]加载", c, "条骂人语录") + }() + engine.OnFullMatch("骂我").SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) { process.SleepAbout1sTo2s() text := getRandomCurseByLevel(minLevel).Text diff --git a/plugin_curse/data.go b/plugin_curse/data.go deleted file mode 100644 index 9dd99bf8..00000000 --- a/plugin_curse/data.go +++ /dev/null @@ -1,41 +0,0 @@ -package curse - -import ( - "os" - - "github.com/sirupsen/logrus" - - sql "github.com/FloatTech/sqlite" - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" -) - -const ( - dbpath = "data/Curse/" - dbfile = dbpath + "curse.db" -) - -var ( - db = &sql.Sqlite{DBPath: dbfile} -) - -// 加载数据库 -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _ = os.MkdirAll(dbpath, 0755) - _, err := file.GetLazyData(dbfile, false, true) - if err != nil { - panic(err) - } - err = db.Create("curse", &curse{}) - if err != nil { - panic(err) - } - c, _ := db.Count("curse") - logrus.Infoln("[curse]加载", c, "条骂人语录") - }() -} diff --git a/plugin_curse/model.go b/plugin_curse/model.go index 20fb7bd2..8405aa5a 100644 --- a/plugin_curse/model.go +++ b/plugin_curse/model.go @@ -1,11 +1,15 @@ package curse +import sql "github.com/FloatTech/sqlite" + type curse struct { ID uint32 `db:"id"` Text string `db:"text"` Level string `db:"level"` } +var db = &sql.Sqlite{} + func getRandomCurseByLevel(level string) (c curse) { _ = db.Find("curse", &c, "where level = '"+level+"' ORDER BY RANDOM() limit 1") return diff --git a/plugin_danbooru/main.go b/plugin_danbooru/main.go index 07b1e156..4c83edef 100644 --- a/plugin_danbooru/main.go +++ b/plugin_danbooru/main.go @@ -4,7 +4,6 @@ package deepdanbooru import ( "crypto/md5" "encoding/hex" - "os" "github.com/FloatTech/AnimeAPI/danbooru" "github.com/FloatTech/AnimeAPI/saucenao" @@ -19,19 +18,16 @@ import ( "github.com/FloatTech/zbputils/control/order" ) -const cachefile = "data/danbooru/" - func init() { // 插件主体 - _ = os.RemoveAll(cachefile) - err := os.MkdirAll(cachefile, 0755) - if err != nil { - panic(err) - } engine := control.Register("danbooru", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "二次元图片标签识别\n" + "- 鉴赏图片[图片]", + PrivateDataFolder: "danbooru", }) + + cachefolder := engine.DataFolder() + // 上传一张图进行评价 engine.OnKeywordGroup([]string{"鉴赏图片"}, zero.OnlyGroup, ctxext.MustProvidePicture).SetBlock(true). Handle(func(ctx *zero.Ctx) { @@ -50,7 +46,7 @@ func init() { // 插件主体 return } digest := md5.Sum(helper.StringToBytes(url)) - f := cachefile + hex.EncodeToString(digest[:]) + f := cachefolder + hex.EncodeToString(digest[:]) if file.IsNotExist(f) { _ = writer.SavePNG2Path(f, t) } diff --git a/plugin_diana/bing.go b/plugin_diana/bing.go index 8041001f..c719059d 100644 --- a/plugin_diana/bing.go +++ b/plugin_diana/bing.go @@ -22,9 +22,17 @@ var engine = control.Register("diana", order.AcquirePrio(), &control.Options{ "- 发大病\n" + "- 教你一篇小作文[作文]\n" + "- [回复]查重", + PublicDataFolder: "Diana", }) func init() { + go func() { + datapath := engine.DataFolder() + dbfile := datapath + "text.db" + defer order.DoneOnExit()() + data.LoadText(dbfile) + }() + // 随机发送一篇上面的小作文 engine.OnFullMatch("小作文").SetBlock(true). Handle(func(ctx *zero.Ctx) { diff --git a/plugin_diana/data/text.go b/plugin_diana/data/text.go index a3192d87..7d6ab005 100644 --- a/plugin_diana/data/text.go +++ b/plugin_diana/data/text.go @@ -4,61 +4,38 @@ package data import ( "crypto/md5" "encoding/binary" - "os" - - log "github.com/sirupsen/logrus" - "github.com/wdvxdr1123/ZeroBot/utils/helper" sql "github.com/FloatTech/sqlite" + binutils "github.com/FloatTech/zbputils/binary" "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" + "github.com/sirupsen/logrus" ) -const ( - datapath = "data/Diana" - dbfile = datapath + "/text.db" -) - -var db = sql.Sqlite{DBPath: dbfile} +var db = sql.Sqlite{} type text struct { ID int64 `db:"id"` Data string `db:"data"` } -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - err := os.MkdirAll(datapath, 0755) - if err != nil { - panic(err) - } - err = LoadText() - if err == nil { - err = db.Create("text", &text{}) - if err != nil { - panic(err) - } - c, _ := db.Count("text") - log.Printf("[Diana]读取%d条小作文", c) - } else { - log.Printf("[Diana]读取小作文错误:%v", err) - } - }() -} - // LoadText 加载小作文 -func LoadText() error { +func LoadText(dbfile string) { _, err := file.GetLazyData(dbfile, false, false) - return err + db.DBPath = dbfile + if err != nil { + panic(err) + } + err = db.Create("text", &text{}) + if err != nil { + panic(err) + } + c, _ := db.Count("text") + logrus.Printf("[Diana]读取%d条小作文", c) } // AddText 添加小作文 func AddText(txt string) error { - s := md5.Sum(helper.StringToBytes(txt)) + s := md5.Sum(binutils.StringToBytes(txt)) i := binary.LittleEndian.Uint64(s[:8]) return db.Insert("text", &text{ID: int64(i), Data: txt}) } diff --git a/plugin_drift_bottle/data.go b/plugin_drift_bottle/data.go index 6313bd47..4627ed1e 100644 --- a/plugin_drift_bottle/data.go +++ b/plugin_drift_bottle/data.go @@ -18,6 +18,7 @@ type bottle struct { Msg string `db:"msg"` // Msg 消息,纯文本 } +var sea = &sql.Sqlite{} var seamu sync.RWMutex func newBottle(qq, grp int64, name, msg string) *bottle { diff --git a/plugin_drift_bottle/db.go b/plugin_drift_bottle/db.go deleted file mode 100644 index 568c9651..00000000 --- a/plugin_drift_bottle/db.go +++ /dev/null @@ -1,23 +0,0 @@ -package driftbottle - -import ( - "os" - - sql "github.com/FloatTech/sqlite" -) - -const ( - dbpath = "data/driftbottle/" - dbfile = dbpath + "sea.db" -) - -var sea = &sql.Sqlite{DBPath: dbfile} - -func init() { - _ = os.MkdirAll(dbpath, 0755) - err := sea.Open() - if err != nil { - panic(err) - } - _ = createChannel(sea, "global") -} diff --git a/plugin_drift_bottle/main.go b/plugin_drift_bottle/main.go index f1a39f7e..9a28453f 100644 --- a/plugin_drift_bottle/main.go +++ b/plugin_drift_bottle/main.go @@ -16,9 +16,16 @@ import ( func init() { en := control.Register("driftbottle", order.AcquirePrio(), &control.Options{ - DisableOnDefault: false, - Help: "漂流瓶\n- (在群xxx)丢漂流瓶(到频道xxx) [消息]\n- (从频道xxx)捡漂流瓶\n- @BOT 创建频道 xxx\n- 跳入(频道)海中\n- 注:不显式限制时,私聊发送可在所有群抽到,群聊发送仅可在本群抽到,默认频道为 global", + DisableOnDefault: false, + Help: "漂流瓶\n- (在群xxx)丢漂流瓶(到频道xxx) [消息]\n- (从频道xxx)捡漂流瓶\n- @BOT 创建频道 xxx\n- 跳入(频道)海中\n- 注:不显式限制时,私聊发送可在所有群抽到,群聊发送仅可在本群抽到,默认频道为 global", + PrivateDataFolder: "driftbottle", }) + sea.DBPath = en.DataFolder() + "sea.db" + err := sea.Open() + if err != nil { + panic(err) + } + _ = createChannel(sea, "global") en.OnRegex(`^(在群\d+)?丢漂流瓶(到频道\w+)?\s+(.*)$`).SetBlock(true). Handle(func(ctx *zero.Ctx) { msgs := ctx.State["regex_matched"].([]string) diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index 2a0d46f2..3f5c9b61 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -41,8 +41,7 @@ const ( ) var ( - // 底图类型列表:车万 DC4 爱因斯坦 星空列车 樱云之恋 富婆妹 李清歌 - // 公主连结 原神 明日方舟 碧蓝航线 碧蓝幻想 战双 阴阳师 赛马娘 + // 底图类型列表 table = [...]string{"车万", "DC4", "爱因斯坦", "星空列车", "樱云之恋", "富婆妹", "李清歌", "公主连结", "原神", "明日方舟", "碧蓝航线", "碧蓝幻想", "战双", "阴阳师", "赛马娘"} // 映射底图与 index index = make(map[string]uint8) @@ -51,37 +50,37 @@ var ( ) func init() { - for i, s := range table { - index[s] = uint8(i) - } - err := os.MkdirAll(images, 0755) - if err != nil { - panic(err) - } - _ = os.RemoveAll(cache) - err = os.MkdirAll(cache, 0755) - if err != nil { - panic(err) - } - data, err := file.GetLazyData(omikujson, true, false) - if err != nil { - panic(err) - } - err = json.Unmarshal(data, &omikujis) - if err != nil { - panic(err) - } - _, err = file.GetLazyData(font, false, true) - if err != nil { - panic(err) - } // 插件主体 en := control.Register("fortune", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "每日运势: \n" + "- 运势 | 抽签\n" + "- 设置底图[车万 | DC4 | 爱因斯坦 | 星空列车 | 樱云之恋 | 富婆妹 | 李清歌 | 公主连结 | 原神 | 明日方舟 | 碧蓝航线 | 碧蓝幻想 | 战双 | 阴阳师 | 赛马娘]", + PublicDataFolder: "Fortune", }) + go func() { + defer order.DoneOnExit()() + for i, s := range table { + index[s] = uint8(i) + } + _ = os.RemoveAll(cache) + err := os.MkdirAll(cache, 0755) + if err != nil { + panic(err) + } + data, err := file.GetLazyData(omikujson, true, false) + if err != nil { + panic(err) + } + err = json.Unmarshal(data, &omikujis) + if err != nil { + panic(err) + } + _, err = file.GetLazyData(font, false, true) + if err != nil { + panic(err) + } + }() en.OnRegex(`^设置底图\s?(.*)`).SetBlock(true). Handle(func(ctx *zero.Ctx) { gid := ctx.Event.GroupID @@ -93,7 +92,7 @@ func init() { if ok { c, ok := control.Lookup("fortune") if ok { - err = c.SetData(gid, int64(i)&0xff) + err := c.SetData(gid, int64(i)&0xff) if err != nil { ctx.SendChain(message.Text("设置失败:", err)) return @@ -125,7 +124,7 @@ func init() { } // 检查背景图片是否存在 zipfile := images + kind + ".zip" - _, err = file.GetLazyData(zipfile, false, false) + _, err := file.GetLazyData(zipfile, false, false) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return diff --git a/plugin_funny/data.go b/plugin_funny/data.go deleted file mode 100644 index ba284b10..00000000 --- a/plugin_funny/data.go +++ /dev/null @@ -1,41 +0,0 @@ -package funny - -import ( - "os" - - "github.com/sirupsen/logrus" - - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" -) - -type joke struct { - ID uint32 `db:"id"` - Text string `db:"text"` -} - -const ( - dbpath = "data/Funny/" - dbfile = dbpath + "jokes.db" -) - -// 加载数据库 -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _ = os.MkdirAll(dbpath, 0755) - _, 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, "个笑话") - }() -} diff --git a/plugin_funny/data_test.go b/plugin_funny/data_test.go deleted file mode 100644 index 75fa7172..00000000 --- a/plugin_funny/data_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package funny - -import ( - "crypto/md5" - "encoding/binary" - "os" - "strings" - "testing" - - sql "github.com/FloatTech/sqlite" - "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) - } -} diff --git a/plugin_funny/laugh.go b/plugin_funny/laugh.go index 7f06a04a..596e5044 100644 --- a/plugin_funny/laugh.go +++ b/plugin_funny/laugh.go @@ -4,24 +4,50 @@ package funny import ( "strings" + "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" sql "github.com/FloatTech/sqlite" control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/control/order" ) -var db = &sql.Sqlite{DBPath: dbfile} +type joke struct { + ID uint32 `db:"id"` + Text string `db:"text"` +} + +var db = &sql.Sqlite{} func init() { - control.Register("funny", order.AcquirePrio(), &control.Options{ + en := control.Register("funny", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "讲个笑话\n" + "- 讲个笑话[@xxx] | 讲个笑话[qq号]", - }).OnPrefix("讲个笑话").SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) { + PublicDataFolder: "Funny", + }) + + go func() { + dbpath := en.DataFolder() + db.DBPath = dbpath + "jokes.db" + defer order.DoneOnExit()() + _, err := file.GetLazyData(db.DBPath, 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, "个笑话") + }() + + en.OnPrefix("讲个笑话").SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) { // 获取名字 name := ctxext.NickName(ctx) var j joke diff --git a/plugin_gif/run.go b/plugin_gif/run.go index de0e28b9..b6268c0a 100644 --- a/plugin_gif/run.go +++ b/plugin_gif/run.go @@ -3,7 +3,6 @@ package gif import ( "math/rand" - "os" "reflect" "strconv" "strings" @@ -11,6 +10,7 @@ import ( control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/file" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" @@ -20,17 +20,18 @@ import ( var ( cmds = []string{"搓", "冲", "摸", "拍", "丢", "吃", "敲", "啃", "蹭", "爬", "撕", "灰度", "上翻", "下翻", "左翻", "右翻", "反色", "浮雕", "打码", "负片"} - botpath, _ = os.Getwd() - datapath = botpath + "/data/gif/" + datapath string ) func init() { // 插件主体 - _ = os.MkdirAll(datapath, 0755) rand.Seed(time.Now().UnixNano()) // 设置种子 - control.Register("gif", order.AcquirePrio(), &control.Options{ - DisableOnDefault: false, - Help: "制图\n- " + strings.Join(cmds, "\n- "), - }).ApplySingle(ctxext.DefaultSingle).OnRegex(`^(` + strings.Join(cmds, "|") + `)\D*?(\[CQ:(image\,file=([0-9a-zA-Z]{32}).*|at.+?(\d{5,11}))\].*|(\d+))$`). + en := control.Register("gif", order.AcquirePrio(), &control.Options{ + DisableOnDefault: false, + Help: "制图\n- " + strings.Join(cmds, "\n- "), + PrivateDataFolder: "gif", + }).ApplySingle(ctxext.DefaultSingle) + datapath = file.BOTPATH + "/" + en.DataFolder() + en.OnRegex(`^(` + strings.Join(cmds, "|") + `)\D*?(\[CQ:(image\,file=([0-9a-zA-Z]{32}).*|at.+?(\d{5,11}))\].*|(\d+))$`). SetBlock(true).Handle(func(ctx *zero.Ctx) { c := newContext(ctx.Event.UserID) list := ctx.State["regex_matched"].([]string) diff --git a/plugin_hs/run.go b/plugin_hs/run.go index a4e77229..cd430f8c 100644 --- a/plugin_hs/run.go +++ b/plugin_hs/run.go @@ -20,11 +20,8 @@ import ( "github.com/FloatTech/zbputils/control/order" ) -var ( - cachedir = file.BOTPATH + "/data/hs/" - reqconf = [...]string{"GET", "https://hs.fbigame.com", - "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36"} -) +var reqconf = [...]string{"GET", "https://hs.fbigame.com", + "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36"} const ( hs = `https://hs.fbigame.com/ajax.php?` @@ -46,19 +43,15 @@ const ( ) func init() { - os.RemoveAll(cachedir) - err := os.MkdirAll(cachedir, 0755) - if err != nil { - panic(err) - } - engine := control.Register("hs", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "炉石\n" + "- 搜卡[xxxx]\n" + "- [卡组代码xxx]\n" + "- 更多搜卡指令参数:https://hs.fbigame.com/misc/searchhelp", + PrivateDataFolder: "hs", }).ApplySingle(ctxext.DefaultSingle) + cachedir := file.BOTPATH + "/" + engine.DataFolder() engine.OnRegex(`^搜卡(.+)$`). SetBlock(true).Handle(func(ctx *zero.Ctx) { List := ctx.State["regex_matched"].([]string)[1] diff --git a/plugin_jandan/data.go b/plugin_jandan/data.go index a5adf6b1..21534d15 100644 --- a/plugin_jandan/data.go +++ b/plugin_jandan/data.go @@ -1,21 +1,12 @@ package jandan import ( - "os" "sync" sql "github.com/FloatTech/sqlite" - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - log "github.com/sirupsen/logrus" - - "github.com/FloatTech/zbputils/control/order" ) -const dbpath = "data/Jandan/" -const dbfile = dbpath + "pics.db" - -var db = &sql.Sqlite{DBPath: dbfile} +var db = &sql.Sqlite{} var mu sync.RWMutex type picture struct { @@ -31,22 +22,3 @@ func getRandomPicture() (u string, err error) { u = p.URL return } - -// 加载数据库 -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _ = os.MkdirAll(dbpath, 0755) - _, _ = file.GetLazyData(dbfile, false, false) - err := db.Create("picture", &picture{}) - if err != nil { - panic(err) - } - n, err := db.Count("picture") - if err != nil { - panic(err) - } - log.Printf("[jandan]读取%d张图片", n) - }() -} diff --git a/plugin_jandan/jandan.go b/plugin_jandan/jandan.go index bf424a6b..b2008f06 100644 --- a/plugin_jandan/jandan.go +++ b/plugin_jandan/jandan.go @@ -9,6 +9,7 @@ import ( "github.com/FloatTech/zbputils/binary" "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/file" "github.com/antchfx/htmlquery" "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" @@ -25,8 +26,25 @@ func init() { engine := control.Register("jandan", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "煎蛋网无聊图\n- 来份屌图\n- 更新屌图\n", + PublicDataFolder: "Jandan", }) + go func() { + dbpath := engine.DataFolder() + db.DBPath = dbpath + "pics.db" + defer order.DoneOnExit()() + _, _ = file.GetLazyData(db.DBPath, false, false) + err := db.Create("picture", &picture{}) + if err != nil { + panic(err) + } + n, err := db.Count("picture") + if err != nil { + panic(err) + } + logrus.Printf("[jandan]读取%d张图片", n) + }() + engine.OnFullMatch("来份屌图").SetBlock(true). Handle(func(ctx *zero.Ctx) { u, err := getRandomPicture() diff --git a/plugin_manager/manager.go b/plugin_manager/manager.go index 969d7f7d..e617f80f 100644 --- a/plugin_manager/manager.go +++ b/plugin_manager/manager.go @@ -4,7 +4,6 @@ package manager import ( "fmt" "math/rand" - "os" "sort" "strconv" "strings" @@ -26,9 +25,7 @@ import ( ) const ( - datapath = "data/manager/" - confile = datapath + "config.db" - hint = "====群管====\n" + + hint = "====群管====\n" + "- 禁言@QQ 1分钟\n" + "- 解除禁言 @QQ\n" + "- 我要自闭 1分钟\n" + @@ -56,20 +53,20 @@ const ( ) var ( - db = &sql.Sqlite{DBPath: confile} + db = &sql.Sqlite{} clock timer.Clock ) func init() { // 插件主体 engine := control.Register("manager", order.AcquirePrio(), &control.Options{ - DisableOnDefault: false, - Help: hint, + DisableOnDefault: false, + Help: hint, + PrivateDataFolder: "manager", }) go func() { defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _ = os.MkdirAll(datapath, 0755) + db.DBPath = engine.DataFolder() + "config.db" clock = timer.NewClock(db) err := db.Create("welcome", &welcome{}) if err != nil { diff --git a/plugin_nativesetu/data.go b/plugin_nativesetu/data.go index e4828752..3786ea3a 100644 --- a/plugin_nativesetu/data.go +++ b/plugin_nativesetu/data.go @@ -10,14 +10,10 @@ import ( "github.com/corona10/goimagehash" "github.com/sirupsen/logrus" - "github.com/wdvxdr1123/ZeroBot/utils/helper" _ "golang.org/x/image/webp" // import webp decoding sql "github.com/FloatTech/sqlite" "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" ) // setuclass holds setus in a folder, which is the class name. @@ -27,25 +23,7 @@ type setuclass struct { Path string `db:"path"` // Path 图片路径 } -var ns = &nsetu{db: &sql.Sqlite{DBPath: dbfile}} - -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - err := os.MkdirAll(datapath, 0755) - if err != nil { - panic(err) - } - if file.IsExist(cfgfile) { - b, err := os.ReadFile(cfgfile) - if err == nil { - setupath = helper.BytesToString(b) - logrus.Println("[nsetu] set setu dir to", setupath) - } - } - }() -} +var ns = &nsetu{db: &sql.Sqlite{}} type nsetu struct { db *sql.Sqlite diff --git a/plugin_nativesetu/main.go b/plugin_nativesetu/main.go index b43bb52a..a954f6eb 100644 --- a/plugin_nativesetu/main.go +++ b/plugin_nativesetu/main.go @@ -12,16 +12,11 @@ import ( "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/control/order" ) -const ( - datapath = "data/nsetu" - dbfile = datapath + "/data.db" - cfgfile = datapath + "/setupath.txt" -) - var ( setupath = "/tmp" // 绝对路径,图片根目录 ) @@ -35,7 +30,19 @@ func init() { "- 设置本地setu绝对路径[xxx]\n" + "- 刷新所有本地setu\n" + "- 所有本地setu分类", + PrivateDataFolder: "nsetu", }) + + ns.db.DBPath = engine.DataFolder() + "data.db" + cfgfile := engine.DataFolder() + "setupath.txt" + if file.IsExist(cfgfile) { + b, err := os.ReadFile(cfgfile) + if err == nil { + setupath = helper.BytesToString(b) + logrus.Println("[nsetu] set setu dir to", setupath) + } + } + engine.OnRegex(`^本地(.*)$`, ctxext.FirstValueInList(ns)).SetBlock(true). Handle(func(ctx *zero.Ctx) { imgtype := ctx.State["regex_matched"].([]string)[1] diff --git a/plugin_nativewife/main.go b/plugin_nativewife/main.go index 3a506999..2fced058 100644 --- a/plugin_nativewife/main.go +++ b/plugin_nativewife/main.go @@ -23,19 +23,14 @@ import ( "github.com/FloatTech/zbputils/file" ) -const base = "data/nwife" - -var baseuri = "file:///" + file.BOTPATH + "/" + base - func init() { - err := os.MkdirAll(base, 0755) - if err != nil { - panic(err) - } engine := control.Register("nwife", order.AcquirePrio(), &control.Options{ - DisableOnDefault: false, - Help: "nativewife\n- 抽wife[@xxx]\n- 添加wife[名字][图片]\n- 删除wife[名字]\n- [让 | 不让]所有人均可添加wife", + DisableOnDefault: false, + Help: "nativewife\n- 抽wife[@xxx]\n- 添加wife[名字][图片]\n- 删除wife[名字]\n- [让 | 不让]所有人均可添加wife", + PrivateDataFolder: "nwife", }) + base := engine.DataFolder() + baseuri := "file:///" + file.BOTPATH + "/" + base engine.OnPrefix("抽wife", zero.OnlyGroup).SetBlock(true). Handle(func(ctx *zero.Ctx) { grpf := strconv.FormatInt(ctx.Event.GroupID, 36) @@ -78,13 +73,13 @@ func init() { url := ctx.State["image_url"].([]string)[0] grpfolder := base + "/" + strconv.FormatInt(ctx.Event.GroupID, 36) if file.IsNotExist(grpfolder) { - err = os.Mkdir(grpfolder, 0755) + err := os.Mkdir(grpfolder, 0755) if err != nil { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("错误:", err.Error())) return } } - err = file.DownloadTo(url, grpfolder+"/"+name, true) + err := file.DownloadTo(url, grpfolder+"/"+name, true) if err == nil { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("成功!")) } else { @@ -108,7 +103,7 @@ func init() { } if name != "" { grpfolder := base + "/" + strconv.FormatInt(ctx.Event.GroupID, 36) - err = os.Remove(grpfolder + "/" + name) + err := os.Remove(grpfolder + "/" + name) if err == nil { ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("成功!")) } else { diff --git a/plugin_omikuji/data.go b/plugin_omikuji/data.go deleted file mode 100644 index 2ba44a1f..00000000 --- a/plugin_omikuji/data.go +++ /dev/null @@ -1,38 +0,0 @@ -package omikuji - -import ( - "os" - - log "github.com/sirupsen/logrus" - - sql "github.com/FloatTech/sqlite" - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" -) - -const ( - dbpath = "data/Omikuji/" - dbfile = dbpath + "kuji.db" -) - -var db = &sql.Sqlite{DBPath: dbfile} - -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _ = os.MkdirAll(dbpath, 0755) - _, _ = file.GetLazyData(dbfile, false, true) - err := db.Create("kuji", &kuji{}) - if err != nil { - panic(err) - } - n, err := db.Count("kuji") - if err != nil { - panic(err) - } - log.Printf("[kuji]读取%d条签文", n) - }() -} diff --git a/plugin_omikuji/model.go b/plugin_omikuji/model.go index f0ad393a..2f204acf 100644 --- a/plugin_omikuji/model.go +++ b/plugin_omikuji/model.go @@ -1,12 +1,18 @@ package omikuji -import "strconv" +import ( + "strconv" + + sql "github.com/FloatTech/sqlite" +) type kuji struct { ID uint8 `db:"id"` Text string `db:"text"` } +var db = &sql.Sqlite{} + // 返回一个解签 func getKujiByBango(id uint8) string { var s kuji diff --git a/plugin_omikuji/sensou.go b/plugin_omikuji/sensou.go index 722be422..79db9a2f 100644 --- a/plugin_omikuji/sensou.go +++ b/plugin_omikuji/sensou.go @@ -14,6 +14,7 @@ import ( control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" + "github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/img/text" "github.com/FloatTech/zbputils/control/order" @@ -26,7 +27,25 @@ func init() { // 插件主体 DisableOnDefault: false, Help: "浅草寺求签\n" + "- 求签 | 占卜\n- 解签", + PublicDataFolder: "Omikuji", }).ApplySingle(ctxext.DefaultSingle) + + go func() { + dbpath := engine.DataFolder() + db.DBPath = dbpath + "kuji.db" + defer order.DoneOnExit()() + _, _ = file.GetLazyData(db.DBPath, false, true) + err := db.Create("kuji", &kuji{}) + if err != nil { + panic(err) + } + n, err := db.Count("kuji") + if err != nil { + panic(err) + } + log.Printf("[kuji]读取%d条签文", n) + }() + engine.OnFullMatchGroup([]string{"求签", "占卜"}).SetBlock(true). Handle(func(ctx *zero.Ctx) { miku := bangoToday(ctx.Event.UserID) diff --git a/plugin_reborn/born.go b/plugin_reborn/born.go index 5255bbfd..a08e198a 100644 --- a/plugin_reborn/born.go +++ b/plugin_reborn/born.go @@ -4,7 +4,13 @@ import ( wr "github.com/mroth/weightedrand" ) +type rate []struct { + Name string `json:"name"` + Weight float64 `json:"weight"` +} + var ( + areac *wr.Chooser gender, _ = wr.NewChooser( wr.Choice{Item: "男孩子", Weight: 50707}, wr.Choice{Item: "女孩子", Weight: 48292}, diff --git a/plugin_reborn/load.go b/plugin_reborn/load.go index 49af0682..e9430864 100644 --- a/plugin_reborn/load.go +++ b/plugin_reborn/load.go @@ -2,59 +2,12 @@ package reborn import ( "encoding/json" - "os" - - wr "github.com/mroth/weightedrand" - log "github.com/sirupsen/logrus" "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" ) -const ( - datapath = "data/Reborn" - jsonfile = datapath + "/rate.json" -) - -type rate []struct { - Name string `json:"name"` - Weight float64 `json:"weight"` -} - -var ( - areac *wr.Chooser -) - -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - err := os.MkdirAll(datapath, 0755) - if err != nil { - panic(err) - } - area := make(rate, 226) - err = load(&area) - if err != nil { - panic(err) - } - choices := make([]wr.Choice, len(area)) - for i, a := range area { - choices[i].Item = a.Name - choices[i].Weight = uint(a.Weight * 1e9) - } - areac, err = wr.NewChooser(choices...) - if err != nil { - panic(err) - } - log.Printf("[Reborn]读取%d个国家/地区", len(area)) - }() -} - // load 加载rate数据 -func load(area *rate) error { +func load(area *rate, jsonfile string) error { data, err := file.GetLazyData(jsonfile, true, true) if err != nil { return err diff --git a/plugin_reborn/main.go b/plugin_reborn/main.go index 7b4c61ce..062e338f 100644 --- a/plugin_reborn/main.go +++ b/plugin_reborn/main.go @@ -4,9 +4,10 @@ package reborn import ( "fmt" "math/rand" - "time" control "github.com/FloatTech/zbputils/control" + wr "github.com/mroth/weightedrand" + "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" @@ -14,11 +15,32 @@ import ( ) func init() { - rand.Seed(time.Now().UnixNano()) - control.Register("reborn", order.AcquirePrio(), &control.Options{ + en := control.Register("reborn", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "投胎\n- reborn", - }).OnFullMatch("reborn").SetBlock(true). + PublicDataFolder: "Reborn", + }) + go func() { + datapath := en.DataFolder() + jsonfile := datapath + "rate.json" + defer order.DoneOnExit()() + area := make(rate, 226) + err := load(&area, jsonfile) + if err != nil { + panic(err) + } + choices := make([]wr.Choice, len(area)) + for i, a := range area { + choices[i].Item = a.Name + choices[i].Weight = uint(a.Weight * 1e9) + } + areac, err = wr.NewChooser(choices...) + if err != nil { + panic(err) + } + logrus.Printf("[Reborn]读取%d个国家/地区", len(area)) + }() + en.OnFullMatch("reborn").SetBlock(true). Handle(func(ctx *zero.Ctx) { if rand.Int31() > 1<<27 { ctx.SendChain(message.At(ctx.Event.UserID), message.Text(fmt.Sprintf("投胎成功!\n您出生在 %s, 是 %s。", randcoun(), randgen()))) diff --git a/plugin_scale/main.go b/plugin_scale/main.go index 8cf1e11c..070bd93f 100644 --- a/plugin_scale/main.go +++ b/plugin_scale/main.go @@ -17,18 +17,13 @@ import ( "github.com/FloatTech/zbputils/control/order" ) -const cachedir = "data/scale/" - func init() { - _ = os.RemoveAll(cachedir) - err := os.MkdirAll(cachedir, 0755) - if err != nil { - panic(err) - } engine := control.Register("scale", order.AcquirePrio(), &control.Options{ - DisableOnDefault: false, - Help: "叔叔的AI二次元图片放大\n- 放大图片[图片]", + DisableOnDefault: false, + Help: "叔叔的AI二次元图片放大\n- 放大图片[图片]", + PrivateDataFolder: "scale", }).ApplySingle(ctxext.DefaultSingle) + cachedir := engine.DataFolder() // 上传一张图进行评价 engine.OnKeywordGroup([]string{"放大图片"}, zero.OnlyGroup, ctxext.MustProvidePicture, getPara).SetBlock(true). Handle(func(ctx *zero.Ctx) { diff --git a/plugin_score/data.go b/plugin_score/data.go deleted file mode 100644 index fcd4e829..00000000 --- a/plugin_score/data.go +++ /dev/null @@ -1,43 +0,0 @@ -package score - -import ( - "os" - - log "github.com/sirupsen/logrus" - - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/img/text" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" -) - -const ( - cachePath = dbpath + "cache/" - dbpath = "data/score/" - dbfile = dbpath + "score.db" -) - -// sdb 得分数据库 -var sdb *scoredb - -// 加载数据库 -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _, err := file.GetLazyData(text.BoldFontFile, false, true) - if err != nil { - panic(err) - } - _, err = file.GetLazyData(text.FontFile, false, true) - if err != nil { - panic(err) - } - _ = os.MkdirAll(dbpath, 0755) - os.RemoveAll(cachePath) - _ = os.MkdirAll(cachePath, 0755) - sdb = initialize(dbfile) - log.Println("[score]加载score数据库") - }() -} diff --git a/plugin_score/model.go b/plugin_score/model.go index ff5a7718..25d6d738 100644 --- a/plugin_score/model.go +++ b/plugin_score/model.go @@ -8,6 +8,9 @@ import ( _ "github.com/logoove/sqlite" // import sql ) +// sdb 得分数据库 +var sdb *scoredb + // scoredb 分数数据库 type scoredb gorm.DB diff --git a/plugin_score/sign_in.go b/plugin_score/sign_in.go index 5271d3d7..e76fc983 100644 --- a/plugin_score/sign_in.go +++ b/plugin_score/sign_in.go @@ -38,9 +38,29 @@ var levelArray = [...]int{0, 1, 2, 5, 10, 20, 35, 55, 75, 100, 120} func init() { engine := control.Register("score", order.AcquirePrio(), &control.Options{ - DisableOnDefault: false, - Help: "签到得分\n- 签到\n- 获得签到背景[@xxx] | 获得签到背景", + DisableOnDefault: false, + Help: "签到得分\n- 签到\n- 获得签到背景[@xxx] | 获得签到背景", + PrivateDataFolder: "score", }) + cachePath := engine.DataFolder() + "cache/" + go func() { + defer order.DoneOnExit()() + os.RemoveAll(cachePath) + err := os.MkdirAll(cachePath, 0755) + if err != nil { + panic(err) + } + _, err = file.GetLazyData(text.BoldFontFile, false, true) + if err != nil { + panic(err) + } + _, err = file.GetLazyData(text.FontFile, false, true) + if err != nil { + panic(err) + } + sdb = initialize(engine.DataFolder() + "score.db") + log.Println("[score]加载score数据库") + }() engine.OnFullMatch("签到", zero.OnlyGroup).SetBlock(true). Handle(func(ctx *zero.Ctx) { uid := ctx.Event.UserID diff --git a/plugin_setutime/setu_geter.go b/plugin_setutime/setu_geter.go index 4e155cf5..a5f86993 100644 --- a/plugin_setutime/setu_geter.go +++ b/plugin_setutime/setu_geter.go @@ -4,7 +4,6 @@ package setutime import ( "errors" "fmt" - "os" "strconv" "strings" "sync" @@ -43,19 +42,27 @@ func (p *imgpool) List() (l []string) { } var pool = &imgpool{ - db: &sql.Sqlite{DBPath: "data/SetuTime/SetuTime.db"}, + db: &sql.Sqlite{}, path: pixiv.CacheDir, max: 10, pool: map[string][]*pixiv.Illust{}, } func init() { // 插件主体 - _ = os.MkdirAll("data/SetuTime", 0755) + engine := control.Register("setutime", order.AcquirePrio(), &control.Options{ + DisableOnDefault: false, + Help: "涩图\n" + + "- 来份[涩图/二次元/风景/车万]\n" + + "- 添加[涩图/二次元/风景/车万][P站图片ID]\n" + + "- 删除[涩图/二次元/风景/车万][P站图片ID]\n" + + "- >setu status", + PublicDataFolder: "SetuTime", + }) go func() { defer order.DoneOnExit()() - process.SleepAbout1sTo2s() // 如果数据库不存在则下载 + pool.db.DBPath = engine.DataFolder() + "SetuTime.db" _, _ = fileutil.GetLazyData(pool.db.DBPath, false, false) err := pool.db.Open() if err != nil { @@ -68,14 +75,6 @@ func init() { // 插件主体 } }() - engine := control.Register("setutime", order.AcquirePrio(), &control.Options{ - DisableOnDefault: false, - Help: "涩图\n" + - "- 来份[涩图/二次元/风景/车万]\n" + - "- 添加[涩图/二次元/风景/车万][P站图片ID]\n" + - "- 删除[涩图/二次元/风景/车万][P站图片ID]\n" + - "- >setu status", - }) engine.OnRegex(`^来份(.*)$`, ctxext.FirstValueInList(pool)).SetBlock(true).Limit(ctxext.LimitByUser). Handle(func(ctx *zero.Ctx) { var imgtype = ctx.State["regex_matched"].([]string)[1] diff --git a/plugin_sleep_manage/data.go b/plugin_sleep_manage/data.go deleted file mode 100644 index d41f922e..00000000 --- a/plugin_sleep_manage/data.go +++ /dev/null @@ -1,20 +0,0 @@ -package sleepmanage - -import ( - "os" - - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" - - "github.com/FloatTech/ZeroBot-Plugin/plugin_sleep_manage/model" -) - -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _ = os.MkdirAll(dbpath, 0755) - model.Initialize(dbfile) - }() -} diff --git a/plugin_sleep_manage/sleep_manage.go b/plugin_sleep_manage/sleep_manage.go index ddeb513e..2b0fdcc5 100644 --- a/plugin_sleep_manage/sleep_manage.go +++ b/plugin_sleep_manage/sleep_manage.go @@ -16,14 +16,13 @@ import ( "github.com/FloatTech/ZeroBot-Plugin/plugin_sleep_manage/model" ) -const dbpath = "data/sleep/" -const dbfile = dbpath + "manage.db" - func init() { engine := control.Register("sleepmanage", order.AcquirePrio(), &control.Options{ - DisableOnDefault: false, - Help: "sleepmanage\n- 早安\n- 晚安", + DisableOnDefault: false, + Help: "sleepmanage\n- 早安\n- 晚安", + PrivateDataFolder: "sleep", }) + dbfile := engine.DataFolder() + "manage.db" engine.OnFullMatch("早安", isMorning, zero.OnlyGroup).SetBlock(true). Handle(func(ctx *zero.Ctx) { db, err := model.Open(dbfile) diff --git a/plugin_thesaurus/chat.go b/plugin_thesaurus/chat.go index f077b42c..19356821 100644 --- a/plugin_thesaurus/chat.go +++ b/plugin_thesaurus/chat.go @@ -2,31 +2,42 @@ package thesaurus import ( + "encoding/json" "math/rand" control "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/file" + "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/zbputils/control/order" ) -const ( - dbpath = "data/Chat/" - dbfile = dbpath + "kimoi.json" -) - -var ( - engine = control.Register("thesaurus", order.AcquirePrio(), &control.Options{ - DisableOnDefault: false, - Help: "thesaurus\n- 词典匹配回复", - }) - kimomap = make(kimo, 256) - chatList = make([]string, 0, 256) -) +type kimo = map[string]*[]string func init() { - initThesaurusList(func() { + engine := control.Register("thesaurus", order.AcquirePrio(), &control.Options{ + DisableOnDefault: false, + Help: "thesaurus\n- 词典匹配回复", + PublicDataFolder: "Chat", + }) + go func() { + defer order.DoneOnExit()() + data, err := file.GetLazyData(engine.DataFolder()+"kimoi.json", true, true) + if err != nil { + panic(err) + } + kimomap := make(kimo, 256) + err = json.Unmarshal(data, &kimomap) + if err != nil { + panic(err) + } + chatList := make([]string, 0, 256) + for k := range kimomap { + chatList = append(chatList, k) + } + logrus.Infoln("[thesaurus]加载", len(chatList), "条kimoi") engine.OnFullMatchGroup(chatList, zero.OnlyToMe).SetBlock(true).Handle( func(ctx *zero.Ctx) { key := ctx.MessageString() @@ -34,5 +45,5 @@ func init() { text := val[rand.Intn(len(val))] ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(text)) // 来自于 https://github.com/Kyomotoi/AnimeThesaurus 的回复 经过二次修改 }) - }) + }() } diff --git a/plugin_thesaurus/data.go b/plugin_thesaurus/data.go deleted file mode 100644 index 0ee3a7a7..00000000 --- a/plugin_thesaurus/data.go +++ /dev/null @@ -1,36 +0,0 @@ -package thesaurus - -import ( - "encoding/json" - "os" - - "github.com/sirupsen/logrus" - - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" -) - -type kimo = map[string]*[]string - -func initThesaurusList(postinit func()) { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _ = os.MkdirAll(dbpath, 0755) - data, err := file.GetLazyData(dbfile, true, true) - if err != nil { - panic(err) - } - err = json.Unmarshal(data, &kimomap) - if err != nil { - panic(err) - } - for k := range kimomap { - chatList = append(chatList, k) - } - logrus.Infoln("[thesaurus]加载", len(chatList), "条kimoi") - postinit() - }() -} diff --git a/plugin_vtb_quotation/cron.go b/plugin_vtb_quotation/cron.go deleted file mode 100644 index df5f5b1d..00000000 --- a/plugin_vtb_quotation/cron.go +++ /dev/null @@ -1,45 +0,0 @@ -package vtbquotation - -import ( - "github.com/fumiama/cron" - log "github.com/sirupsen/logrus" - - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" - - "github.com/FloatTech/ZeroBot-Plugin/plugin_vtb_quotation/model" -) - -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - log.Println("[vtb/cron] 开启vtb数据库日常更新") - vtbDaily() - }() -} - -func vtbDaily() { - c := cron.New() - _, err := c.AddFunc("0 4 * * *", func() { vtbData() }) - if err != nil { - log.Errorln("定时任务有错误:", err) - } else { - log.Println("开启vtb数据库定时任务") - c.Start() - } -} - -func vtbData() { - db := model.Initialize(dbfile) - if db != nil { - for _, v := range db.GetVtbList() { - db.StoreVtb(v) - } - err := db.Close() - if err != nil { - log.Errorln("[vtb/cron]", err) - } - } -} diff --git a/plugin_vtb_quotation/data.go b/plugin_vtb_quotation/data.go deleted file mode 100644 index 18b9b70a..00000000 --- a/plugin_vtb_quotation/data.go +++ /dev/null @@ -1,20 +0,0 @@ -package vtbquotation - -import ( - "os" - - "github.com/FloatTech/zbputils/file" - "github.com/FloatTech/zbputils/process" - - "github.com/FloatTech/zbputils/control/order" -) - -// 加载数据库 -func init() { - go func() { - defer order.DoneOnExit()() - process.SleepAbout1sTo2s() - _ = os.MkdirAll(dbpath, 0755) - _, _ = file.GetLazyData(dbfile, false, false) - }() -} diff --git a/plugin_vtb_quotation/vtb_quotation.go b/plugin_vtb_quotation/vtb_quotation.go index 1fd5d00d..40a0c12c 100644 --- a/plugin_vtb_quotation/vtb_quotation.go +++ b/plugin_vtb_quotation/vtb_quotation.go @@ -15,6 +15,7 @@ import ( "github.com/wdvxdr1123/ZeroBot/utils/helper" control "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/img/text" "github.com/FloatTech/zbputils/control/order" @@ -22,17 +23,19 @@ import ( "github.com/FloatTech/ZeroBot-Plugin/plugin_vtb_quotation/model" ) -const ( - regStr = ".*/(.*)" - dbpath = "data/VtbQuotation/" - dbfile = dbpath + "vtb.db" -) +const regStr = ".*/(.*)" func init() { engine := control.Register("vtbquotation", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, Help: "vtbkeyboard.moe\n- vtb语录\n- 随机vtb\n- 更新vtb\n", + PublicDataFolder: "VtbQuotation", }) + dbfile := engine.DataFolder() + "vtb.db" + go func() { + defer order.DoneOnExit()() + _, _ = file.GetLazyData(dbfile, false, false) + }() engine.OnFullMatch("vtb语录").SetBlock(true). Handle(func(ctx *zero.Ctx) { var firstIndex int @@ -198,7 +201,16 @@ func init() { engine.OnFullMatch("更新vtb", zero.SuperUserPermission).SetBlock(true). Handle(func(ctx *zero.Ctx) { ctx.Send("少女祈祷中......") - vtbData() + db := model.Initialize(dbfile) + if db != nil { + for _, v := range db.GetVtbList() { + db.StoreVtb(v) + } + err := db.Close() + if err != nil { + log.Errorln("[vtb/cron]", err) + } + } ctx.Send("vtb数据库已更新") }) }