diff --git a/.golangci.yml b/.golangci.yml index 9fb87591..53e95137 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,6 @@ linters-settings: errcheck: - ignore: fmt:.*,io/ioutil:^Read.* + ignore: fmt:.* ignoretests: true goimports: diff --git a/README.md b/README.md index 3dd5b388..0a1cf31c 100644 --- a/README.md +++ b/README.md @@ -250,7 +250,6 @@ print("run[CQ:image,file="+j["img"]+"]") - **bilibili** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/bilibili"` - [x] >vup info [名字 | uid] - [x] >user info [名字 | uid] - - [x] /开启粉丝日报 - **嘉然** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/diana"` - [x] 小作文 - [x] 发大病 diff --git a/plugin/bilibili/fansDaily.go b/plugin/bilibili/fansDaily.go deleted file mode 100644 index b4a54e4d..00000000 --- a/plugin/bilibili/fansDaily.go +++ /dev/null @@ -1,165 +0,0 @@ -package bilibili - -import ( - "encoding/json" - "net/http" - "time" - - control "github.com/FloatTech/zbputils/control" - "github.com/FloatTech/zbputils/process" - zero "github.com/wdvxdr1123/ZeroBot" - "github.com/wdvxdr1123/ZeroBot/message" - - "github.com/FloatTech/zbputils/control/order" -) - -type follower struct { - Mid int `json:"mid"` - Uname string `json:"uname"` - Video int `json:"video"` - Roomid int `json:"roomid"` - Rise int `json:"rise"` - Follower int `json:"follower"` - GuardNum int `json:"guardNum"` - AreaRank int `json:"areaRank"` -} - -// 开启日报推送 -func init() { - fansDaily() - en := control.Register("fansdaily", order.AcquirePrio(), &control.Options{ - DisableOnDefault: true, - Help: "fansdaily\n- /开启粉丝日报\n- /关闭粉丝日报", - }) - zero.OnCommand("开启粉丝日报", zero.AdminPermission).SetBlock(true). - Handle(func(ctx *zero.Ctx) { - m, ok := control.Lookup("fansdaily") - if ok { - if m.IsEnabledIn(ctx.Event.GroupID) { - ctx.Send(message.Text("已启用!")) - } else { - m.Enable(ctx.Event.GroupID) - ctx.Send(message.Text("添加成功!")) - } - } else { - ctx.Send(message.Text("找不到该服务!")) - } - }) - en.OnCommand("关闭粉丝日报", zero.AdminPermission).SetBlock(true). - Handle(func(ctx *zero.Ctx) { - m, ok := control.Lookup("fansdaily") - if ok { - if m.IsEnabledIn(ctx.Event.GroupID) { - m.Disable(ctx.Event.GroupID) - ctx.Send(message.Text("关闭成功!")) - } else { - ctx.Send(message.Text("未启用!")) - } - } else { - ctx.Send(message.Text("找不到该服务!")) - } - }) -} - -// 定时任务每天晚上最后2分钟执行一次 -func fansDaily() { - _, err := process.CronTab.AddFunc("58 23 * * *", func() { sendNotice() }) - if err != nil { - panic(err) - } -} - -// 获取数据拼接消息链并发送 -func getMsg() message.MessageSegment { - var ( - diana = fansapi("672328094") - ava = fansapi("672346917") - eileen = fansapi("672342685") - bella = fansapi("672353429") - carol = fansapi("351609538") - ) - return message.Text( - time.Now().Format("2006-01-02"), " Asoul全团粉丝日报如下", "\n\n", - "uid: ", diana.Mid, "\n", - "名字: ", diana.Uname, "\n", - "当前粉丝数: ", diana.Follower, "\n", - "今日涨粉数: ", diana.Rise, "\n", - "视频投稿数: ", diana.Video, "\n", - "直播间id: ", diana.Roomid, "\n", - "舰队: ", diana.GuardNum, "\n", - "直播总排名: ", diana.AreaRank, "\n", - "数据来源: ", "https://vtbs.moe/detail/", "672328094", "\n\n", - - "uid: ", ava.Mid, "\n", - "名字: ", ava.Uname, "\n", - "当前粉丝数: ", ava.Follower, "\n", - "今日涨粉数: ", ava.Rise, "\n", - "视频投稿数: ", ava.Video, "\n", - "直播间id: ", ava.Roomid, "\n", - "舰队: ", ava.GuardNum, "\n", - "直播总排名: ", ava.AreaRank, "\n", - "数据来源: ", "https://vtbs.moe/detail/", "672346917", "\n\n", - - "uid: ", eileen.Mid, "\n", - "名字: ", eileen.Uname, "\n", - "当前粉丝数: ", eileen.Follower, "\n", - "今日涨粉数: ", eileen.Rise, "\n", - "视频投稿数: ", eileen.Video, "\n", - "直播间id: ", eileen.Roomid, "\n", - "舰队: ", eileen.GuardNum, "\n", - "直播总排名: ", eileen.AreaRank, "\n", - "数据来源: ", "https://vtbs.moe/detail/", "672342685", "\n\n", - - "uid: ", bella.Mid, "\n", - "名字: ", bella.Uname, "\n", - "当前粉丝数: ", bella.Follower, "\n", - "今日涨粉数: ", bella.Rise, "\n", - "视频投稿数: ", bella.Video, "\n", - "直播间id: ", bella.Roomid, "\n", - "舰队: ", bella.GuardNum, "\n", - "直播总排名: ", bella.AreaRank, "\n", - "数据来源: ", "https://vtbs.moe/detail/", "672353429", "\n\n", - - "uid: ", carol.Mid, "\n", - "名字: ", carol.Uname, "\n", - "当前粉丝数: ", carol.Follower, "\n", - "今日涨粉数: ", carol.Rise, "\n", - "视频投稿数: ", carol.Video, "\n", - "直播间id: ", carol.Roomid, "\n", - "舰队: ", carol.GuardNum, "\n", - "直播总排名: ", carol.AreaRank, "\n", - "数据来源: ", "https://vtbs.moe/detail/", "351609538", - ) -} - -// 获取数据拼接消息链并发送 -func sendNotice() { - m, ok := control.Lookup("fansdaily") - if ok { - msg := getMsg() - zero.RangeBot(func(id int64, ctx *zero.Ctx) bool { - for _, g := range ctx.GetGroupList().Array() { - grp := g.Get("group_id").Int() - if m.IsEnabledIn(grp) { - ctx.SendGroupMessage(grp, msg) - } - } - return true - }) - } -} - -// 请求api -func fansapi(uid string) *follower { - url := "https://api.vtbs.moe/v1/detail/" + uid - resp, err := http.Get(url) - if err != nil { - panic(err) - } - defer resp.Body.Close() - result := &follower{} - if err := json.NewDecoder(resp.Body).Decode(result); err != nil { - panic(err) - } - return result -} diff --git a/plugin/bilibili/info.go b/plugin/bilibili/info.go index e3d8c368..bc517312 100644 --- a/plugin/bilibili/info.go +++ b/plugin/bilibili/info.go @@ -2,7 +2,7 @@ package bilibili import ( - "io/ioutil" + "io" "net/http" control "github.com/FloatTech/zbputils/control" @@ -47,7 +47,7 @@ func init() { } defer res.Body.Close() - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return diff --git a/plugin/bilibili/live_info.go b/plugin/bilibili/live_info.go index a8f90ae3..f0769a22 100644 --- a/plugin/bilibili/live_info.go +++ b/plugin/bilibili/live_info.go @@ -1,11 +1,12 @@ package bilibili import ( + "encoding/json" "errors" - "io/ioutil" "net/http" "time" + "github.com/FloatTech/zbputils/web" "github.com/tidwall/gjson" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" @@ -23,37 +24,62 @@ func init() { } id := res.Get("data.result.0.mid").String() // 获取详情 - json := fansapi(id) + fo, err := fansapi(id) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } ctx.SendChain(message.Text( - "uid: ", json.Mid, "\n", - "名字: ", json.Uname, "\n", - "当前粉丝数: ", json.Follower, "\n", - "24h涨粉数: ", json.Rise, "\n", - "视频投稿数: ", json.Video, "\n", - "直播间id: ", json.Roomid, "\n", - "舰队: ", json.GuardNum, "\n", - "直播总排名: ", json.AreaRank, "\n", - "数据来源: ", "https://vtbs.moe/detail/", json.Mid, "\n", + "uid: ", fo.Mid, "\n", + "名字: ", fo.Uname, "\n", + "当前粉丝数: ", fo.Follower, "\n", + "24h涨粉数: ", fo.Rise, "\n", + "视频投稿数: ", fo.Video, "\n", + "直播间id: ", fo.Roomid, "\n", + "舰队: ", fo.GuardNum, "\n", + "直播总排名: ", fo.AreaRank, "\n", + "数据来源: ", "https://vtbs.moe/detail/", fo.Mid, "\n", "数据获取时间: ", time.Now().Format("2006-01-02 15:04:05"), )) }) } // 搜索api:通过把触发指令传入的昵称找出uid返回 -func uid(keyword string) (gjson.Result, error) { +func uid(keyword string) (*gjson.Result, error) { api := "http://api.bilibili.com/x/web-interface/search/type?search_type=bili_user&&user_type=1&keyword=" + keyword - resp, err := http.Get(api) + data, err := web.GetData(api) if err != nil { - return gjson.Result{}, err + return nil, err } - if resp.StatusCode != http.StatusOK { - return gjson.Result{}, errors.New("code not 200") - } - data, _ := ioutil.ReadAll(resp.Body) - resp.Body.Close() json := gjson.ParseBytes(data) if json.Get("data.numResults").Int() == 0 { - return gjson.Result{}, errors.New("查无此人") + return nil, errors.New("查无此人") } - return json, nil + return &json, nil +} + +type follower struct { + Mid int `json:"mid"` + Uname string `json:"uname"` + Video int `json:"video"` + Roomid int `json:"roomid"` + Rise int `json:"rise"` + Follower int `json:"follower"` + GuardNum int `json:"guardNum"` + AreaRank int `json:"areaRank"` +} + +// 请求api +func fansapi(uid string) (*follower, error) { + url := "https://api.vtbs.moe/v1/detail/" + uid + resp, err := http.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + result := &follower{} + if err := json.NewDecoder(resp.Body).Decode(result); err != nil { + return nil, err + } + return result, nil } diff --git a/plugin/github/repo_searcher.go b/plugin/github/repo_searcher.go index 4552b3ef..97f17e4c 100644 --- a/plugin/github/repo_searcher.go +++ b/plugin/github/repo_searcher.go @@ -4,7 +4,7 @@ package github import ( "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -119,7 +119,7 @@ func netGet(dest string, header http.Header) ([]byte, error) { return nil, err } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } diff --git a/plugin/inject/main.go b/plugin/inject/main.go new file mode 100644 index 00000000..6ea3bd26 --- /dev/null +++ b/plugin/inject/main.go @@ -0,0 +1,23 @@ +// Package inject 注入指令 +package inject + +import ( + "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/control/order" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" +) + +func init() { + en := control.Register("inject", order.AcquirePrio(), &control.Options{ + DisableOnDefault: false, + Help: "注入指令\n" + + "- run[CQ码]", + }) + // 运行 CQ 码 + en.OnPrefix("run", zero.SuperUserPermission).SetBlock(true). + Handle(func(ctx *zero.Ctx) { + // 可注入,权限为主人 + ctx.Send(message.UnescapeCQCodeText(ctx.State["args"].(string))) + }) +} diff --git a/plugin/lolicon/lolicon.go b/plugin/lolicon/lolicon.go index 7fcd3a64..044a3863 100644 --- a/plugin/lolicon/lolicon.go +++ b/plugin/lolicon/lolicon.go @@ -2,8 +2,6 @@ package lolicon import ( - "io/ioutil" - "net/http" "strings" "time" @@ -16,6 +14,7 @@ import ( "github.com/FloatTech/zbputils/img/pool" "github.com/FloatTech/zbputils/math" "github.com/FloatTech/zbputils/process" + "github.com/FloatTech/zbputils/web" "github.com/FloatTech/zbputils/control/order" ) @@ -38,17 +37,11 @@ func init() { Handle(func(ctx *zero.Ctx) { go func() { for i := 0; i < math.Min(cap(queue)-len(queue), 2); i++ { - resp, err := http.Get(api) + data, err := web.GetData(api) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) continue } - if resp.StatusCode != http.StatusOK { - ctx.SendChain(message.Text("ERROR: code ", resp.StatusCode)) - continue - } - data, _ := ioutil.ReadAll(resp.Body) - resp.Body.Close() json := gjson.ParseBytes(data) if e := json.Get("error").Str; e != "" { ctx.SendChain(message.Text("ERROR: ", e)) diff --git a/plugin/manager/manager.go b/plugin/manager/manager.go index ce913587..2543d6e9 100644 --- a/plugin/manager/manager.go +++ b/plugin/manager/manager.go @@ -509,12 +509,6 @@ func init() { // 插件主体 } ctx.SendChain(message.Text("找不到服务!")) }) - // 运行 CQ 码 - engine.OnRegex(`^run(.*)$`, zero.SuperUserPermission).SetBlock(true). - Handle(func(ctx *zero.Ctx) { - // 可注入,权限为主人 - ctx.Send(message.UnescapeCQCodeText(ctx.State["regex_matched"].([]string)[1])) - }) // 根据 gist 自动同意加群 // 加群请在github新建一个gist,其文件名为本群群号的字符串的md5(小写),内容为一行,是当前unix时间戳(10分钟内有效)。 // 然后请将您的用户名和gist哈希(小写)按照username/gisthash的格式填写到回答即可。 diff --git a/plugin/music/selecter.go b/plugin/music/selecter.go index 64db14d3..0dafc083 100644 --- a/plugin/music/selecter.go +++ b/plugin/music/selecter.go @@ -5,7 +5,7 @@ import ( "crypto/md5" "encoding/hex" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -231,7 +231,7 @@ func netGet(url string, header http.Header) []byte { return nil } defer res.Body.Close() - result, _ := ioutil.ReadAll(res.Body) + result, _ := io.ReadAll(res.Body) return result } @@ -245,6 +245,6 @@ func netPost(url string, data url.Values, header http.Header) []byte { return nil } defer res.Body.Close() - result, _ := ioutil.ReadAll(res.Body) + result, _ := io.ReadAll(res.Body) return result } diff --git a/plugin/nbnhhsh/nbnhhsh.go b/plugin/nbnhhsh/nbnhhsh.go index ca46a816..e65be351 100644 --- a/plugin/nbnhhsh/nbnhhsh.go +++ b/plugin/nbnhhsh/nbnhhsh.go @@ -2,7 +2,7 @@ package nbnhhsh import ( - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -31,7 +31,7 @@ func getValue(text string) []string { urlValues.Add("text", text) resp, err := http.PostForm("https://lab.magiconch.com/api/nbnhhsh/guess", urlValues) if err == nil { - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err == nil { resp.Body.Close() json := gjson.ParseBytes(body) diff --git a/plugin/novel/qianbi.go b/plugin/novel/qianbi.go index e594a630..5de16299 100644 --- a/plugin/novel/qianbi.go +++ b/plugin/novel/qianbi.go @@ -3,7 +3,7 @@ package novel import ( "fmt" - "io/ioutil" + "io" "net/http" "net/http/cookiejar" "net/url" @@ -164,7 +164,7 @@ func search(searchKey string) (searchHTML string) { log.Errorln("[novel]", err) } defer searchResp.Body.Close() - searchData, err := ioutil.ReadAll(searchResp.Body) + searchData, err := io.ReadAll(searchResp.Body) if err != nil { log.Errorf("[novel] get response for url=%s got error=%s\n", searchURL, err.Error()) } diff --git a/plugin/runcode/code_runner.go b/plugin/runcode/code_runner.go index 3507b5d6..a13c48f6 100644 --- a/plugin/runcode/code_runner.go +++ b/plugin/runcode/code_runner.go @@ -3,7 +3,7 @@ package runcode import ( "errors" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -181,7 +181,7 @@ func runCode(code string, runType [2]string) (string, error) { if body.StatusCode != http.StatusOK { return "", errors.New("code not 200") } - res, err := ioutil.ReadAll(body.Body) + res, err := io.ReadAll(body.Body) if err != nil { return "", err } diff --git a/plugin/translation/tl.go b/plugin/translation/tl.go index 9722aff4..02981e30 100644 --- a/plugin/translation/tl.go +++ b/plugin/translation/tl.go @@ -2,11 +2,6 @@ package translation import ( - "errors" - "fmt" - "io/ioutil" - "net/http" - "github.com/tidwall/gjson" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" @@ -14,29 +9,11 @@ import ( control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" "github.com/FloatTech/zbputils/process" + "github.com/FloatTech/zbputils/web" "github.com/FloatTech/zbputils/control/order" ) -func tl(d string) ([]byte, error) { - url := "https://api.cloolc.club/fanyi?data=" + d - resp, err := http.Get(url) - if err != nil { - fmt.Println(err) - } - data, err := ioutil.ReadAll(resp.Body) - _ = resp.Body.Close() - if err != nil { - return nil, err - } - if code := resp.StatusCode; code != 200 { - // 如果返回不是200则立刻抛出错误 - errmsg := fmt.Sprintf("code %d", code) - return nil, errors.New(errmsg) - } - return data, err -} - func init() { control.Register("translation", order.AcquirePrio(), &control.Options{ DisableOnDefault: false, @@ -45,11 +22,11 @@ func init() { }).OnRegex(`^>TL\s(-.{1,10}? )?(.*)$`).SetBlock(true).Limit(ctxext.LimitByUser). Handle(func(ctx *zero.Ctx) { msg := []string{ctx.State["regex_matched"].([]string)[2]} - rely, err := tl(msg[0]) + data, err := web.GetData("https://api.cloolc.club/fanyi?data=" + msg[0]) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) } - info := gjson.ParseBytes(rely) + info := gjson.ParseBytes(data) repo := info.Get("data.0") process.SleepAbout1sTo2s() ctx.SendChain(message.Text(repo.Get("value.0"))) diff --git a/plugin/vtb_quotation/model/model.go b/plugin/vtb_quotation/model/model.go index 852177ab..24e94c01 100644 --- a/plugin/vtb_quotation/model/model.go +++ b/plugin/vtb_quotation/model/model.go @@ -2,7 +2,7 @@ package model import ( - "io/ioutil" + "io" "math/rand" "net/http" "os" @@ -199,7 +199,7 @@ func (vdb *VtbDB) GetVtbList() (uidList []string) { } defer resp.Body.Close() - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { log.Errorln(err) return @@ -262,7 +262,7 @@ func (vdb *VtbDB) StoreVtb(uid string) { } defer resp.Body.Close() - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) if err != nil { log.Errorln(err) return diff --git a/plugin/wtf/model.go b/plugin/wtf/model.go index 10b7abd4..88a02d2b 100644 --- a/plugin/wtf/model.go +++ b/plugin/wtf/model.go @@ -3,9 +3,9 @@ package wtf import ( "encoding/json" "errors" - "io" - "net/http" "net/url" + + "github.com/FloatTech/zbputils/web" ) /* JS path getter for https://wtf.hiigara.net/ranking @@ -134,12 +134,7 @@ func (w *wtf) predict(names ...string) (string, error) { name += "/" + url.QueryEscape(n) } u := apiprefix + w.path + name - resp, err := http.Get(u) - if err != nil { - return "", err - } - r, err := io.ReadAll(resp.Body) - resp.Body.Close() + r, err := web.GetData(u) if err != nil { return "", err }