@@ -32,11 +32,10 @@ type pigImage struct {
3232
3333var (
3434 pigCache []pigImage
35- pigMap = make (map [string ]* pigImage ) // 使用 map 方便 ID 查找
35+ pigMap = make (map [string ]* pigImage )
3636 pigMutex sync.RWMutex
37- lastUpdateTime time.Time
37+ lastUpdateTime = time .Now ()
3838
39- // 初始化 engine
4039 engine = control .AutoRegister (& ctrl.Options [* zero.Ctx ]{
4140 DisableOnDefault : false ,
4241 Brief : "来份猪猪" ,
4645)
4746
4847func init () {
48+ _ = checkAndUpdateData ()
4949 // 1. 随机猪猪
5050 engine .OnRegex (`^(随机猪猪|来份猪猪|抽个猪猪)$` ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
5151 if err := checkAndUpdateData (); err != nil {
@@ -70,7 +70,7 @@ func init() {
7070
7171 ctx .SendChain (
7272 message .Text (fmt .Sprintf ("🐷 ID: %s | %s" , target .ID , target .Title )),
73- message .ImageBytes (imgData ), // 直接使用 ImageBytes,无需 base64
73+ message .ImageBytes (imgData ),
7474 )
7575 })
7676
@@ -136,7 +136,6 @@ func init() {
136136 pigMutex .RLock ()
137137 defer pigMutex .RUnlock ()
138138
139- // 直接使用 map 进行 O(1) 查找,抛弃 for 循环
140139 target , exists := pigMap [targetID ]
141140 if ! exists {
142141 ctx .SendChain (message .Text ("[Pig] ERROR: 未找到 ID 为 " , targetID , " 的猪猪" ))
@@ -151,7 +150,7 @@ func init() {
151150
152151 ctx .SendChain (
153152 message .Text (fmt .Sprintf ("🐷 ID: %s | %s" , target .ID , target .Title )),
154- message .ImageBytes (imgData ), // 直接使用 ImageBytes
153+ message .ImageBytes (imgData ),
155154 )
156155 })
157156}
@@ -193,15 +192,13 @@ func checkAndUpdateData() error {
193192 return nil
194193}
195194
196- // fetch 作为 pigImage 的专属方法,直接返回 []byte
197195func (img * pigImage ) fetch () ([]byte , error ) {
198196 if img .Filename == "" {
199197 return nil , errors .New ("图片数据异常,缺少文件名" )
200198 }
201199
202200 targetPath := filepath .Join ("assets" , img .Filename )
203201
204- // 使用 true,直接返回字节数组
205202 imgData , err := engine .GetLazyData (targetPath , true )
206203 if err != nil {
207204 return nil , errors .New ("图片资源缺失 (" + targetPath + "): " + err .Error ())
0 commit comments