koishi-plugin-prism-neo 0.0.19 → 0.0.21

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 CHANGED
@@ -303,6 +303,34 @@ var formatBilling = /* @__PURE__ */ __name((res, currency) => {
303
303
  }
304
304
  return message.join("\n");
305
305
  }, "formatBilling");
306
+ async function executeWithAutoRegister(context, userArg, userId, action, formatter) {
307
+ try {
308
+ const res = await action();
309
+ return await formatter(res);
310
+ } catch (e) {
311
+ const code = e?.response?.data?.errcode;
312
+ if (code === "USER_NOT_FOUND") {
313
+ let message = "用户不存在,尝试注册\n";
314
+ message += await handleRegisterCmd(context, userArg);
315
+ message += "\n";
316
+ try {
317
+ const res = await action();
318
+ message += await formatter(res);
319
+ return message;
320
+ } catch (retryError) {
321
+ const apiMessage = retryError?.response?.data?.message;
322
+ if (apiMessage) {
323
+ message += apiMessage;
324
+ } else {
325
+ message += "操作失败。";
326
+ }
327
+ return message;
328
+ }
329
+ }
330
+ throw e;
331
+ }
332
+ }
333
+ __name(executeWithAutoRegister, "executeWithAutoRegister");
306
334
  async function getTargetUserId(context, user) {
307
335
  if (user) {
308
336
  if (!await context.ctx.permissions.check(context.config.admin, context.session)) {
@@ -337,13 +365,10 @@ async function handleLogoutCmd(context, user) {
337
365
  if (error) return error;
338
366
  if (!context.config.logoutConfirmation) {
339
367
  const res = await logout(context, targetUserId);
340
- const messagePrefix = user ? `✅ 已为用户 ${await getUserName(context.session, targetUserId)} 退场` : "✅ 退场成功";
341
- return [
342
- messagePrefix,
343
- `入场时间: ${formatDateTime(res.session.createdAt)}`,
344
- `离场时间: ${formatDateTime(res.session.closedAt)}`,
345
- `消费: ${res.session.finalCost} ${context.config.currency}`
346
- ].join("\n");
368
+ let message = user ? `✅ 已为用户 ${await getUserName(context.session, targetUserId)} 退场` : "✅ 退场成功";
369
+ message += "\n";
370
+ message += formatBilling(res, context.config.currency);
371
+ return message;
347
372
  }
348
373
  const pendingLogout = kv.get(targetUserId);
349
374
  const now = Date.now();
@@ -519,53 +544,79 @@ async function handleWalletAdd(context, user, amount) {
519
544
  const { error, userId } = await getTargetUserId(context, user);
520
545
  if (error) return error;
521
546
  if (!amount) return "请输入数量";
522
- const res = await walletAdd(context, parseInt(amount), userId);
523
- return [
524
- `为用户 ${await getUserName(context.session, userId)} 增加${context.config.currency}成功`,
525
- `增加前: ${res.originalBalance}`,
526
- `增加后: ${res.finalBalance}`
527
- ].join("\n");
547
+ return executeWithAutoRegister(
548
+ context,
549
+ user,
550
+ userId,
551
+ () => walletAdd(context, parseInt(amount), userId),
552
+ async (res) => {
553
+ return [
554
+ `为用户 ${await getUserName(context.session, userId)} 增加${context.config.currency}成功`,
555
+ `增加前: ${res.originalBalance}`,
556
+ `增加后: ${res.finalBalance}`
557
+ ].join("\n");
558
+ }
559
+ );
528
560
  }
529
561
  __name(handleWalletAdd, "handleWalletAdd");
530
562
  async function handleWalletDeduct(context, user, amount) {
531
563
  const { error, userId } = await getTargetUserId(context, user);
532
564
  if (error) return error;
533
565
  if (!amount) return "请输入数量";
534
- const res = await walletDel(context, parseInt(amount), userId);
535
- return [
536
- `为用户 ${userId} 扣除${context.config.currency}成功`,
537
- `扣款前: ${res.originalBalance}`,
538
- `扣款后: ${res.finalBalance}`
539
- ].join("\n");
566
+ return executeWithAutoRegister(
567
+ context,
568
+ user,
569
+ userId,
570
+ () => walletDel(context, parseInt(amount), userId),
571
+ (res) => {
572
+ return [
573
+ `为用户 ${userId} 扣除${context.config.currency}成功`,
574
+ `扣款前: ${res.originalBalance}`,
575
+ `扣款后: ${res.finalBalance}`
576
+ ].join("\n");
577
+ }
578
+ );
540
579
  }
541
580
  __name(handleWalletDeduct, "handleWalletDeduct");
542
581
  async function handleCostOverwrite(context, user, amount) {
543
582
  const { error, userId } = await getTargetUserId(context, user);
544
583
  if (error) return error;
545
584
  if (!amount) return "请输入数量";
546
- await costOverwrite(context, amount, userId);
547
- return `为用户 ${userId} 调价成功`;
585
+ return executeWithAutoRegister(
586
+ context,
587
+ user,
588
+ userId,
589
+ () => costOverwrite(context, amount, userId),
590
+ () => `为用户 ${userId} 调价成功`
591
+ );
548
592
  }
549
593
  __name(handleCostOverwrite, "handleCostOverwrite");
550
594
  async function handleRedeem(context, code) {
551
595
  const { error, userId } = await getTargetUserId(context, null);
552
596
  if (error) return error;
553
597
  if (!code) return "请输入兑换码";
554
- const res = await redeem(context, code, userId);
555
- const items = res;
556
- if (!items || items.length === 0) {
557
- return "兑换成功,但没有获得任何物品。";
558
- }
559
- const message = ["✅ 兑换成功!您获得了以下物品:"];
560
- items.forEach((item) => {
561
- let itemName = item.name;
562
- if (item.assetType === "PASS" && item.durationMs) {
563
- const days = Math.floor(item.durationMs / (1e3 * 60 * 60 * 24));
564
- if (days > 0) itemName += ` (${days}天)`;
598
+ return executeWithAutoRegister(
599
+ context,
600
+ void 0,
601
+ userId,
602
+ () => redeem(context, code, userId),
603
+ (res) => {
604
+ const items = res;
605
+ if (!items || items.length === 0) {
606
+ return "兑换成功,但没有获得任何物品。";
607
+ }
608
+ const message = ["✅ 兑换成功!您获得了以下物品:"];
609
+ items.forEach((item) => {
610
+ let itemName = item.name;
611
+ if (item.assetType === "PASS" && item.durationMs) {
612
+ const days = Math.floor(item.durationMs / (1e3 * 60 * 60 * 24));
613
+ if (days > 0) itemName += ` (${days}天)`;
614
+ }
615
+ message.push(`- ${itemName} x${item.count}`);
616
+ });
617
+ return message.join("\n");
565
618
  }
566
- message.push(`- ${itemName} x${item.count}`);
567
- });
568
- return message.join("\n");
619
+ );
569
620
  }
570
621
  __name(handleRedeem, "handleRedeem");
571
622
  async function handleCoin(context, alias) {
package/lib/model.d.ts CHANGED
@@ -119,6 +119,7 @@ export interface BillingResponse {
119
119
  }
120
120
  export interface ApiErrorData {
121
121
  message: string;
122
+ errcode: string;
122
123
  }
123
124
  export interface ApiError {
124
125
  response: {
package/lib/service.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { ActionContext } from "./types";
2
- import { BillingResponse, ListResponse, LogoutResponse, UserAsset, Wallet } from "./model";
2
+ import { BillingResponse, ListResponse, UserAsset, Wallet } from "./model";
3
3
  export declare function register({ ctx, config }: ActionContext, userId: string): Promise<unknown>;
4
4
  export declare function login({ ctx, config }: ActionContext, userId: string): Promise<unknown>;
5
- export declare function logout({ ctx, config }: ActionContext, userId: string): Promise<LogoutResponse>;
5
+ export declare function logout({ ctx, config }: ActionContext, userId: string): Promise<BillingResponse>;
6
6
  export declare function billing({ ctx, config }: ActionContext, userId: string): Promise<BillingResponse>;
7
7
  export declare function list({ ctx, config }: ActionContext): Promise<ListResponse>;
8
8
  export declare function wallet({ ctx, config }: ActionContext, userId: string): Promise<Wallet>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism-neo",
3
3
  "description": "prism koishi前端",
4
- "version": "0.0.19",
4
+ "version": "0.0.21",
5
5
  "main": "lib/index.js",
6
6
  "files": [
7
7
  "lib",