Skip to content

Commit

Permalink
feat(wallet): 完善钱包管理和查询功能 (#954)
Browse files Browse the repository at this point in the history
1. 新增“管理钱包余额”功能,仅超级用户可用
2. 重构“查看我的钱包功能”,可以查看任意群员的钱包
3. 增加“钱包转账”功能

有问题的地方:
1.任何人的钱包都可查,群友钱包余额泄露
2. ”管理钱包余额“和“钱包转账”代码高度相似,是否有必要优化

* chore: update readme
  • Loading branch information
vatebur authored Aug 19, 2024
1 parent 1c0f8ff commit 852c5f2
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 10 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1305,12 +1305,18 @@ print("run[CQ:image,file="+j["img"]+"]")

`import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/wallet"`

- [x] 查看我的钱包

- [x] 查看钱包排名

- [x] 设置硬币名称[ATRI币]

- [x] 管理钱包余额[+金额|-金额][@xxx]

- [x] 查看我的钱包|查询钱包余额[@xxx]

- [x] 钱包转账[金额][@xxx]

- 注:仅超级用户能"管理钱包余额",

</details>
<details>
<summary>据意查句</summary>
Expand Down
125 changes: 117 additions & 8 deletions plugin/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package wallet
import (
"math"
"os"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -23,9 +24,14 @@ import (

func init() {
en := control.AutoRegister(&ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "钱包",
Help: "- 查看我的钱包\n- 查看钱包排名\n- 设置硬币名称XXX",
DisableOnDefault: false,
Brief: "钱包",
Help: "- 查看钱包排名\n" +
"- 设置硬币名称XX\n" +
"- 管理钱包余额[+金额|-金额][@xxx]\n" +
"- 查看我的钱包|查询钱包余额[@xxx]\n" +
"- 钱包转账[金额][@xxx]\n" +
"注:仅超级用户能“管理钱包余额”\n",
PrivateDataFolder: "wallet",
})
cachePath := en.DataFolder() + "cache/"
Expand All @@ -50,11 +56,6 @@ func init() {
}
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, wallet.GetWalletName()))
})

en.OnFullMatch("查看钱包排名", zero.OnlyGroup).Limit(ctxext.LimitByGroup).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
Expand Down Expand Up @@ -149,4 +150,112 @@ func init() {
wallet.SetWalletName(coinName)
ctx.SendChain(message.Text("记住啦~"))
})

en.OnPrefix(`管理钱包余额`, zero.SuperUserPermission).SetBlock(true).Limit(ctxext.LimitByGroup).
Handle(func(ctx *zero.Ctx) {
param := strings.TrimSpace(ctx.State["args"].(string))

// 捕获修改的金额
re := regexp.MustCompile(`^[+-]?\d+$`)
amount, err := strconv.Atoi(re.FindString(param))
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("输入的金额异常"))
return
}

// 捕获用户QQ号,只支持@事件
var uidStr string
if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" {
uidStr = ctx.Event.Message[1].Data["qq"]
} else {
// 没at就修改自己的钱包
uidStr = strconv.FormatInt(ctx.Event.UserID, 10)
}

uidInt, err := strconv.ParseInt(uidStr, 10, 64)
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("QQ号处理失败"))
return
}
if amount < wallet.GetWalletOf(uidInt) {
ctx.SendChain(message.Text("管理失败:对方钱包余额不足,扣款失败:"))
return
}
err = wallet.InsertWalletOf(uidInt, amount)
if err != nil {
ctx.SendChain(message.Text("[ERROR]:管理失败,钱包坏掉了:\n", err))
return
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("钱包余额修改成功,已修改用户:", uidStr, "的钱包,修改金额为:", amount))

})

// 保留用户习惯,兼容旧语法“查看我的钱包”
en.OnPrefixGroup([]string{`查询钱包余额`, `查看我的钱包`}).SetBlock(true).Limit(ctxext.LimitByGroup).
Handle(func(ctx *zero.Ctx) {
param := ctx.State["args"].(string)
var uidStr string
if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" {
uidStr = ctx.Event.Message[1].Data["qq"]
} else if param == "" {
uidStr = strconv.FormatInt(ctx.Event.UserID, 10)
}
uidInt, err := strconv.ParseInt(uidStr, 10, 64)
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("QQ号处理失败"))
return
}
money := wallet.GetWalletOf(uidInt)
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("QQ号:", uidStr, ",的钱包有", money, wallet.GetWalletName()))
})

en.OnPrefix(`钱包转账`, zero.OnlyGroup).SetBlock(true).Limit(ctxext.LimitByGroup).
Handle(func(ctx *zero.Ctx) {

param := strings.TrimSpace(ctx.State["args"].(string))

// 捕获修改的金额
re := regexp.MustCompile(`^[+-]?\d+$`)
amount, err := strconv.Atoi(re.FindString(param))
if err != nil || amount <= 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("输入额异常,请检查金额或at是否正常"))
return
}

// 捕获用户QQ号,只支持@事件
var uidStr string
if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" {
uidStr = ctx.Event.Message[1].Data["qq"]
} else {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("获取被转方信息失败"))
return
}

uidInt, err := strconv.ParseInt(uidStr, 10, 64)
if err != nil {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("QQ号处理失败"))
return
}

// 开始转账流程
if amount > wallet.GetWalletOf(ctx.Event.UserID) {
ctx.SendChain(message.Text("[ERROR]:钱包余额不足,转账失败"))
return
}

err = wallet.InsertWalletOf(ctx.Event.UserID, -amount)
if err != nil {
ctx.SendChain(message.Text("[ERROR]:转账失败,扣款异常:\n", err))
return
}

err = wallet.InsertWalletOf(uidInt, amount)
if err != nil {
ctx.SendChain(message.Text("[ERROR]:转账失败,转账时银行被打劫:\n", err))
return
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("转账成功:成功给"), message.At(uidInt), message.Text(",转账:", amount, wallet.GetWalletName()))

})

}

0 comments on commit 852c5f2

Please sign in to comment.