koishi-plugin-monetary-admin 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +4 -19
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -33,7 +33,7 @@ function apply(ctx) {
|
|
|
33
33
|
}
|
|
34
34
|
return { uid, name };
|
|
35
35
|
}
|
|
36
|
-
ctx.command('monetary.add [target:user] <currency:string> <amount:number>', '
|
|
36
|
+
ctx.command('monetary.add [target:user] <currency:string> <amount:number>', '给目标用户或自己添加货币', { authority: 5 })
|
|
37
37
|
.userFields(['id'])
|
|
38
38
|
.action(async ({ session }, target, currency, amount) => {
|
|
39
39
|
try {
|
|
@@ -56,12 +56,7 @@ function apply(ctx) {
|
|
|
56
56
|
return '请输入金额。';
|
|
57
57
|
if (amount <= 0)
|
|
58
58
|
return '金额必须为正数。';
|
|
59
|
-
|
|
60
|
-
await ctx.database.upsert('monetary', (row) => [{
|
|
61
|
-
uid,
|
|
62
|
-
currency,
|
|
63
|
-
value: koishi_1.$.add(row.value, amount),
|
|
64
|
-
}], ['uid', 'currency']);
|
|
59
|
+
await ctx.monetary.gain(uid, amount, currency);
|
|
65
60
|
return `成功给用户 ${name} (UID: ${uid}) 添加了 ${amount} ${currency}。`;
|
|
66
61
|
}
|
|
67
62
|
catch (e) {
|
|
@@ -91,12 +86,7 @@ function apply(ctx) {
|
|
|
91
86
|
return '请输入金额。';
|
|
92
87
|
if (amount <= 0)
|
|
93
88
|
return '金额必须为正数。';
|
|
94
|
-
|
|
95
|
-
await ctx.database.upsert('monetary', (row) => [{
|
|
96
|
-
uid,
|
|
97
|
-
currency,
|
|
98
|
-
value: koishi_1.$.sub(row.value, amount),
|
|
99
|
-
}], ['uid', 'currency']);
|
|
89
|
+
await ctx.monetary.gain(uid, -amount, currency);
|
|
100
90
|
return `成功从用户 ${name} (UID: ${uid}) 扣除了 ${amount} ${currency}。`;
|
|
101
91
|
}
|
|
102
92
|
catch (e) {
|
|
@@ -193,12 +183,7 @@ function apply(ctx) {
|
|
|
193
183
|
const { uid: targetUid, name: targetName } = await resolveUser(target);
|
|
194
184
|
if (senderUid === targetUid)
|
|
195
185
|
return '不能给自己转账。';
|
|
196
|
-
//
|
|
197
|
-
const senderRecords = await ctx.database.get('monetary', { uid: senderUid, currency });
|
|
198
|
-
if (senderRecords.length === 0 || senderRecords[0].value < amount) {
|
|
199
|
-
return `您的 ${currency} 余额不足。`;
|
|
200
|
-
}
|
|
201
|
-
// 执行转账
|
|
186
|
+
// 执行转账(先扣款,如果余额不足会自动失败)
|
|
202
187
|
await ctx.monetary.gain(senderUid, -amount, currency);
|
|
203
188
|
await ctx.monetary.gain(targetUid, amount, currency);
|
|
204
189
|
const [senderUser] = await ctx.database.get('user', { id: senderUid }, ['name']);
|