MCPcopy Create free account
hub / github.com/FloatTech/ZeroBot-Plugin-Playground / avatar

Method avatar

plugin/cybercat/catdata.go:80–140  ·  view source on GitHub ↗
(gid int64)

Source from the content-addressed store, hash-verified

78}
79
80func (inf *catInfo) avatar(gid int64) string {
81 // 构建缓存文件路径
82 cache := path.Join(engine.DataFolder(), "cache")
83 imgname := fmt.Sprintf("%d_%d", inf.User, gid)
84 imgfile := filepath.Join(cache, inf.Type+imgname+".png")
85 aimgfile := filepath.Join(file.BOTPATH, imgfile)
86
87 // 确保缓存目录存在
88 _ = os.MkdirAll(cache, 0755)
89
90 // 1. 如果本地已有缓存文件,直接返回
91 if file.IsExist(aimgfile) {
92 return "file:///" + aimgfile
93 }
94
95 // 2. 优先使用已有的 Picurl(例如用户上传的猫娘图片)
96 if inf.Picurl != "" {
97 if err := downloadAndSave(inf.Picurl, aimgfile); err == nil {
98 return "file:///" + aimgfile
99 }
100 // 下载失败,继续尝试通过品种获取
101 }
102
103 // 3. 通过品种ID获取图片
104 breedID, ok := typeZH2Breeds[inf.Type]
105 if ok {
106 // 构建请求URL(使用 breed_ids 参数)
107 url := apiURL + "search?breed_ids=" + breedID + "&limit=1"
108 // 添加 API 密钥
109 if apiKey := getCatAPIKey(); apiKey != "" {
110 url += "&api_key=" + apiKey
111 }
112
113 data, err := web.GetData(url)
114 if err == nil {
115 imgURL := gjson.ParseBytes(data).Get("0.url").String()
116 if imgURL != "" {
117 if err := downloadAndSave(imgURL, aimgfile); err == nil {
118 // 保存 URL 到数据库供下次使用
119 inf.Picurl = imgURL
120 go func() {
121 gidStr := "group" + strconv.FormatInt(gid, 10)
122 _ = catdata.insert(gidStr, inf) // 异步更新,忽略错误
123 }()
124 return "file:///" + aimgfile
125 }
126 }
127 }
128 }
129
130 // 4. 后备方案:随机猫图
131 result := suineko()
132 if result.Err == nil && result.ImageURL != "" {
133 if err := downloadAndSave(result.ImageURL, aimgfile); err == nil {
134 return "file:///" + aimgfile
135 }
136 }
137

Callers 1

initFunction · 0.80

Calls 4

downloadAndSaveFunction · 0.85
getCatAPIKeyFunction · 0.85
suinekoFunction · 0.85
insertMethod · 0.45

Tested by

no test coverage detected