package workers import ( "fmt" "sort" "strconv" "time" "github.com/imroc/req/v3" ) func init() { RegisterWorkerFactory("mner", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { return &Mner{ StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), } }) } type Mner struct { *StdAns } func (a *Mner) GetMsg() string { if len(a.Parms) < 2 { return "请输入AUR的维护者 如:/mner user 查询 user 用户维护的包" } user := a.Parms[1] client := req.C().SetTimeout(time.Second * 10) url := "https://aur.archlinux.org/rpc/v5/search/" + user + "?by=maintainer" var result map[string]interface{} resp, err := client.R(). SetSuccessResult(&result). Get(url) if err != nil { return "网络错误!" } defer resp.Body.Close() results := result["results"].([]interface{}) if len(results) == 0 { return "未找到该维护者维护的包" } msg := "该维护者 " + a.Parms[1] + " 共维护了 " + strconv.FormatInt(int64(result["resultcount"].(float64)), 10) + " 个包:\n" // 将结果按NumVotes排序 sort.Slice(results, func(i, j int) bool { return results[i].(map[string]interface{})["NumVotes"].(float64) > results[j].(map[string]interface{})["NumVotes"].(float64) }) // 只显示NumVotes最大的5个包 for i, v := range results { if i >= 5 { break } pkg := v.(map[string]interface{}) msg += fmt.Sprintf("包名:%s\n版本:%s\n描述:%s\n票数:%d\n\n", pkg["Name"], pkg["Version"], pkg["Description"], int(pkg["NumVotes"].(float64))) } return msg }