@@ -4,6 +4,7 @@ package wallet
4
4
import (
5
5
"math"
6
6
"os"
7
+ "regexp"
7
8
"strconv"
8
9
"strings"
9
10
"time"
@@ -23,9 +24,14 @@ import (
23
24
24
25
func init () {
25
26
en := control .AutoRegister (& ctrl.Options [* zero.Ctx ]{
26
- DisableOnDefault : false ,
27
- Brief : "钱包" ,
28
- Help : "- 查看我的钱包\n - 查看钱包排名\n - 设置硬币名称XXX" ,
27
+ DisableOnDefault : false ,
28
+ Brief : "钱包" ,
29
+ Help : "- 查看钱包排名\n " +
30
+ "- 设置硬币名称XX\n " +
31
+ "- 管理钱包余额[+金额|-金额][@xxx]\n " +
32
+ "- 查看我的钱包|查询钱包余额[@xxx]\n " +
33
+ "- 钱包转账[金额][@xxx]\n " +
34
+ "注:仅超级用户能“管理钱包余额”\n " ,
29
35
PrivateDataFolder : "wallet" ,
30
36
})
31
37
cachePath := en .DataFolder () + "cache/"
@@ -50,11 +56,6 @@ func init() {
50
56
}
51
57
wallet .SetWalletName (coinName )
52
58
}()
53
- en .OnFullMatch ("查看我的钱包" ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
54
- uid := ctx .Event .UserID
55
- money := wallet .GetWalletOf (uid )
56
- ctx .SendChain (message .At (uid ), message .Text ("你的钱包当前有" , money , wallet .GetWalletName ()))
57
- })
58
59
59
60
en .OnFullMatch ("查看钱包排名" , zero .OnlyGroup ).Limit (ctxext .LimitByGroup ).SetBlock (true ).
60
61
Handle (func (ctx * zero.Ctx ) {
@@ -149,4 +150,112 @@ func init() {
149
150
wallet .SetWalletName (coinName )
150
151
ctx .SendChain (message .Text ("记住啦~" ))
151
152
})
153
+
154
+ en .OnPrefix (`管理钱包余额` , zero .SuperUserPermission ).SetBlock (true ).Limit (ctxext .LimitByGroup ).
155
+ Handle (func (ctx * zero.Ctx ) {
156
+ param := strings .TrimSpace (ctx .State ["args" ].(string ))
157
+
158
+ // 捕获修改的金额
159
+ re := regexp .MustCompile (`^[+-]?\d+$` )
160
+ amount , err := strconv .Atoi (re .FindString (param ))
161
+ if err != nil {
162
+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("输入的金额异常" ))
163
+ return
164
+ }
165
+
166
+ // 捕获用户QQ号,只支持@事件
167
+ var uidStr string
168
+ if len (ctx .Event .Message ) > 1 && ctx .Event .Message [1 ].Type == "at" {
169
+ uidStr = ctx .Event .Message [1 ].Data ["qq" ]
170
+ } else {
171
+ // 没at就修改自己的钱包
172
+ uidStr = strconv .FormatInt (ctx .Event .UserID , 10 )
173
+ }
174
+
175
+ uidInt , err := strconv .ParseInt (uidStr , 10 , 64 )
176
+ if err != nil {
177
+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("QQ号处理失败" ))
178
+ return
179
+ }
180
+ if amount < wallet .GetWalletOf (uidInt ) {
181
+ ctx .SendChain (message .Text ("管理失败:对方钱包余额不足,扣款失败:" ))
182
+ return
183
+ }
184
+ err = wallet .InsertWalletOf (uidInt , amount )
185
+ if err != nil {
186
+ ctx .SendChain (message .Text ("[ERROR]:管理失败,钱包坏掉了:\n " , err ))
187
+ return
188
+ }
189
+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("钱包余额修改成功,已修改用户:" , uidStr , "的钱包,修改金额为:" , amount ))
190
+
191
+ })
192
+
193
+ // 保留用户习惯,兼容旧语法“查看我的钱包”
194
+ en .OnPrefixGroup ([]string {`查询钱包余额` , `查看我的钱包` }).SetBlock (true ).Limit (ctxext .LimitByGroup ).
195
+ Handle (func (ctx * zero.Ctx ) {
196
+ param := ctx .State ["args" ].(string )
197
+ var uidStr string
198
+ if len (ctx .Event .Message ) > 1 && ctx .Event .Message [1 ].Type == "at" {
199
+ uidStr = ctx .Event .Message [1 ].Data ["qq" ]
200
+ } else if param == "" {
201
+ uidStr = strconv .FormatInt (ctx .Event .UserID , 10 )
202
+ }
203
+ uidInt , err := strconv .ParseInt (uidStr , 10 , 64 )
204
+ if err != nil {
205
+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("QQ号处理失败" ))
206
+ return
207
+ }
208
+ money := wallet .GetWalletOf (uidInt )
209
+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("QQ号:" , uidStr , ",的钱包有" , money , wallet .GetWalletName ()))
210
+ })
211
+
212
+ en .OnPrefix (`钱包转账` , zero .OnlyGroup ).SetBlock (true ).Limit (ctxext .LimitByGroup ).
213
+ Handle (func (ctx * zero.Ctx ) {
214
+
215
+ param := strings .TrimSpace (ctx .State ["args" ].(string ))
216
+
217
+ // 捕获修改的金额
218
+ re := regexp .MustCompile (`^[+-]?\d+$` )
219
+ amount , err := strconv .Atoi (re .FindString (param ))
220
+ if err != nil || amount <= 0 {
221
+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("输入额异常,请检查金额或at是否正常" ))
222
+ return
223
+ }
224
+
225
+ // 捕获用户QQ号,只支持@事件
226
+ var uidStr string
227
+ if len (ctx .Event .Message ) > 1 && ctx .Event .Message [1 ].Type == "at" {
228
+ uidStr = ctx .Event .Message [1 ].Data ["qq" ]
229
+ } else {
230
+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("获取被转方信息失败" ))
231
+ return
232
+ }
233
+
234
+ uidInt , err := strconv .ParseInt (uidStr , 10 , 64 )
235
+ if err != nil {
236
+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("QQ号处理失败" ))
237
+ return
238
+ }
239
+
240
+ // 开始转账流程
241
+ if amount > wallet .GetWalletOf (ctx .Event .UserID ) {
242
+ ctx .SendChain (message .Text ("[ERROR]:钱包余额不足,转账失败" ))
243
+ return
244
+ }
245
+
246
+ err = wallet .InsertWalletOf (ctx .Event .UserID , - amount )
247
+ if err != nil {
248
+ ctx .SendChain (message .Text ("[ERROR]:转账失败,扣款异常:\n " , err ))
249
+ return
250
+ }
251
+
252
+ err = wallet .InsertWalletOf (uidInt , amount )
253
+ if err != nil {
254
+ ctx .SendChain (message .Text ("[ERROR]:转账失败,转账时银行被打劫:\n " , err ))
255
+ return
256
+ }
257
+ ctx .SendChain (message .Reply (ctx .Event .MessageID ), message .Text ("转账成功:成功给" ), message .At (uidInt ), message .Text (",转账:" , amount , wallet .GetWalletName ()))
258
+
259
+ })
260
+
152
261
}
0 commit comments