Skip to content

Commit

Permalink
修复查看钱包和热词功能的图片问题
Browse files Browse the repository at this point in the history
  • Loading branch information
vatebur committed Mar 5, 2024
1 parent 5bb8234 commit bb7798e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
33 changes: 31 additions & 2 deletions plugin/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
package wallet

import (
"encoding/base64"
"io"
"math"
"os"
"strconv"
"strings"
"time"

"github.com/FloatTech/AnimeAPI/wallet"
Expand Down Expand Up @@ -46,7 +49,7 @@ func init() {
today := time.Now().Format("20060102")
drawedFile := cachePath + gid + today + "walletRank.png"
if file.IsExist(drawedFile) {
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
trySendImage(drawedFile, ctx)
return
}
// 无缓存获取群员列表
Expand Down Expand Up @@ -120,6 +123,32 @@ func init() {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
trySendImage(drawedFile, ctx)
})
}

// 用base64发送图片
func trySendImage(filePath string, ctx *zero.Ctx) {
filePath = file.BOTPATH + "/" + filePath
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
}
}
29 changes: 28 additions & 1 deletion plugin/wordcount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wordcount

import (
"fmt"
"io"
"os"
"regexp"
"sort"
Expand Down Expand Up @@ -175,7 +176,7 @@ func init() {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
trySendImage(drawedFile, ctx)
})
}

Expand All @@ -200,3 +201,29 @@ type pairlist []pair
func (p pairlist) Len() int { return len(p) }
func (p pairlist) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p pairlist) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

// 用base64发送图片
func trySendImage(filePath string, ctx *zero.Ctx) {
filePath = file.BOTPATH + "/" + filePath
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 bb7798e

Please sign in to comment.