package robot import ( "blazing/cool" "blazing/modules/player/service" "bytes" "fmt" "time" "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 标准方式) // ====================== msg := ctx.Send(message.ImageBytes(buf.Bytes())) cool.Cron.AfterFunc(20*time.Second, func() { ctx.DeleteMessage(ctx.Event.MessageID) ctx.DeleteMessage(msg) }) }) }