Files
bl/modules/player/controller/robot/gold_list.go
2026-03-23 22:00:05 +08:00

62 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package robot
import (
"blazing/modules/player/service"
"bytes"
"fmt"
"github.com/fogleman/gg"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
func init() {
zero.OnCommand("行情").
Handle(func(ctx *zero.Ctx) {
var msgs []string
for _, v := range service.NewGoldListService(0).Get() {
msgs = append(msgs, fmt.Sprintf("数量:%d 汇率:%.2f 所需金币:%.2f", v.ExchangeNum, 1/v.Rate, float64(v.ExchangeNum)*v.Rate))
}
// ======================
// 生成行情图片
// ======================
const (
width = 400
lineHeight = 24
padding = 15
)
lineCount := len(msgs)
height := padding*2 + lineHeight*lineCount + 40
// 创建图片
img := gg.NewContext(width, height)
// 背景色
img.SetRGB255(245, 247, 250)
img.Clear()
// 标题
img.SetRGB255(44, 62, 80)
img.LoadFontFace("/opt/blazing/build/simhei.ttf", 20) // 黑体,解决中文乱码
img.DrawString("骄阳号金豆集市行情列表", padding, 30) //📊
// 画内容
img.SetRGB255(30, 30, 30)
img.LoadFontFace("/opt/blazing/build/simhei.ttf", 16)
for i, line := range msgs {
y := 60 + i*lineHeight
img.DrawString(line, padding, float64(y))
}
// 保存为图片到内存(不落地文件)
var buf bytes.Buffer
img.EncodePNG(&buf) // 👈 就是你要的这个方法!
// ======================
// 发送图片zeroBot 标准方式)
// ======================
ctx.Send(message.ImageBytes(buf.Bytes()))
})
}