🎨 优化初始化逻辑

This commit is contained in:
源文雨 2022-03-07 13:40:32 +08:00
parent f38f3ab69c
commit 3911b5ed82
25 changed files with 58 additions and 67 deletions

4
go.mod
View File

@ -3,9 +3,9 @@ module github.com/FloatTech/ZeroBot-Plugin
go 1.17 go 1.17
require ( require (
github.com/FloatTech/AnimeAPI v1.3.1-0.20220306143604-2bcf039fe27a github.com/FloatTech/AnimeAPI v1.3.1-0.20220307053346-aa76aec4b635
github.com/FloatTech/sqlite v0.2.1 github.com/FloatTech/sqlite v0.2.1
github.com/FloatTech/zbputils v1.3.1-0.20220306143438-9fe5651df713 github.com/FloatTech/zbputils v1.3.1-0.20220307053255-29a0c0203693
github.com/antchfx/htmlquery v1.2.4 github.com/antchfx/htmlquery v1.2.4
github.com/corona10/goimagehash v1.0.3 github.com/corona10/goimagehash v1.0.3
github.com/fogleman/gg v1.3.0 github.com/fogleman/gg v1.3.0

8
go.sum
View File

@ -1,10 +1,10 @@
github.com/FloatTech/AnimeAPI v1.3.1-0.20220306143604-2bcf039fe27a h1:H0zAK0VwYBCM4V7QFhPH2p05hfaFG/hkPFxc1R6mmK8= github.com/FloatTech/AnimeAPI v1.3.1-0.20220307053346-aa76aec4b635 h1:T7J1wLvSqn8PvkM7X98dgbafW+zG1l0LdX664HbrG0U=
github.com/FloatTech/AnimeAPI v1.3.1-0.20220306143604-2bcf039fe27a/go.mod h1:qXQBNGfuCCuqMZnUsFv0KjgEYDozQwmwRZX9OKmzjBs= github.com/FloatTech/AnimeAPI v1.3.1-0.20220307053346-aa76aec4b635/go.mod h1:ZNhcnGEchvEjd09WbpLMlmxlmYy4trtLYEAJHRAoKaw=
github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ= github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ=
github.com/FloatTech/sqlite v0.2.1 h1:9t6Me48XJJCIoPy4nLRvcdhcVKfT0c2lilp7SEKROG8= github.com/FloatTech/sqlite v0.2.1 h1:9t6Me48XJJCIoPy4nLRvcdhcVKfT0c2lilp7SEKROG8=
github.com/FloatTech/sqlite v0.2.1/go.mod h1:6NfHRzqOo9RWeMJEoAQVuo51Omd5LFNxCNQhMF02/9U= github.com/FloatTech/sqlite v0.2.1/go.mod h1:6NfHRzqOo9RWeMJEoAQVuo51Omd5LFNxCNQhMF02/9U=
github.com/FloatTech/zbputils v1.3.1-0.20220306143438-9fe5651df713 h1:heSSdAa/gYHVHyYo5/KtFNJ9X9dD2vFLsS+dALrQCGQ= github.com/FloatTech/zbputils v1.3.1-0.20220307053255-29a0c0203693 h1:6OEcbIfzQ+9608zRBiR0x6VCrORncBNXfdvD1EWhqGk=
github.com/FloatTech/zbputils v1.3.1-0.20220306143438-9fe5651df713/go.mod h1:1nzt5KFkggpZaqkW0faY3y0X/qeMKTodqWc8wbVYhoY= github.com/FloatTech/zbputils v1.3.1-0.20220307053255-29a0c0203693/go.mod h1:1nzt5KFkggpZaqkW0faY3y0X/qeMKTodqWc8wbVYhoY=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= 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 h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0=
github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc/go.mod h1:OMmITAib6POA37xCichWM0aRnoVpSMZO1rB/G01wrr0= github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc/go.mod h1:OMmITAib6POA37xCichWM0aRnoVpSMZO1rB/G01wrr0=

42
kanban/banner.go Normal file
View File

@ -0,0 +1,42 @@
package kanban
import (
"fmt"
"strings"
"github.com/fumiama/go-registry"
)
var (
info = [...]string{
"* OneBot + ZeroBot + Golang",
"* Version 1.3.0 - 2022-02-09 14:31:34 +0800 CST",
"* Copyright © 2020 - 2021 FloatTech. All Rights Reserved.",
"* Project: https://github.com/FloatTech/ZeroBot-Plugin",
}
Banner = strings.Join(info[:], "\n")
reg = registry.NewRegReader("reilia.westeurope.cloudapp.azure.com:32664", "fumiama")
)
func PrintBanner() {
fmt.Print(
"\n======================[ZeroBot-Plugin]======================",
"\n", Banner, "\n",
"----------------------[ZeroBot-公告栏]----------------------",
"\n", Kanban(), "\n",
"============================================================\n\n",
)
}
func Kanban() string {
err := reg.Connect()
if err != nil {
return err.Error()
}
defer reg.Close()
text, err := reg.Get("ZeroBot-Plugin/kanban")
if err != nil {
return err.Error()
}
return text
}

5
kanban/init.go Normal file
View File

@ -0,0 +1,5 @@
package kanban
func init() {
PrintBanner()
}

44
main.go
View File

@ -5,9 +5,10 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"strings"
"time" "time"
"github.com/FloatTech/ZeroBot-Plugin/kanban" // 在最前打印 banner
// ---------以下插件均可通过前面加 // 注释,注释后停用并不加载插件--------- // // ---------以下插件均可通过前面加 // 注释,注释后停用并不加载插件--------- //
// ----------------------插件优先级按顺序从高到低---------------------- // // ----------------------插件优先级按顺序从高到低---------------------- //
// // // //
@ -140,9 +141,7 @@ import (
// // // //
// // // //
// -----------------------以下为内置依赖,勿动------------------------ // // -----------------------以下为内置依赖,勿动------------------------ //
"github.com/FloatTech/zbputils/control/order"
"github.com/FloatTech/zbputils/process" "github.com/FloatTech/zbputils/process"
"github.com/fumiama/go-registry"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/driver" "github.com/wdvxdr1123/ZeroBot/driver"
@ -151,19 +150,11 @@ import (
) )
var ( var (
contents = []string{
"* OneBot + ZeroBot + Golang",
"* Version 1.3.0 - 2022-02-09 14:31:34 +0800 CST",
"* Copyright © 2020 - 2021 FloatTech. All Rights Reserved.",
"* Project: https://github.com/FloatTech/ZeroBot-Plugin",
}
nicks = []string{"ATRI", "atri", "亚托莉", "アトリ"} nicks = []string{"ATRI", "atri", "亚托莉", "アトリ"}
banner = strings.Join(contents, "\n")
token *string token *string
url *string url *string
adana *string adana *string
prefix *string prefix *string
reg = registry.NewRegReader("reilia.westeurope.cloudapp.azure.com:32664", "fumiama")
) )
func init() { func init() {
@ -184,7 +175,7 @@ func init() {
flag.Parse() flag.Parse()
if *h { if *h {
printBanner() kanban.PrintBanner()
fmt.Println("Usage:") fmt.Println("Usage:")
flag.PrintDefaults() flag.PrintDefaults()
os.Exit(0) os.Exit(0)
@ -201,41 +192,16 @@ func init() {
// webctrl.InitGui(*g) // webctrl.InitGui(*g)
} }
func printBanner() {
fmt.Print(
"\n======================[ZeroBot-Plugin]======================",
"\n", banner, "\n",
"----------------------[ZeroBot-公告栏]----------------------",
"\n", getKanban(), "\n",
"============================================================\n",
)
}
func getKanban() string {
err := reg.Connect()
if err != nil {
return err.Error()
}
defer reg.Close()
text, err := reg.Get("ZeroBot-Plugin/kanban")
if err != nil {
return err.Error()
}
return text
}
func main() { func main() {
order.Wait()
printBanner()
rand.Seed(time.Now().UnixNano()) // 全局 seed其他插件无需再 seed rand.Seed(time.Now().UnixNano()) // 全局 seed其他插件无需再 seed
// 帮助 // 帮助
zero.OnFullMatchGroup([]string{"/help", ".help", "菜单"}, zero.OnlyToMe).SetBlock(true). zero.OnFullMatchGroup([]string{"/help", ".help", "菜单"}, zero.OnlyToMe).SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SendChain(message.Text(banner, "\n可发送\"/服务列表\"查看 bot 功能")) ctx.SendChain(message.Text(kanban.Banner, "\n可发送\"/服务列表\"查看 bot 功能"))
}) })
zero.OnFullMatch("查看zbp公告", zero.OnlyToMe, zero.AdminPermission).SetBlock(true). zero.OnFullMatch("查看zbp公告", zero.OnlyToMe, zero.AdminPermission).SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SendChain(message.Text(getKanban())) ctx.SendChain(message.Text(kanban.Kanban()))
}) })
zero.Run( zero.Run(
zero.Config{ zero.Config{

View File

@ -75,7 +75,6 @@ func init() {
go func() { go func() {
dbpath := en.DataFolder() dbpath := en.DataFolder()
dbfile := dbpath + "push.db" dbfile := dbpath + "push.db"
defer order.DoneOnExit()()
bdb = initialize(dbfile) bdb = initialize(dbfile)
log.Println("[bilibilipush]加载bilibilipush数据库") log.Println("[bilibilipush]加载bilibilipush数据库")
}() }()

View File

@ -23,7 +23,6 @@ func init() {
go func() { go func() {
dbpath := engine.DataFolder() dbpath := engine.DataFolder()
db.DBPath = dbpath + "bookreview.db" db.DBPath = dbpath + "bookreview.db"
defer order.DoneOnExit()()
// os.RemoveAll(dbpath) // os.RemoveAll(dbpath)
_, _ = file.GetLazyData(db.DBPath, false, true) _, _ = file.GetLazyData(db.DBPath, false, true)
err := db.Create("bookreview", &book{}) err := db.Create("bookreview", &book{})

View File

@ -21,7 +21,6 @@ func init() {
go func() { go func() {
dbpath := en.DataFolder() dbpath := en.DataFolder()
db.DBPath = dbpath + "cxh.db" db.DBPath = dbpath + "cxh.db"
defer order.DoneOnExit()()
// os.RemoveAll(dbpath) // os.RemoveAll(dbpath)
_, _ = file.GetLazyData(db.DBPath, false, true) _, _ = file.GetLazyData(db.DBPath, false, true)
err := db.Create("pinyin", &pinyin{}) err := db.Create("pinyin", &pinyin{})

View File

@ -26,7 +26,6 @@ func init() {
go func() { go func() {
dbpath := engine.DataFolder() dbpath := engine.DataFolder()
db.DBPath = dbpath + "cp.db" db.DBPath = dbpath + "cp.db"
defer order.DoneOnExit()()
// os.RemoveAll(dbpath) // os.RemoveAll(dbpath)
_, _ = file.GetLazyData(db.DBPath, false, true) _, _ = file.GetLazyData(db.DBPath, false, true)
err := db.Create("cp_story", &cpstory{}) err := db.Create("cp_story", &cpstory{})

View File

@ -29,7 +29,6 @@ func init() {
go func() { go func() {
dbpath := engine.DataFolder() dbpath := engine.DataFolder()
db.DBPath = dbpath + "curse.db" db.DBPath = dbpath + "curse.db"
defer order.DoneOnExit()()
_, err := file.GetLazyData(db.DBPath, false, true) _, err := file.GetLazyData(db.DBPath, false, true)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -26,7 +26,6 @@ func init() {
go func() { go func() {
datapath := engine.DataFolder() datapath := engine.DataFolder()
dbfile := datapath + "text.db" dbfile := datapath + "text.db"
defer order.DoneOnExit()()
data.LoadText(dbfile) data.LoadText(dbfile)
}() }()

View File

@ -7,7 +7,6 @@ import (
sql "github.com/FloatTech/sqlite" sql "github.com/FloatTech/sqlite"
binutils "github.com/FloatTech/zbputils/binary" binutils "github.com/FloatTech/zbputils/binary"
"github.com/FloatTech/zbputils/control/order"
"github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/file"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -21,7 +20,6 @@ type text struct {
// LoadText 加载小作文 // LoadText 加载小作文
func LoadText(dbfile string) { func LoadText(dbfile string) {
defer order.DoneOnExit()()
_, err := file.GetLazyData(dbfile, false, false) _, err := file.GetLazyData(dbfile, false, false)
db.DBPath = dbfile db.DBPath = dbfile
if err != nil { if err != nil {

View File

@ -59,7 +59,6 @@ func init() {
PublicDataFolder: "Fortune", PublicDataFolder: "Fortune",
}) })
go func() { go func() {
defer order.DoneOnExit()()
for i, s := range table { for i, s := range table {
index[s] = uint8(i) index[s] = uint8(i)
} }

View File

@ -34,7 +34,6 @@ func init() {
go func() { go func() {
dbpath := en.DataFolder() dbpath := en.DataFolder()
db.DBPath = dbpath + "jokes.db" db.DBPath = dbpath + "jokes.db"
defer order.DoneOnExit()()
_, err := file.GetLazyData(db.DBPath, false, true) _, err := file.GetLazyData(db.DBPath, false, true)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -32,7 +32,6 @@ func init() {
go func() { go func() {
dbpath := engine.DataFolder() dbpath := engine.DataFolder()
db.DBPath = dbpath + "pics.db" db.DBPath = dbpath + "pics.db"
defer order.DoneOnExit()()
_, _ = file.GetLazyData(db.DBPath, false, false) _, _ = file.GetLazyData(db.DBPath, false, false)
err := db.Create("picture", &picture{}) err := db.Create("picture", &picture{})
if err != nil { if err != nil {

View File

@ -66,7 +66,6 @@ func init() { // 插件主体
}) })
go func() { go func() {
defer order.DoneOnExit()()
db.DBPath = engine.DataFolder() + "config.db" db.DBPath = engine.DataFolder() + "config.db"
clock = timer.NewClock(db) clock = timer.NewClock(db)
err := db.Create("welcome", &welcome{}) err := db.Create("welcome", &welcome{})

View File

@ -33,7 +33,6 @@ func init() { // 插件主体
go func() { go func() {
dbpath := engine.DataFolder() dbpath := engine.DataFolder()
db.DBPath = dbpath + "kuji.db" db.DBPath = dbpath + "kuji.db"
defer order.DoneOnExit()()
_, _ = file.GetLazyData(db.DBPath, false, true) _, _ = file.GetLazyData(db.DBPath, false, true)
err := db.Create("kuji", &kuji{}) err := db.Create("kuji", &kuji{})
if err != nil { if err != nil {

View File

@ -3,13 +3,11 @@ package reborn
import ( import (
"encoding/json" "encoding/json"
"github.com/FloatTech/zbputils/control/order"
"github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/file"
) )
// load 加载rate数据 // load 加载rate数据
func load(area *rate, jsonfile string) error { func load(area *rate, jsonfile string) error {
defer order.DoneOnExit()()
data, err := file.GetLazyData(jsonfile, true, true) data, err := file.GetLazyData(jsonfile, true, true)
if err != nil { if err != nil {
return err return err

View File

@ -23,7 +23,6 @@ func init() {
go func() { go func() {
datapath := en.DataFolder() datapath := en.DataFolder()
jsonfile := datapath + "rate.json" jsonfile := datapath + "rate.json"
defer order.DoneOnExit()()
area := make(rate, 226) area := make(rate, 226)
err := load(&area, jsonfile) err := load(&area, jsonfile)
if err != nil { if err != nil {

View File

@ -43,7 +43,6 @@ func init() {
}) })
cachePath := engine.DataFolder() + "cache/" cachePath := engine.DataFolder() + "cache/"
go func() { go func() {
defer order.DoneOnExit()()
os.RemoveAll(cachePath) os.RemoveAll(cachePath)
err := os.MkdirAll(cachePath, 0755) err := os.MkdirAll(cachePath, 0755)
if err != nil { if err != nil {

View File

@ -62,7 +62,6 @@ func init() { // 插件主体
}) })
go func() { go func() {
defer order.DoneOnExit()()
// 如果数据库不存在则下载 // 如果数据库不存在则下载
pool.db.DBPath = engine.DataFolder() + "SetuTime.db" pool.db.DBPath = engine.DataFolder() + "SetuTime.db"
_, _ = fileutil.GetLazyData(pool.db.DBPath, false, false) _, _ = fileutil.GetLazyData(pool.db.DBPath, false, false)

View File

@ -23,7 +23,6 @@ func init() {
PublicDataFolder: "Chat", PublicDataFolder: "Chat",
}) })
go func() { go func() {
defer order.DoneOnExit()()
data, err := file.GetLazyData(engine.DataFolder()+"kimoi.json", true, true) data, err := file.GetLazyData(engine.DataFolder()+"kimoi.json", true, true)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -44,7 +44,6 @@ func init() {
dbfile := engine.DataFolder() + "vtb.db" dbfile := engine.DataFolder() + "vtb.db"
storePath := engine.DataFolder() + "store/" storePath := engine.DataFolder() + "store/"
go func() { go func() {
defer order.DoneOnExit()()
err := os.MkdirAll(storePath, 0755) err := os.MkdirAll(storePath, 0755)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -73,7 +73,6 @@ func init() {
}), }),
)) ))
go func() { go func() {
defer order.DoneOnExit()()
for i := 5; i <= 7; i++ { for i := 5; i <= 7; i++ {
dc, err := file.GetLazyData(fmt.Sprintf("%scet-4_%d.txt", en.DataFolder(), i), true, true) dc, err := file.GetLazyData(fmt.Sprintf("%scet-4_%d.txt", en.DataFolder(), i), true, true)
if err != nil { if err != nil {

View File

@ -20,7 +20,6 @@ func init() {
}) })
dbfile := engine.DataFolder() + "ymgal.db" dbfile := engine.DataFolder() + "ymgal.db"
go func() { go func() {
defer order.DoneOnExit()()
_, _ = file.GetLazyData(dbfile, false, false) _, _ = file.GetLazyData(dbfile, false, false)
gdb = initialize(dbfile) gdb = initialize(dbfile)
}() }()