feat: configurable custom coin name (#950)

This commit is contained in:
Eat Hatsune Shallots
2024-08-08 18:02:53 +08:00
committed by GitHub
parent 26fcb5aad5
commit 2a4aa39bab
6 changed files with 48 additions and 18 deletions

View File

@@ -5,9 +5,11 @@ import (
"math"
"os"
"strconv"
"strings"
"time"
"github.com/FloatTech/AnimeAPI/wallet"
"github.com/FloatTech/floatbox/binary"
"github.com/FloatTech/floatbox/file"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
@@ -23,21 +25,35 @@ func init() {
en := control.AutoRegister(&ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "钱包",
Help: "- 查看我的钱包\n- 查看钱包排名",
Help: "- 查看我的钱包\n- 查看钱包排名\n- 设置硬币名称XXX",
PrivateDataFolder: "wallet",
})
cachePath := en.DataFolder() + "cache/"
coinNameFile := en.DataFolder() + "coin_name.txt"
go func() {
_ = os.RemoveAll(cachePath)
err := os.MkdirAll(cachePath, 0755)
if err != nil {
panic(err)
}
// 更改硬币名称
var coinName string
if file.IsExist(coinNameFile) {
content, err := os.ReadFile(coinNameFile)
if err != nil {
panic(err)
}
coinName = binary.BytesToString(content)
} else {
// 旧版本数据
coinName = "ATRI币"
}
wallet.SetWalletName(coinName)
}()
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币"))
ctx.SendChain(message.At(uid), message.Text("你的钱包当前有", money, wallet.GetWalletName()))
})
en.OnFullMatch("查看钱包排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
@@ -62,7 +78,7 @@ func init() {
return
}
if len(st) == 0 {
ctx.SendChain(message.Text("ERROR: 当前没人获取过ATRI币"))
ctx.SendChain(message.Text("ERROR: 当前没人获取过", wallet.GetWalletName()))
return
} else if len(st) > 10 {
st = st[:10]
@@ -98,7 +114,7 @@ func init() {
}
err = chart.BarChart{
Font: font,
Title: "ATRI币排名(1天只刷新1次)",
Title: wallet.GetWalletName() + "排名(1天只刷新1次)",
Background: chart.Style{
Padding: chart.Box{
Top: 40,
@@ -122,4 +138,15 @@ func init() {
}
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
})
en.OnPrefix("设置硬币名称", zero.OnlyToMe, zero.SuperUserPermission).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
coinName := strings.TrimSpace(ctx.State["args"].(string))
err := os.WriteFile(coinNameFile, binary.StringToBytes(coinName), 0644)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
wallet.SetWalletName(coinName)
ctx.SendChain(message.Text("记住啦~"))
})
}