koishi-plugin-monetary-admin 1.1.0 → 1.3.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 +69 -10
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -33,46 +33,93 @@ function apply(ctx) {
33
33
  }
34
34
  return { uid, name };
35
35
  }
36
- ctx.command('monetary.add <target:user> <currency:string> <amount:number>', '给目标用户添加货币', { authority: 5 })
36
+ ctx.command('monetary.add [target:user] <currency:string> <amount:number>', '给目标用户添加货币', { authority: 5 })
37
+ .userFields(['id'])
37
38
  .action(async ({ session }, target, currency, amount) => {
38
39
  try {
39
- const { uid, name } = await resolveUser(target);
40
+ let uid, name;
41
+ if (!target) {
42
+ // 如果没有指定目标,则给自己添加
43
+ if (!session?.user?.id)
44
+ return '无法获取您的用户信息。';
45
+ uid = session.user.id;
46
+ name = session.username || String(uid);
47
+ }
48
+ else {
49
+ const resolved = await resolveUser(target);
50
+ uid = resolved.uid;
51
+ name = resolved.name;
52
+ }
40
53
  if (!currency)
41
54
  return '请输入货币类型。';
42
55
  if (!amount)
43
56
  return '请输入金额。';
44
57
  if (amount <= 0)
45
58
  return '金额必须为正数。';
46
- await ctx.monetary.gain(uid, amount, currency);
59
+ // 直接使用数据库 upsert,避免 monetary.gain() bug
60
+ await ctx.database.upsert('monetary', (row) => [{
61
+ uid,
62
+ currency,
63
+ value: koishi_1.$.add(row.value, amount),
64
+ }], ['uid', 'currency']);
47
65
  return `成功给用户 ${name} (UID: ${uid}) 添加了 ${amount} ${currency}。`;
48
66
  }
49
67
  catch (e) {
50
68
  return typeof e === 'string' ? e : `添加失败: ${e.message}`;
51
69
  }
52
70
  });
53
- ctx.command('monetary.reduce <target:user> <currency:string> <amount:number>', '扣除目标用户货币', { authority: 5 })
71
+ ctx.command('monetary.reduce [target:user] <currency:string> <amount:number>', '扣除目标用户货币', { authority: 5 })
54
72
  .alias('扣款')
73
+ .userFields(['id'])
55
74
  .action(async ({ session }, target, currency, amount) => {
56
75
  try {
57
- const { uid, name } = await resolveUser(target);
76
+ let uid, name;
77
+ if (!target) {
78
+ if (!session?.user?.id)
79
+ return '无法获取您的用户信息。';
80
+ uid = session.user.id;
81
+ name = session.username || String(uid);
82
+ }
83
+ else {
84
+ const resolved = await resolveUser(target);
85
+ uid = resolved.uid;
86
+ name = resolved.name;
87
+ }
58
88
  if (!currency)
59
89
  return '请输入货币类型。';
60
90
  if (!amount)
61
91
  return '请输入金额。';
62
92
  if (amount <= 0)
63
93
  return '金额必须为正数。';
64
- await ctx.monetary.gain(uid, -amount, currency);
94
+ // 直接使用数据库 upsert,避免 monetary.gain() bug
95
+ await ctx.database.upsert('monetary', (row) => [{
96
+ uid,
97
+ currency,
98
+ value: koishi_1.$.sub(row.value, amount),
99
+ }], ['uid', 'currency']);
65
100
  return `成功从用户 ${name} (UID: ${uid}) 扣除了 ${amount} ${currency}。`;
66
101
  }
67
102
  catch (e) {
68
103
  return typeof e === 'string' ? e : `扣除失败: ${e.message}`;
69
104
  }
70
105
  });
71
- ctx.command('monetary.clear <target:user> <currency:string>', '清零目标用户指定货币', { authority: 5 })
106
+ ctx.command('monetary.clear [target:user] <currency:string>', '清零目标用户指定货币', { authority: 5 })
72
107
  .alias('清零')
108
+ .userFields(['id'])
73
109
  .action(async ({ session }, target, currency) => {
74
110
  try {
75
- const { uid, name } = await resolveUser(target);
111
+ let uid, name;
112
+ if (!target) {
113
+ if (!session?.user?.id)
114
+ return '无法获取您的用户信息。';
115
+ uid = session.user.id;
116
+ name = session.username || String(uid);
117
+ }
118
+ else {
119
+ const resolved = await resolveUser(target);
120
+ uid = resolved.uid;
121
+ name = resolved.name;
122
+ }
76
123
  if (!currency)
77
124
  return '请输入货币类型。';
78
125
  await ctx.database.set('monetary', { uid, currency }, { value: 0 });
@@ -98,11 +145,23 @@ function apply(ctx) {
98
145
  return typeof e === 'string' ? e : `删除失败: ${e.message}`;
99
146
  }
100
147
  });
101
- ctx.command('monetary.balance <target:user>', '查询目标用户的货币余额', { authority: 1 })
148
+ ctx.command('monetary.balance [target:user]', '查询目标用户的货币余额', { authority: 1 })
102
149
  .alias('查询余额')
150
+ .userFields(['id'])
103
151
  .action(async ({ session }, target) => {
104
152
  try {
105
- const { uid, name } = await resolveUser(target);
153
+ let uid, name;
154
+ if (!target) {
155
+ if (!session?.user?.id)
156
+ return '无法获取您的用户信息。';
157
+ uid = session.user.id;
158
+ name = session.username || '您';
159
+ }
160
+ else {
161
+ const resolved = await resolveUser(target);
162
+ uid = resolved.uid;
163
+ name = resolved.name;
164
+ }
106
165
  const monetaryRecords = await ctx.database.get('monetary', { uid });
107
166
  if (monetaryRecords.length === 0) {
108
167
  return `用户 ${name} (UID: ${uid}) 暂无任何货币记录。`;
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.1.0",
4
+ "version": "1.3.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [