koishi-plugin-prism-neo 0.0.19 → 0.0.20
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 +85 -35
- package/lib/model.d.ts +1 -0
- package/lib/service.d.ts +2 -2
- package/package.json +1 -1
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,9 @@ 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
|
-
|
|
341
|
-
|
|
342
|
-
|
|
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);
|
|
347
371
|
}
|
|
348
372
|
const pendingLogout = kv.get(targetUserId);
|
|
349
373
|
const now = Date.now();
|
|
@@ -519,53 +543,79 @@ async function handleWalletAdd(context, user, amount) {
|
|
|
519
543
|
const { error, userId } = await getTargetUserId(context, user);
|
|
520
544
|
if (error) return error;
|
|
521
545
|
if (!amount) return "请输入数量";
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
546
|
+
return executeWithAutoRegister(
|
|
547
|
+
context,
|
|
548
|
+
user,
|
|
549
|
+
userId,
|
|
550
|
+
() => walletAdd(context, parseInt(amount), userId),
|
|
551
|
+
async (res) => {
|
|
552
|
+
return [
|
|
553
|
+
`为用户 ${await getUserName(context.session, userId)} 增加${context.config.currency}成功`,
|
|
554
|
+
`增加前: ${res.originalBalance}`,
|
|
555
|
+
`增加后: ${res.finalBalance}`
|
|
556
|
+
].join("\n");
|
|
557
|
+
}
|
|
558
|
+
);
|
|
528
559
|
}
|
|
529
560
|
__name(handleWalletAdd, "handleWalletAdd");
|
|
530
561
|
async function handleWalletDeduct(context, user, amount) {
|
|
531
562
|
const { error, userId } = await getTargetUserId(context, user);
|
|
532
563
|
if (error) return error;
|
|
533
564
|
if (!amount) return "请输入数量";
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
565
|
+
return executeWithAutoRegister(
|
|
566
|
+
context,
|
|
567
|
+
user,
|
|
568
|
+
userId,
|
|
569
|
+
() => walletDel(context, parseInt(amount), userId),
|
|
570
|
+
(res) => {
|
|
571
|
+
return [
|
|
572
|
+
`为用户 ${userId} 扣除${context.config.currency}成功`,
|
|
573
|
+
`扣款前: ${res.originalBalance}`,
|
|
574
|
+
`扣款后: ${res.finalBalance}`
|
|
575
|
+
].join("\n");
|
|
576
|
+
}
|
|
577
|
+
);
|
|
540
578
|
}
|
|
541
579
|
__name(handleWalletDeduct, "handleWalletDeduct");
|
|
542
580
|
async function handleCostOverwrite(context, user, amount) {
|
|
543
581
|
const { error, userId } = await getTargetUserId(context, user);
|
|
544
582
|
if (error) return error;
|
|
545
583
|
if (!amount) return "请输入数量";
|
|
546
|
-
|
|
547
|
-
|
|
584
|
+
return executeWithAutoRegister(
|
|
585
|
+
context,
|
|
586
|
+
user,
|
|
587
|
+
userId,
|
|
588
|
+
() => costOverwrite(context, amount, userId),
|
|
589
|
+
() => `为用户 ${userId} 调价成功`
|
|
590
|
+
);
|
|
548
591
|
}
|
|
549
592
|
__name(handleCostOverwrite, "handleCostOverwrite");
|
|
550
593
|
async function handleRedeem(context, code) {
|
|
551
594
|
const { error, userId } = await getTargetUserId(context, null);
|
|
552
595
|
if (error) return error;
|
|
553
596
|
if (!code) return "请输入兑换码";
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
597
|
+
return executeWithAutoRegister(
|
|
598
|
+
context,
|
|
599
|
+
void 0,
|
|
600
|
+
userId,
|
|
601
|
+
() => redeem(context, code, userId),
|
|
602
|
+
(res) => {
|
|
603
|
+
const items = res;
|
|
604
|
+
if (!items || items.length === 0) {
|
|
605
|
+
return "兑换成功,但没有获得任何物品。";
|
|
606
|
+
}
|
|
607
|
+
const message = ["✅ 兑换成功!您获得了以下物品:"];
|
|
608
|
+
items.forEach((item) => {
|
|
609
|
+
let itemName = item.name;
|
|
610
|
+
if (item.assetType === "PASS" && item.durationMs) {
|
|
611
|
+
const days = Math.floor(item.durationMs / (1e3 * 60 * 60 * 24));
|
|
612
|
+
if (days > 0) itemName += ` (${days}天)`;
|
|
613
|
+
}
|
|
614
|
+
message.push(`- ${itemName} x${item.count}`);
|
|
615
|
+
});
|
|
616
|
+
return message.join("\n");
|
|
565
617
|
}
|
|
566
|
-
|
|
567
|
-
});
|
|
568
|
-
return message.join("\n");
|
|
618
|
+
);
|
|
569
619
|
}
|
|
570
620
|
__name(handleRedeem, "handleRedeem");
|
|
571
621
|
async function handleCoin(context, alias) {
|
package/lib/model.d.ts
CHANGED
package/lib/service.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ActionContext } from "./types";
|
|
2
|
-
import { BillingResponse, ListResponse,
|
|
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<
|
|
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>;
|