mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 22:00:11 +08:00
126 lines
3.3 KiB
Go
126 lines
3.3 KiB
Go
// Package wallet 钱包
|
|
package wallet
|
|
|
|
import (
|
|
"math"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/FloatTech/AnimeAPI/wallet"
|
|
"github.com/FloatTech/floatbox/file"
|
|
ctrl "github.com/FloatTech/zbpctrl"
|
|
"github.com/FloatTech/zbputils/control"
|
|
"github.com/FloatTech/zbputils/ctxext"
|
|
"github.com/FloatTech/zbputils/img/text"
|
|
"github.com/golang/freetype"
|
|
"github.com/wcharczuk/go-chart/v2"
|
|
zero "github.com/wdvxdr1123/ZeroBot"
|
|
"github.com/wdvxdr1123/ZeroBot/message"
|
|
)
|
|
|
|
func init() {
|
|
en := control.AutoRegister(&ctrl.Options[*zero.Ctx]{
|
|
DisableOnDefault: false,
|
|
Brief: "钱包",
|
|
Help: "- 查看我的钱包\n- 查看钱包排名",
|
|
PrivateDataFolder: "wallet",
|
|
})
|
|
cachePath := en.DataFolder() + "cache/"
|
|
go func() {
|
|
_ = os.RemoveAll(cachePath)
|
|
err := os.MkdirAll(cachePath, 0755)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}()
|
|
en.OnFullMatch("查看我的钱包").SetBlock(true).Handle(func(ctx *zero.Ctx) {
|
|
uid := ctx.Event.UserID
|
|
money := wallet.GetWalletOf(uid)
|
|
ctx.SendChain(message.At(uid), message.Text("你的钱包当前有", money, "ATRI币"))
|
|
})
|
|
|
|
en.OnFullMatch("查看钱包排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
|
|
Handle(func(ctx *zero.Ctx) {
|
|
gid := strconv.FormatInt(ctx.Event.GroupID, 10)
|
|
today := time.Now().Format("20060102")
|
|
drawedFile := cachePath + gid + today + "walletRank.png"
|
|
if file.IsExist(drawedFile) {
|
|
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
|
|
return
|
|
}
|
|
// 无缓存获取群员列表
|
|
temp := ctx.GetThisGroupMemberListNoCache().Array()
|
|
usergroup := make([]int64, len(temp))
|
|
for i, info := range temp {
|
|
usergroup[i] = info.Get("user_id").Int()
|
|
}
|
|
// 获取钱包信息
|
|
st, err := wallet.GetGroupWalletOf(true, usergroup...)
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR: ", err))
|
|
return
|
|
}
|
|
if len(st) == 0 {
|
|
ctx.SendChain(message.Text("ERROR: 当前没人获取过ATRI币"))
|
|
return
|
|
} else if len(st) > 10 {
|
|
st = st[:10]
|
|
}
|
|
_, err = file.GetLazyData(text.FontFile, control.Md5File, true)
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR: ", err))
|
|
return
|
|
}
|
|
b, err := os.ReadFile(text.FontFile)
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR: ", err))
|
|
return
|
|
}
|
|
font, err := freetype.ParseFont(b)
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR: ", err))
|
|
return
|
|
}
|
|
f, err := os.Create(drawedFile)
|
|
if err != nil {
|
|
ctx.SendChain(message.Text("ERROR: ", err))
|
|
return
|
|
}
|
|
var bars []chart.Value
|
|
for _, v := range st {
|
|
if v.Money != 0 {
|
|
bars = append(bars, chart.Value{
|
|
Label: ctx.CardOrNickName(v.UID),
|
|
Value: float64(v.Money),
|
|
})
|
|
}
|
|
}
|
|
err = chart.BarChart{
|
|
Font: font,
|
|
Title: "ATRI币排名(1天只刷新1次)",
|
|
Background: chart.Style{
|
|
Padding: chart.Box{
|
|
Top: 40,
|
|
},
|
|
},
|
|
YAxis: chart.YAxis{
|
|
Range: &chart.ContinuousRange{
|
|
Min: 0,
|
|
Max: math.Ceil(bars[0].Value/10) * 10,
|
|
},
|
|
},
|
|
Height: 500,
|
|
BarWidth: 50,
|
|
Bars: bars,
|
|
}.Render(chart.PNG, f)
|
|
_ = f.Close()
|
|
if err != nil {
|
|
_ = os.Remove(drawedFile)
|
|
ctx.SendChain(message.Text("ERROR: ", err))
|
|
return
|
|
}
|
|
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
|
|
})
|
|
}
|