Skip to content

Commit 4cfa0de

Browse files
authored
fix: 云养猫插件 (#241)
1 parent 3f2af06 commit 4cfa0de

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

plugin/cybercat/catdata.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,51 @@ type catInfo struct {
6060
Picurl string // 猫猫图片
6161
}
6262

63-
func (inf *catInfo) avatar() string {
64-
nti, err := pool.NewNTImage(inf.Picurl)
65-
if err != nil {
66-
return inf.Picurl
67-
}
68-
return nti.String()
63+
func (c *catInfo) avatar(Gid int64) string {
64+
cache := "data/cybercat/cache" // 指定缓存路径
65+
cache = path.Join(engine.DataFolder(), "cache")
66+
imgname := fmt.Sprintf("%d_%d", c.User, Gid)
67+
imgfile := filepath.Join(cache, c.Type+imgname+".png")
68+
aimgfile := filepath.Join(file.BOTPATH, imgfile)
69+
70+
if _, err := os.Stat(cache); os.IsNotExist(err) {
71+
err := os.MkdirAll(cache, 0755)
72+
if err != nil {
73+
fmt.Println("Error creating cache directory:", err)
74+
return err.Error()
75+
}
76+
}
77+
if file.IsNotExist(aimgfile) {
78+
breed := c.Type
79+
data, err := web.GetData(apiURL + "search?has_breeds=" + breed)
80+
if err != nil {
81+
fmt.Println("Error fetching avatar URL:", err)
82+
return err.Error() // 返回错误信息
83+
}
84+
imgurl := gjson.ParseBytes(data).Get("0.url").String()
85+
imgdata, err := web.GetData(imgurl)
86+
if err != nil {
87+
return "错误:未能解析图片URL"
88+
}
89+
var f *os.File
90+
f, err = os.Create(aimgfile) // 使用 aimgfile 作为文件路径
91+
if err != nil {
92+
fmt.Println("Error creating file:", err)
93+
return err.Error() // 返回错误信息
94+
}
95+
defer f.Close()
96+
_, err = f.Write([]byte(imgdata)) // 写入图片数据
97+
if err != nil {
98+
fmt.Println("Error writing file:", err)
99+
return err.Error() // 返回错误信息
100+
}
101+
}
102+
return "file:///" + aimgfile // 返回文件协议的完整路径
69103
}
70104

71105
var (
72-
catdata = &catdb{
73-
db: &sql.Sqlite{},
106+
dbpath = "data/cybercat/catdata.db"
107+
catdata = &catdb{db: sql.New(dbpath),
74108
}
75109
engine = control.Register("cybercat", &ctrl.Options[*zero.Ctx]{
76110
DisableOnDefault: false,

plugin/cybercat/keepcat.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func init() {
157157
userInfo.LastTime = time.Now().Unix()
158158
userInfo.Mood += int(userInfo.Satiety)/5 - int(userInfo.Weight)/10
159159
userInfo = userInfo.settleOfData()
160+
avatarResult := userInfo.avatar(ctx.Event.GroupID)
160161
if err = catdata.insert(gidStr, &userInfo); err != nil {
161162
ctx.SendChain(message.Text("[ERROR]:", err))
162163
return
@@ -165,7 +166,7 @@ func init() {
165166
stauts = "完全没有饱"
166167
}
167168
ctx.SendChain(message.Reply(id), message.Text(userInfo.Name, "当前信息如下:\n"),
168-
message.Image(userInfo.avatar()),
169+
message.Image(avatarResult),
169170
message.Text("品种: "+userInfo.Type,
170171
"\n饱食度: ", strconv.FormatFloat(userInfo.Satiety, 'f', 0, 64),
171172
"\n心情: ", userInfo.Mood,

0 commit comments

Comments
 (0)