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 +86 -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,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
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
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
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
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
|
-
|
|
547
|
-
|
|
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
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
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
|
-
|
|
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
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>;
|