Skip to content

Commit a1f1016

Browse files
authored
独立wallet插件 (#531)
1 parent 913f01d commit a1f1016

File tree

3 files changed

+126
-87
lines changed

3 files changed

+126
-87
lines changed

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ import (
132132
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/translation" // 翻译
133133
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/vitsnyaru" // vits猫雷
134134
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/vtb_quotation" // vtb语录
135+
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/wallet" // 钱包
135136
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/wangyiyun" // 网易云音乐热评
136137
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/wenben" // 文本指令大全
137138
_ "github.com/FloatTech/ZeroBot-Plugin/plugin/wenxinAI" // 百度文心AI画图

plugin/score/sign_in.go

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ func init() {
5454
}
5555
sdb = initialize(engine.DataFolder() + "score.db")
5656
}()
57-
zero.OnFullMatch("查看我的钱包").SetBlock(true).Handle(func(ctx *zero.Ctx) {
58-
uid := ctx.Event.UserID
59-
money := wallet.GetWalletOf(uid)
60-
ctx.SendChain(message.At(uid), message.Text("你的钱包当前有", money, "ATRI币"))
61-
})
6257
engine.OnFullMatch("签到").Limit(ctxext.LimitByUser).SetBlock(true).
6358
Handle(func(ctx *zero.Ctx) {
6459
uid := ctx.Event.UserID
@@ -275,88 +270,6 @@ func init() {
275270
}
276271
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
277272
})
278-
engine.OnFullMatch("查看钱包排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
279-
Handle(func(ctx *zero.Ctx) {
280-
gid := strconv.FormatInt(ctx.Event.GroupID, 10)
281-
today := time.Now().Format("20060102")
282-
drawedFile := cachePath + gid + today + "walletRank.png"
283-
if file.IsExist(drawedFile) {
284-
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
285-
return
286-
}
287-
// 无缓存获取群员列表
288-
temp := ctx.GetThisGroupMemberListNoCache().Array()
289-
usergroup := make([]int64, len(temp))
290-
for i, info := range temp {
291-
usergroup[i] = info.Get("user_id").Int()
292-
}
293-
// 获取钱包信息
294-
st, err := wallet.GetGroupWalletOf(true, usergroup...)
295-
if err != nil {
296-
ctx.SendChain(message.Text("ERROR: ", err))
297-
return
298-
}
299-
if len(st) == 0 {
300-
ctx.SendChain(message.Text("ERROR: 当前没人获取过ATRI币"))
301-
return
302-
} else if len(st) > 10 {
303-
st = st[:10]
304-
}
305-
_, err = file.GetLazyData(text.FontFile, control.Md5File, true)
306-
if err != nil {
307-
ctx.SendChain(message.Text("ERROR: ", err))
308-
return
309-
}
310-
b, err := os.ReadFile(text.FontFile)
311-
if err != nil {
312-
ctx.SendChain(message.Text("ERROR: ", err))
313-
return
314-
}
315-
font, err := freetype.ParseFont(b)
316-
if err != nil {
317-
ctx.SendChain(message.Text("ERROR: ", err))
318-
return
319-
}
320-
f, err := os.Create(drawedFile)
321-
if err != nil {
322-
ctx.SendChain(message.Text("ERROR: ", err))
323-
return
324-
}
325-
var bars []chart.Value
326-
for _, v := range st {
327-
if v.Money != 0 {
328-
bars = append(bars, chart.Value{
329-
Label: ctx.CardOrNickName(v.UID),
330-
Value: float64(v.Money),
331-
})
332-
}
333-
}
334-
err = chart.BarChart{
335-
Font: font,
336-
Title: "ATRI币排名(1天只刷新1次)",
337-
Background: chart.Style{
338-
Padding: chart.Box{
339-
Top: 40,
340-
},
341-
},
342-
YAxis: chart.YAxis{
343-
Range: &chart.ContinuousRange{
344-
Min: 0,
345-
Max: math.Ceil(bars[0].Value/10) * 10,
346-
},
347-
},
348-
Height: 500,
349-
BarWidth: 50,
350-
Bars: bars,
351-
}.Render(chart.PNG, f)
352-
_ = f.Close()
353-
if err != nil {
354-
_ = os.Remove(drawedFile)
355-
ctx.SendChain(message.Text("ERROR: ", err))
356-
return
357-
}
358-
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
359-
})
360273
}
361274

362275
func getHourWord(t time.Time) string {

plugin/wallet/wallet.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Package wallet 钱包
2+
package wallet
3+
4+
import (
5+
"math"
6+
"os"
7+
"strconv"
8+
"time"
9+
10+
"github.com/FloatTech/AnimeAPI/wallet"
11+
"github.com/FloatTech/floatbox/file"
12+
ctrl "github.com/FloatTech/zbpctrl"
13+
"github.com/FloatTech/zbputils/control"
14+
"github.com/FloatTech/zbputils/ctxext"
15+
"github.com/FloatTech/zbputils/img/text"
16+
"github.com/golang/freetype"
17+
"github.com/wcharczuk/go-chart/v2"
18+
zero "github.com/wdvxdr1123/ZeroBot"
19+
"github.com/wdvxdr1123/ZeroBot/message"
20+
)
21+
22+
func init() {
23+
en := control.Register("wallet", &ctrl.Options[*zero.Ctx]{
24+
DisableOnDefault: false,
25+
Brief: "钱包",
26+
Help: "- 查看我的钱包\n- 查看钱包排名",
27+
PrivateDataFolder: "wallet",
28+
})
29+
cachePath := en.DataFolder() + "cache/"
30+
go func() {
31+
_ = os.RemoveAll(cachePath)
32+
err := os.MkdirAll(cachePath, 0755)
33+
if err != nil {
34+
panic(err)
35+
}
36+
}()
37+
en.OnFullMatch("查看我的钱包").SetBlock(true).Handle(func(ctx *zero.Ctx) {
38+
uid := ctx.Event.UserID
39+
money := wallet.GetWalletOf(uid)
40+
ctx.SendChain(message.At(uid), message.Text("你的钱包当前有", money, "ATRI币"))
41+
})
42+
43+
en.OnFullMatch("查看钱包排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
44+
Handle(func(ctx *zero.Ctx) {
45+
gid := strconv.FormatInt(ctx.Event.GroupID, 10)
46+
today := time.Now().Format("20060102")
47+
drawedFile := cachePath + gid + today + "walletRank.png"
48+
if file.IsExist(drawedFile) {
49+
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
50+
return
51+
}
52+
// 无缓存获取群员列表
53+
temp := ctx.GetThisGroupMemberListNoCache().Array()
54+
usergroup := make([]int64, len(temp))
55+
for i, info := range temp {
56+
usergroup[i] = info.Get("user_id").Int()
57+
}
58+
// 获取钱包信息
59+
st, err := wallet.GetGroupWalletOf(true, usergroup...)
60+
if err != nil {
61+
ctx.SendChain(message.Text("ERROR: ", err))
62+
return
63+
}
64+
if len(st) == 0 {
65+
ctx.SendChain(message.Text("ERROR: 当前没人获取过ATRI币"))
66+
return
67+
} else if len(st) > 10 {
68+
st = st[:10]
69+
}
70+
_, err = file.GetLazyData(text.FontFile, control.Md5File, true)
71+
if err != nil {
72+
ctx.SendChain(message.Text("ERROR: ", err))
73+
return
74+
}
75+
b, err := os.ReadFile(text.FontFile)
76+
if err != nil {
77+
ctx.SendChain(message.Text("ERROR: ", err))
78+
return
79+
}
80+
font, err := freetype.ParseFont(b)
81+
if err != nil {
82+
ctx.SendChain(message.Text("ERROR: ", err))
83+
return
84+
}
85+
f, err := os.Create(drawedFile)
86+
if err != nil {
87+
ctx.SendChain(message.Text("ERROR: ", err))
88+
return
89+
}
90+
var bars []chart.Value
91+
for _, v := range st {
92+
if v.Money != 0 {
93+
bars = append(bars, chart.Value{
94+
Label: ctx.CardOrNickName(v.UID),
95+
Value: float64(v.Money),
96+
})
97+
}
98+
}
99+
err = chart.BarChart{
100+
Font: font,
101+
Title: "ATRI币排名(1天只刷新1次)",
102+
Background: chart.Style{
103+
Padding: chart.Box{
104+
Top: 40,
105+
},
106+
},
107+
YAxis: chart.YAxis{
108+
Range: &chart.ContinuousRange{
109+
Min: 0,
110+
Max: math.Ceil(bars[0].Value/10) * 10,
111+
},
112+
},
113+
Height: 500,
114+
BarWidth: 50,
115+
Bars: bars,
116+
}.Render(chart.PNG, f)
117+
_ = f.Close()
118+
if err != nil {
119+
_ = os.Remove(drawedFile)
120+
ctx.SendChain(message.Text("ERROR: ", err))
121+
return
122+
}
123+
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
124+
})
125+
}

0 commit comments

Comments
 (0)