Skip to content

Update:[wallet]完善钱包管理和查询功能 #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()))

})

}
Loading