koishi-plugin-monetary-admin 1.0.3 → 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.
Files changed (2) hide show
  1. package/lib/index.js +92 -74
  2. 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
- ctx.command('monetary.add <target:user> <currency:string> <amount:number>', '给目标用户添加货币', { authority: 3 })
11
- .alias('开户')
12
- .action(async ({ session }, target, currency, amount) => {
10
+ async function resolveUser(target) {
13
11
  if (!target)
14
- return '请输入目标用户。';
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
- return '目标用户格式错误。';
22
- const bindings = await ctx.database.get('binding', {
23
- platform,
24
- pid,
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,83 +31,78 @@ 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.remove <target:user>', '删除目标用户的所有货币记录', { authority: 3 })
53
- .alias('销户')
54
- .action(async ({ session }, target) => {
55
- if (!target)
56
- return '请输入目标用户。';
57
- const [platform, pid] = target.split(':');
58
- if (!platform || !pid)
59
- return '目标用户格式错误。';
60
- const bindings = await ctx.database.get('binding', {
61
- platform,
62
- pid,
63
- }, ['aid']);
64
- if (bindings.length === 0) {
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
- const uid = bindings[0].aid;
68
- const [user] = await ctx.database.get('user', { id: uid }, ['name']);
69
- let name = user?.name || target;
70
- if (!user?.name) {
71
- const bot = ctx.bots.find(b => b.platform === platform);
72
- if (bot) {
73
- try {
74
- const platformUser = await bot.getUser(pid);
75
- if (platformUser?.name) {
76
- name = platformUser.name;
77
- }
78
- }
79
- catch (e) { }
80
- }
67
+ catch (e) {
68
+ return typeof e === 'string' ? e : `扣除失败: ${e.message}`;
81
69
  }
70
+ });
71
+ ctx.command('monetary.clear <target:user> <currency:string>', '清零目标用户指定货币', { authority: 5 })
72
+ .alias('清零')
73
+ .action(async ({ session }, target, currency) => {
82
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}`;
83
+ }
84
+ });
85
+ ctx.command('monetary.remove <target:user>', '删除目标用户的所有货币记录', { authority: 5 })
86
+ .alias('销户')
87
+ .option('force', '-f 强制删除')
88
+ .action(async ({ session, options }, target) => {
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}`;
88
99
  }
89
100
  });
90
101
  ctx.command('monetary.balance <target:user>', '查询目标用户的货币余额', { authority: 1 })
91
102
  .alias('查询余额')
92
103
  .action(async ({ session }, target) => {
93
- if (!target)
94
- return '请输入目标用户。';
95
- const [platform, pid] = target.split(':');
96
- if (!platform || !pid)
97
- return '目标用户格式错误。';
98
- const bindings = await ctx.database.get('binding', {
99
- platform,
100
- pid,
101
- }, ['aid']);
102
- if (bindings.length === 0) {
103
- return '未找到该用户。';
104
- }
105
- const uid = bindings[0].aid;
106
- const [user] = await ctx.database.get('user', { id: uid }, ['name']);
107
- let name = user?.name || target;
108
- if (!user?.name) {
109
- const bot = ctx.bots.find(b => b.platform === platform);
110
- if (bot) {
111
- try {
112
- const platformUser = await bot.getUser(pid);
113
- if (platformUser?.name) {
114
- name = platformUser.name;
115
- }
116
- }
117
- catch (e) { }
118
- }
119
- }
120
104
  try {
105
+ const { uid, name } = await resolveUser(target);
121
106
  const monetaryRecords = await ctx.database.get('monetary', { uid });
122
107
  if (monetaryRecords.length === 0) {
123
108
  return `用户 ${name} (UID: ${uid}) 暂无任何货币记录。`;
@@ -129,7 +114,40 @@ function apply(ctx) {
129
114
  return result.trim();
130
115
  }
131
116
  catch (e) {
132
- return `查询失败: ${e.message}`;
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}`;
133
151
  }
134
152
  });
135
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.3",
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",