koishi-plugin-w-ygo 0.0.2 → 0.0.3

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/apis.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare namespace APIs {
1
+ export declare namespace API {
2
2
  const ygocdb: (query: {
3
3
  search: string;
4
4
  }) => string;
@@ -65,7 +65,6 @@ export declare namespace APIs {
65
65
  amount: number;
66
66
  type: string;
67
67
  };
68
- const showMdmObtain: (obtain: MdmObtain) => string;
69
68
  type MdmCard = {
70
69
  _id: string;
71
70
  gameId: string | number;
package/lib/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import { Context, Schema } from 'koishi';
2
2
  export declare const name = "w-ygo";
3
3
  export interface Config {
4
+ retry: number;
5
+ mdmDefault: boolean;
6
+ showExpiredPacks: boolean;
4
7
  }
5
8
  export declare const Config: Schema<Config>;
6
- export declare function apply(ctx: Context): void;
9
+ export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -28,11 +28,11 @@ module.exports = __toCommonJS(src_exports);
28
28
  var import_koishi = require("koishi");
29
29
 
30
30
  // src/apis.ts
31
- var APIs;
32
- ((APIs2) => {
33
- APIs2.ygocdb = /* @__PURE__ */ __name((query) => `https://ygocdb.com/api/v0/?search=${encodeURIComponent(query.search)}`, "ygocdb");
34
- APIs2.mdmcards = /* @__PURE__ */ __name((query) => `https://www.masterduelmeta.com/api/v1/cards?search=${encodeURIComponent(query.name)}`, "mdmcards");
35
- APIs2.showMdmBanStatus = /* @__PURE__ */ __name((banStatus) => {
31
+ var API;
32
+ ((API2) => {
33
+ API2.ygocdb = /* @__PURE__ */ __name((query) => `https://ygocdb.com/api/v0/?search=${encodeURIComponent(query.search)}`, "ygocdb");
34
+ API2.mdmcards = /* @__PURE__ */ __name((query) => `https://www.masterduelmeta.com/api/v1/cards?search=${encodeURIComponent(query.name)}`, "mdmcards");
35
+ API2.showMdmBanStatus = /* @__PURE__ */ __name((banStatus) => {
36
36
  if (!banStatus) return "无";
37
37
  switch (banStatus) {
38
38
  case "Limited 2":
@@ -43,25 +43,33 @@ var APIs;
43
43
  return "禁止";
44
44
  }
45
45
  }, "showMdmBanStatus");
46
- APIs2.showMdmObtain = /* @__PURE__ */ __name((obtain) => {
47
- const expired = obtain.source.expires && new Date(obtain.source.expires) > /* @__PURE__ */ new Date();
48
- return `${obtain.source.name}${expired ? "" : " (过期) "}`;
49
- }, "showMdmObtain");
50
- APIs2.momobako = /* @__PURE__ */ __name((query) => `https://cdn.233.momobako.com/ygopro/pics/${query.id}.jpg`, "momobako");
51
- })(APIs || (APIs = {}));
46
+ API2.momobako = /* @__PURE__ */ __name((query) => `https://cdn.233.momobako.com/ygopro/pics/${query.id}.jpg`, "momobako");
47
+ })(API || (API = {}));
52
48
 
53
49
  // src/index.tsx
54
50
  var import_jsx_runtime = require("@satorijs/element/jsx-runtime");
55
51
  var name = "w-ygo";
56
- var Config = import_koishi.Schema.object({});
57
- function apply(ctx) {
58
- ctx.command("ygo <search:text>", "通过 ygocdb.com 搜索游戏王卡片").option("mdm", "-m 从 Master Duel Meta 获取额外信息(会消耗更多时间)", { fallback: true }).action(async ({ session, options }, search) => {
59
- const { result: cards } = await ctx.http.get(APIs.ygocdb({ search }));
52
+ var Config = import_koishi.Schema.object({
53
+ retry: import_koishi.Schema.number().default(3).description("API 请求失败时的重试次数"),
54
+ mdmDefault: import_koishi.Schema.boolean().default(false).description("是否默认启用 Master Duel Meta 信息"),
55
+ showExpiredPacks: import_koishi.Schema.boolean().default(false).description("是否显示过期的 MD 卡包")
56
+ });
57
+ function apply(ctx, config) {
58
+ const get = /* @__PURE__ */ __name(async (url, { retry = config.retry } = {}) => {
59
+ try {
60
+ return await ctx.http.get(url);
61
+ } catch (error) {
62
+ if (retry > 0) return get(url, { retry: retry - 1 });
63
+ throw error;
64
+ }
65
+ }, "get");
66
+ ctx.command("ygo <search:text>", "通过 ygocdb.com 搜索游戏王卡片").option("mdm", "-m 从 Master Duel Meta 获取额外信息(会消耗更多时间)", { fallback: config.mdmDefault }).option("mdm", "-M", { value: false }).action(async ({ session, options }, search) => {
67
+ const { result: cards } = await get(API.ygocdb({ search }));
60
68
  if (!cards.length) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: "未找到相关卡片" });
61
69
  const [card] = cards;
62
70
  let mdminfo = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: "从 MDM 获取信息失败" });
63
71
  if (options.mdm) {
64
- const mdmcards = await ctx.http.get(APIs.mdmcards({ name: card.en_name }));
72
+ const mdmcards = await get(API.mdmcards({ name: card.en_name }));
65
73
  const gameId = String(card.cid);
66
74
  const mdmcard = mdmcards.find((it) => String(it.gameId) === gameId);
67
75
  if (mdmcard) {
@@ -72,26 +80,29 @@ function apply(ctx) {
72
80
  ] }),
73
81
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { children: [
74
82
  "[MD 禁限] ",
75
- APIs.showMdmBanStatus(mdmcard.banStatus)
83
+ API.showMdmBanStatus(mdmcard.banStatus)
76
84
  ] }),
77
85
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { children: [
78
86
  "[OCG 禁限] ",
79
- APIs.showMdmBanStatus(mdmcard.ocgBanStatus)
87
+ API.showMdmBanStatus(mdmcard.ocgBanStatus)
80
88
  ] }),
81
89
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { children: [
82
90
  "[TCG 禁限] ",
83
- APIs.showMdmBanStatus(mdmcard.tcgBanStatus)
91
+ API.showMdmBanStatus(mdmcard.tcgBanStatus)
84
92
  ] }),
85
93
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { children: [
86
94
  "[MD 卡包] ",
87
- mdmcard.obtain.map(APIs.showMdmObtain).join(", ") || "无"
95
+ mdmcard.obtain.map((it) => {
96
+ const expired = !!it.source.expires && new Date(it.source.expires) < /* @__PURE__ */ new Date();
97
+ return { ...it, expired };
98
+ }).filter((it) => !it.expired || config.showExpiredPacks).sort((i1, i2) => +i1.expired - +i2.expired).map((obtain) => `${obtain.source.name}${obtain.expired ? " (过期)" : ""}`).join(", ") || "无"
88
99
  ] })
89
100
  ] });
90
101
  }
91
102
  }
92
103
  session.send(/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("message", { forward: true, children: [
93
104
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("message", { children: [
94
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("img", { src: APIs.momobako({ id: card.id }), alt: card.en_name }),
105
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("img", { src: API.momobako({ id: card.id }), alt: card.en_name }),
95
106
  /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("p", { children: [
96
107
  "[MD 卡名] ",
97
108
  card.md_name || "无"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-w-ygo",
3
3
  "description": "WillBot: YuGiOh function",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [