Skip to content

Commit

Permalink
[issues 840] Modify the "score" of the Image send type add base64
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 22, 2024
1 parent a006b30 commit 7a3c393
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions plugin/score/sign_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
package score

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

engine.OnPrefix("获得签到背景", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
Expand All @@ -193,16 +196,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: 消息发送失败, 账号可能被风控"))
}
sendMessage(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))
sendMessage(drawedFile, ctx)
return
}
st, err := sdb.GetScoreRankByTopN(10)
Expand Down Expand Up @@ -267,7 +268,7 @@ func init() {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
sendMessage(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 +343,36 @@ func initPic(picFile string, uid int64) (avatar []byte, err error) {
}
return avatar, os.WriteFile(picFile, data, 0644)
}

// 使用"file:"发送图片失败后,改用base64发送
func sendMessage(FilePath string, ctx *zero.Ctx) {

Check failure on line 348 in plugin/score/sign_in.go

View workflow job for this annotation

GitHub Actions / lint

captLocal: `FilePath' should not be capitalized (gocritic)
FilePath = file.BOTPATH + "/" + FilePath
if id := ctx.SendChain(message.Image("file:///" + FilePath)); id.ID() == 0 {

imgFile, err := os.Open(FilePath)
if err != nil {
ctx.SendChain(message.Text("ERROR: 无法打开文件", err))
return
}
defer imgFile.Close()

// 使用 base64.NewEncoder 将文件内容编码为 base64 字符串
var encodedFileData bytes.Buffer
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("base64://" + drawedFileBase64)); id.ID() == 0 {
if err != nil {
ctx.SendChain(message.Text("ERROR: 无法读取图片文件", err))
return
}
}
}
}

0 comments on commit 7a3c393

Please sign in to comment.