Skip to content

Commit

Permalink
[issues 840] Modify the "score" of the Image send type add base64 (#853)
Browse files Browse the repository at this point in the history
Modify the "score":Image send type add base64
适配shamrock,以路径发送图片失败后,使用base64发送图片。
  • Loading branch information
vatebur committed Jan 25, 2024
1 parent 9c65383 commit b8858f0
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions plugin/score/sign_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package score

import (
"encoding/base64"
"io"
"math"
"math/rand"
"os"
Expand Down Expand Up @@ -106,7 +108,7 @@ func init() {
// 如果签到时间是今天
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("今天你已经签到过了!"))
if file.IsExist(drawedFile) {
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
trySendImage(drawedFile, ctx)
}
return
case siUpdateTimeStr != today:
Expand Down Expand Up @@ -176,7 +178,7 @@ func init() {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
trySendImage(drawedFile, ctx)
})

engine.OnPrefix("获得签到背景", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
Expand All @@ -193,16 +195,14 @@ func init() {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请先签到!"))
return
}
if id := ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + picFile)); id.ID() == 0 {
ctx.SendChain(message.Text("ERROR: 消息发送失败, 账号可能被风控"))
}
trySendImage(picFile, ctx)
})
engine.OnFullMatch("查看等级排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
today := time.Now().Format("20060102")
drawedFile := cachePath + today + "scoreRank.png"
if file.IsExist(drawedFile) {
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
trySendImage(drawedFile, ctx)
return
}
st, err := sdb.GetScoreRankByTopN(10)
Expand Down Expand Up @@ -267,7 +267,7 @@ func init() {
ctx.SendChain(message.Text("ERROR: ", err))
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) {
key := ctx.State["regex_matched"].([]string)[1]
Expand Down Expand Up @@ -342,3 +342,32 @@ func initPic(picFile string, uid int64) (avatar []byte, err error) {
}
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
}
}

0 comments on commit b8858f0

Please sign in to comment.