[issues 840] Modify the "score" of the Image send type add base64 (#853)

Modify the "score":Image send type add base64
适配shamrock,以路径发送图片失败后,使用base64发送图片。
This commit is contained in:
vatebur 2024-01-25 16:44:06 +08:00 committed by GitHub
parent 9c65383a7c
commit b8858f0acd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,8 @@
package score package score
import ( import (
"encoding/base64"
"io"
"math" "math"
"math/rand" "math/rand"
"os" "os"
@ -106,7 +108,7 @@ func init() {
// 如果签到时间是今天 // 如果签到时间是今天
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("今天你已经签到过了!")) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("今天你已经签到过了!"))
if file.IsExist(drawedFile) { if file.IsExist(drawedFile) {
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile)) trySendImage(drawedFile, ctx)
} }
return return
case siUpdateTimeStr != today: case siUpdateTimeStr != today:
@ -176,7 +178,7 @@ func init() {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
} }
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile)) trySendImage(drawedFile, ctx)
}) })
engine.OnPrefix("获得签到背景", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true). engine.OnPrefix("获得签到背景", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
@ -193,16 +195,14 @@ func init() {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请先签到!")) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请先签到!"))
return return
} }
if id := ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + picFile)); id.ID() == 0 { trySendImage(picFile, ctx)
ctx.SendChain(message.Text("ERROR: 消息发送失败, 账号可能被风控"))
}
}) })
engine.OnFullMatch("查看等级排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true). engine.OnFullMatch("查看等级排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
today := time.Now().Format("20060102") today := time.Now().Format("20060102")
drawedFile := cachePath + today + "scoreRank.png" drawedFile := cachePath + today + "scoreRank.png"
if file.IsExist(drawedFile) { if file.IsExist(drawedFile) {
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile)) trySendImage(drawedFile, ctx)
return return
} }
st, err := sdb.GetScoreRankByTopN(10) st, err := sdb.GetScoreRankByTopN(10)
@ -267,7 +267,7 @@ func init() {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
} }
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile)) trySendImage(drawedFile, ctx)
}) })
engine.OnRegex(`^设置签到预设\s*(\d+)$`, zero.SuperUserPermission).Limit(ctxext.LimitByUser).SetBlock(true).Handle(func(ctx *zero.Ctx) { engine.OnRegex(`^设置签到预设\s*(\d+)$`, zero.SuperUserPermission).Limit(ctxext.LimitByUser).SetBlock(true).Handle(func(ctx *zero.Ctx) {
key := ctx.State["regex_matched"].([]string)[1] key := ctx.State["regex_matched"].([]string)[1]
@ -342,3 +342,32 @@ func initPic(picFile string, uid int64) (avatar []byte, err error) {
} }
return avatar, os.WriteFile(picFile, data, 0644) return avatar, os.WriteFile(picFile, data, 0644)
} }
// 使用"file:"发送图片失败后改用base64发送
func trySendImage(filePath string, ctx *zero.Ctx) {
filePath = file.BOTPATH + "/" + filePath
if id := ctx.SendChain(message.Image("file:///" + filePath)); id.ID() != 0 {
return
}
imgFile, err := os.Open(filePath)
if err != nil {
ctx.SendChain(message.Text("ERROR: 无法打开文件", err))
return
}
defer imgFile.Close()
// 使用 base64.NewEncoder 将文件内容编码为 base64 字符串
var encodedFileData strings.Builder
encodedFileData.WriteString("base64://")
encoder := base64.NewEncoder(base64.StdEncoding, &encodedFileData)
_, err = io.Copy(encoder, imgFile)
if err != nil {
ctx.SendChain(message.Text("ERROR: 无法编码文件内容", err))
return
}
encoder.Close()
drawedFileBase64 := encodedFileData.String()
if id := ctx.SendChain(message.Image(drawedFileBase64)); id.ID() == 0 {
ctx.SendChain(message.Text("ERROR: 无法读取图片文件", err))
return
}
}