koishi-plugin-monetary-admin 1.0.2 → 1.1.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 +109 -46
- package/package.json +4 -1
package/lib/index.js
CHANGED
|
@@ -7,25 +7,15 @@ exports.name = 'monetary-admin';
|
|
|
7
7
|
exports.inject = ['monetary', 'database'];
|
|
8
8
|
exports.Config = koishi_1.Schema.object({});
|
|
9
9
|
function apply(ctx) {
|
|
10
|
-
|
|
11
|
-
.alias('开户')
|
|
12
|
-
.action(async ({ session }, target, currency, amount) => {
|
|
10
|
+
async function resolveUser(target) {
|
|
13
11
|
if (!target)
|
|
14
|
-
|
|
15
|
-
if (!currency)
|
|
16
|
-
return '请输入货币类型。';
|
|
17
|
-
if (!amount)
|
|
18
|
-
return '请输入金额。';
|
|
12
|
+
throw '请输入目标用户。';
|
|
19
13
|
const [platform, pid] = target.split(':');
|
|
20
14
|
if (!platform || !pid)
|
|
21
|
-
|
|
22
|
-
const bindings = await ctx.database.get('binding', {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}, ['aid']);
|
|
26
|
-
if (bindings.length === 0) {
|
|
27
|
-
return '未找到该用户。';
|
|
28
|
-
}
|
|
15
|
+
throw '目标用户格式错误。';
|
|
16
|
+
const bindings = await ctx.database.get('binding', { platform, pid }, ['aid']);
|
|
17
|
+
if (bindings.length === 0)
|
|
18
|
+
throw '未找到该用户。';
|
|
29
19
|
const uid = bindings[0].aid;
|
|
30
20
|
const [user] = await ctx.database.get('user', { id: uid }, ['name']);
|
|
31
21
|
let name = user?.name || target;
|
|
@@ -41,50 +31,123 @@ function apply(ctx) {
|
|
|
41
31
|
catch (e) { }
|
|
42
32
|
}
|
|
43
33
|
}
|
|
34
|
+
return { uid, name };
|
|
35
|
+
}
|
|
36
|
+
ctx.command('monetary.add <target:user> <currency:string> <amount:number>', '给目标用户添加货币', { authority: 5 })
|
|
37
|
+
.action(async ({ session }, target, currency, amount) => {
|
|
44
38
|
try {
|
|
39
|
+
const { uid, name } = await resolveUser(target);
|
|
40
|
+
if (!currency)
|
|
41
|
+
return '请输入货币类型。';
|
|
42
|
+
if (!amount)
|
|
43
|
+
return '请输入金额。';
|
|
44
|
+
if (amount <= 0)
|
|
45
|
+
return '金额必须为正数。';
|
|
45
46
|
await ctx.monetary.gain(uid, amount, currency);
|
|
46
47
|
return `成功给用户 ${name} (UID: ${uid}) 添加了 ${amount} ${currency}。`;
|
|
47
48
|
}
|
|
48
49
|
catch (e) {
|
|
49
|
-
return `添加失败: ${e.message}`;
|
|
50
|
+
return typeof e === 'string' ? e : `添加失败: ${e.message}`;
|
|
50
51
|
}
|
|
51
52
|
});
|
|
52
|
-
ctx.command('monetary.
|
|
53
|
-
.alias('
|
|
54
|
-
.action(async ({ session }, target) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return '未找到该用户。';
|
|
53
|
+
ctx.command('monetary.reduce <target:user> <currency:string> <amount:number>', '扣除目标用户货币', { authority: 5 })
|
|
54
|
+
.alias('扣款')
|
|
55
|
+
.action(async ({ session }, target, currency, amount) => {
|
|
56
|
+
try {
|
|
57
|
+
const { uid, name } = await resolveUser(target);
|
|
58
|
+
if (!currency)
|
|
59
|
+
return '请输入货币类型。';
|
|
60
|
+
if (!amount)
|
|
61
|
+
return '请输入金额。';
|
|
62
|
+
if (amount <= 0)
|
|
63
|
+
return '金额必须为正数。';
|
|
64
|
+
await ctx.monetary.gain(uid, -amount, currency);
|
|
65
|
+
return `成功从用户 ${name} (UID: ${uid}) 扣除了 ${amount} ${currency}。`;
|
|
66
66
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
67
|
+
catch (e) {
|
|
68
|
+
return typeof e === 'string' ? e : `扣除失败: ${e.message}`;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
ctx.command('monetary.clear <target:user> <currency:string>', '清零目标用户指定货币', { authority: 5 })
|
|
72
|
+
.alias('清零')
|
|
73
|
+
.action(async ({ session }, target, currency) => {
|
|
74
|
+
try {
|
|
75
|
+
const { uid, name } = await resolveUser(target);
|
|
76
|
+
if (!currency)
|
|
77
|
+
return '请输入货币类型。';
|
|
78
|
+
await ctx.database.set('monetary', { uid, currency }, { value: 0 });
|
|
79
|
+
return `成功将用户 ${name} (UID: ${uid}) 的 ${currency} 余额清零。`;
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
return typeof e === 'string' ? e : `清零失败: ${e.message}`;
|
|
81
83
|
}
|
|
84
|
+
});
|
|
85
|
+
ctx.command('monetary.remove <target:user>', '删除目标用户的所有货币记录', { authority: 5 })
|
|
86
|
+
.alias('销户')
|
|
87
|
+
.option('force', '-f 强制删除')
|
|
88
|
+
.action(async ({ session, options }, target) => {
|
|
82
89
|
try {
|
|
90
|
+
const { uid, name } = await resolveUser(target);
|
|
91
|
+
if (!options.force) {
|
|
92
|
+
return '该操作将删除用户的所有货币记录,且不可恢复。请使用 --force 选项确认删除。';
|
|
93
|
+
}
|
|
83
94
|
await ctx.database.remove('monetary', { uid });
|
|
84
95
|
return `成功删除用户 ${name} (UID: ${uid}) 的所有货币记录。`;
|
|
85
96
|
}
|
|
86
97
|
catch (e) {
|
|
87
|
-
return `删除失败: ${e.message}`;
|
|
98
|
+
return typeof e === 'string' ? e : `删除失败: ${e.message}`;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
ctx.command('monetary.balance <target:user>', '查询目标用户的货币余额', { authority: 1 })
|
|
102
|
+
.alias('查询余额')
|
|
103
|
+
.action(async ({ session }, target) => {
|
|
104
|
+
try {
|
|
105
|
+
const { uid, name } = await resolveUser(target);
|
|
106
|
+
const monetaryRecords = await ctx.database.get('monetary', { uid });
|
|
107
|
+
if (monetaryRecords.length === 0) {
|
|
108
|
+
return `用户 ${name} (UID: ${uid}) 暂无任何货币记录。`;
|
|
109
|
+
}
|
|
110
|
+
let result = `用户 ${name} (UID: ${uid}) 的货币余额:\n`;
|
|
111
|
+
for (const record of monetaryRecords) {
|
|
112
|
+
result += `${record.currency} ${record.value}\n`;
|
|
113
|
+
}
|
|
114
|
+
return result.trim();
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
return typeof e === 'string' ? e : `查询失败: ${e.message}`;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
ctx.command('monetary.transfer <target:user> <currency:string> <amount:number>', '转账给目标用户', { authority: 1 })
|
|
121
|
+
.alias('转账')
|
|
122
|
+
.userFields(['id'])
|
|
123
|
+
.action(async ({ session }, target, currency, amount) => {
|
|
124
|
+
try {
|
|
125
|
+
if (!session?.user?.id)
|
|
126
|
+
return '无法获取您的用户信息。';
|
|
127
|
+
if (!currency)
|
|
128
|
+
return '请输入货币类型。';
|
|
129
|
+
if (!amount)
|
|
130
|
+
return '请输入金额。';
|
|
131
|
+
if (amount <= 0)
|
|
132
|
+
return '转账金额必须为正数。';
|
|
133
|
+
const senderUid = session.user.id;
|
|
134
|
+
const { uid: targetUid, name: targetName } = await resolveUser(target);
|
|
135
|
+
if (senderUid === targetUid)
|
|
136
|
+
return '不能给自己转账。';
|
|
137
|
+
// 检查发送者余额
|
|
138
|
+
const senderRecords = await ctx.database.get('monetary', { uid: senderUid, currency });
|
|
139
|
+
if (senderRecords.length === 0 || senderRecords[0].value < amount) {
|
|
140
|
+
return `您的 ${currency} 余额不足。`;
|
|
141
|
+
}
|
|
142
|
+
// 执行转账
|
|
143
|
+
await ctx.monetary.gain(senderUid, -amount, currency);
|
|
144
|
+
await ctx.monetary.gain(targetUid, amount, currency);
|
|
145
|
+
const [senderUser] = await ctx.database.get('user', { id: senderUid }, ['name']);
|
|
146
|
+
const senderName = senderUser?.name || String(senderUid);
|
|
147
|
+
return `成功将 ${amount} ${currency} 从 ${senderName} 转账给 ${targetName}。`;
|
|
148
|
+
}
|
|
149
|
+
catch (e) {
|
|
150
|
+
return typeof e === 'string' ? e : `转账失败: ${e.message}`;
|
|
88
151
|
}
|
|
89
152
|
});
|
|
90
153
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-monetary-admin",
|
|
3
3
|
"description": "Admin commands for Koishi monetary system",
|
|
4
|
-
"version": "1.0
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -9,6 +9,9 @@
|
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
11
|
"license": "MIT",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc"
|
|
14
|
+
},
|
|
12
15
|
"keywords": [
|
|
13
16
|
"koishi",
|
|
14
17
|
"plugin",
|