From 9b9fa6a616c0def17fa6a09b7274c1b5312641c6 Mon Sep 17 00:00:00 2001 From: liyp Date: Mon, 1 Apr 2024 11:37:03 +0800 Subject: [PATCH] fix pkg --- utils/router.go | 13 ++++++------- workers/pkg.go | 12 ++++++++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/utils/router.go b/utils/router.go index f227b96..a163a65 100644 --- a/utils/router.go +++ b/utils/router.go @@ -1,7 +1,6 @@ package utils import ( - "encoding/json" "fmt" "go-bot/workers" "strings" @@ -9,13 +8,13 @@ import ( func Router(data map[string]interface{}) { // 将map转换为格式化的JSON字符串 - jsonString, err := json.MarshalIndent(data, "", " ") - if err != nil { - panic(err) - } + // jsonString, err := json.MarshalIndent(data, "", " ") + // if err != nil { + // panic(err) + // } // 输出格式化后的JSON - fmt.Println(string(jsonString)) + // fmt.Println(string(jsonString)) // 读取字段值 uid := data["user_id"].(float64) gid := data["group_id"].(float64) @@ -28,7 +27,7 @@ func Router(data map[string]interface{}) { worker := workers.NewWorker(parms, fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg) // fmt.Println("router:", parms[0]) - fmt.Println(worker.CheckPermission()) + // fmt.Println(worker.CheckPermission()) // ans := NewStdAns(parms[0], fmt.Sprintf("%d", int(uid)), fmt.Sprintf("%d", int(gid)), role, fmt.Sprintf("%d", int(mid)), raw_msg) message := worker.CheckPermission() if message != "0" { diff --git a/workers/pkg.go b/workers/pkg.go index 6477dc5..32553ea 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -15,11 +15,11 @@ func (a *Pkg) GetMsg() string { if len(a.Parms) < 2 { return "请输入包名 如:/pkg linux 查询 linux 相关软件" } - url := "https://archlinux.org/packages/search/json/?name=" + a.Parms[0] + url := "https://archlinux.org/packages/search/json/?name=" + a.Parms[1] if len(a.Parms) > 2 { - url += "&arch=" + a.Parms[1] + url += "&arch=" + a.Parms[2] } - // fmt.Println("pkg url:", url) + fmt.Println("pkg url:", url) req, err := http.Get(url) if err != nil { return err.Error() @@ -37,7 +37,11 @@ func (a *Pkg) GetMsg() string { return err.Error() } // fmt.Println("pkg:", pkg) - result := pkg["results"].([]interface{})[0].(map[string]interface{}) + resultSlipe := pkg["results"].([]interface{}) + if len(resultSlipe) == 0 { + return "没有找到相关软件" + } + result := resultSlipe[0].(map[string]interface{}) fmt.Println("result:", result) if result != nil {