Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix issues 840] Modify the "plugin\score" of the Image send type #853

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading