2
2
package score
3
3
4
4
import (
5
+ "encoding/base64"
6
+ "io"
5
7
"math"
6
8
"math/rand"
7
9
"os"
@@ -106,7 +108,7 @@ func init() {
106
108
// 如果签到时间是今天
107
109
ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("今天你已经签到过了!" ))
108
110
if file .IsExist (drawedFile ) {
109
- ctx . SendChain ( message . Image ( "file:///" + file . BOTPATH + "/" + drawedFile ) )
111
+ trySendImage ( drawedFile , ctx )
110
112
}
111
113
return
112
114
case siUpdateTimeStr != today :
@@ -176,7 +178,7 @@ func init() {
176
178
ctx .SendChain (message .Text ("ERROR: " , err ))
177
179
return
178
180
}
179
- ctx . SendChain ( message . Image ( "file:///" + file . BOTPATH + "/" + drawedFile ) )
181
+ trySendImage ( drawedFile , ctx )
180
182
})
181
183
182
184
engine .OnPrefix ("获得签到背景" , zero .OnlyGroup ).Limit (ctxext .LimitByGroup ).SetBlock (true ).
@@ -193,16 +195,14 @@ func init() {
193
195
ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("请先签到!" ))
194
196
return
195
197
}
196
- if id := ctx .SendChain (message .Image ("file:///" + file .BOTPATH + "/" + picFile )); id .ID () == 0 {
197
- ctx .SendChain (message .Text ("ERROR: 消息发送失败, 账号可能被风控" ))
198
- }
198
+ trySendImage (picFile , ctx )
199
199
})
200
200
engine .OnFullMatch ("查看等级排名" , zero .OnlyGroup ).Limit (ctxext .LimitByGroup ).SetBlock (true ).
201
201
Handle (func (ctx * zero.Ctx ) {
202
202
today := time .Now ().Format ("20060102" )
203
203
drawedFile := cachePath + today + "scoreRank.png"
204
204
if file .IsExist (drawedFile ) {
205
- ctx . SendChain ( message . Image ( "file:///" + file . BOTPATH + "/" + drawedFile ) )
205
+ trySendImage ( drawedFile , ctx )
206
206
return
207
207
}
208
208
st , err := sdb .GetScoreRankByTopN (10 )
@@ -267,7 +267,7 @@ func init() {
267
267
ctx .SendChain (message .Text ("ERROR: " , err ))
268
268
return
269
269
}
270
- ctx . SendChain ( message . Image ( "file:///" + file . BOTPATH + "/" + drawedFile ) )
270
+ trySendImage ( drawedFile , ctx )
271
271
})
272
272
engine .OnRegex (`^设置签到预设\s*(\d+)$` , zero .SuperUserPermission ).Limit (ctxext .LimitByUser ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
273
273
key := ctx .State ["regex_matched" ].([]string )[1 ]
@@ -342,3 +342,32 @@ func initPic(picFile string, uid int64) (avatar []byte, err error) {
342
342
}
343
343
return avatar , os .WriteFile (picFile , data , 0644 )
344
344
}
345
+
346
+ // 使用"file:"发送图片失败后,改用base64发送
347
+ func trySendImage (filePath string , ctx * zero.Ctx ) {
348
+ filePath = file .BOTPATH + "/" + filePath
349
+ if id := ctx .SendChain (message .Image ("file:///" + filePath )); id .ID () != 0 {
350
+ return
351
+ }
352
+ imgFile , err := os .Open (filePath )
353
+ if err != nil {
354
+ ctx .SendChain (message .Text ("ERROR: 无法打开文件" , err ))
355
+ return
356
+ }
357
+ defer imgFile .Close ()
358
+ // 使用 base64.NewEncoder 将文件内容编码为 base64 字符串
359
+ var encodedFileData strings.Builder
360
+ encodedFileData .WriteString ("base64://" )
361
+ encoder := base64 .NewEncoder (base64 .StdEncoding , & encodedFileData )
362
+ _ , err = io .Copy (encoder , imgFile )
363
+ if err != nil {
364
+ ctx .SendChain (message .Text ("ERROR: 无法编码文件内容" , err ))
365
+ return
366
+ }
367
+ encoder .Close ()
368
+ drawedFileBase64 := encodedFileData .String ()
369
+ if id := ctx .SendChain (message .Image (drawedFileBase64 )); id .ID () == 0 {
370
+ ctx .SendChain (message .Text ("ERROR: 无法读取图片文件" , err ))
371
+ return
372
+ }
373
+ }
0 commit comments