koishi-plugin-monetary-bourse 2.0.3-Alpha.12 → 2.1.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.
package/lib/index.js CHANGED
@@ -138,7 +138,7 @@ function apply(ctx, config) {
138
138
  }
139
139
  __name(isMarketOpen, "isMarketOpen");
140
140
  async function getCashBalance(uid, currency) {
141
- if (!uid || typeof uid !== "number" || Number.isNaN(uid)) {
141
+ if (uid === void 0 || uid === null || typeof uid !== "number" || Number.isNaN(uid)) {
142
142
  logger.warn(`getCashBalance: 无效的uid: ${uid}`);
143
143
  return 0;
144
144
  }
@@ -157,7 +157,7 @@ function apply(ctx, config) {
157
157
  }
158
158
  __name(getCashBalance, "getCashBalance");
159
159
  async function changeCashBalance(uid, currency, delta) {
160
- if (!uid || typeof uid !== "number" || Number.isNaN(uid)) {
160
+ if (uid === void 0 || uid === null || typeof uid !== "number" || Number.isNaN(uid)) {
161
161
  logger.warn(`changeCashBalance: 无效的uid: ${uid}`);
162
162
  return false;
163
163
  }
@@ -190,7 +190,7 @@ function apply(ctx, config) {
190
190
  }
191
191
  __name(changeCashBalance, "changeCashBalance");
192
192
  async function getBankDemandBalance(uid, currency) {
193
- if (!uid || typeof uid !== "number" || Number.isNaN(uid)) return 0;
193
+ if (uid === void 0 || uid === null || typeof uid !== "number" || Number.isNaN(uid)) return 0;
194
194
  try {
195
195
  const tables = ctx.database.tables;
196
196
  if (!tables || !("monetary_bank_int" in tables)) {
@@ -211,7 +211,7 @@ function apply(ctx, config) {
211
211
  }
212
212
  __name(getBankDemandBalance, "getBankDemandBalance");
213
213
  async function deductBankDemand(uid, currency, amount) {
214
- if (!uid || typeof uid !== "number" || Number.isNaN(uid) || amount <= 0) return false;
214
+ if (uid === void 0 || uid === null || typeof uid !== "number" || Number.isNaN(uid) || amount <= 0) return false;
215
215
  try {
216
216
  const tables = ctx.database.tables;
217
217
  if (!tables || !("monetary_bank_int" in tables)) return false;
@@ -727,7 +727,7 @@ function apply(ctx, config) {
727
727
  });
728
728
  }
729
729
  } else if (txn.type === "sell") {
730
- if (txn.uid && typeof txn.uid === "number") {
730
+ if (txn.uid !== void 0 && txn.uid !== null && typeof txn.uid === "number" && !Number.isNaN(txn.uid)) {
731
731
  const amount = Number(txn.cost.toFixed(2));
732
732
  const success = await changeCashBalance(txn.uid, config.currency, amount);
733
733
  if (!success) {
@@ -816,18 +816,14 @@ function apply(ctx, config) {
816
816
  }
817
817
  if (!await isMarketOpen()) return "休市中,无法交易。";
818
818
  const visibleUserId = session.userId;
819
- if (!session.user || session.user.id == null) {
819
+ if (!session.user || session.user.id === void 0 || session.user.id === null) {
820
820
  logger.error(`stock.buy: session.user 不存在或 id 为空 user=${session.userId}`);
821
821
  return "无法获取用户ID,请稍后重试。";
822
822
  }
823
- let uid = session.user.id;
823
+ const uid = session.user.id;
824
824
  if (typeof uid !== "number") {
825
- const parsedUid = Number(uid);
826
- if (!parsedUid || isNaN(parsedUid)) {
827
- logger.error(`stock.buy: 无法获取数字UID user=${session.userId}, rawId=${uid}`);
828
- return "无法获取用户ID,请稍后重试。";
829
- }
830
- uid = parsedUid;
825
+ logger.error(`stock.buy: 无法获取数字UID user=${session.userId}, rawId=${uid}`);
826
+ return "无法获取用户ID,请稍后重试。";
831
827
  }
832
828
  const cost = Number((currentPrice * amount).toFixed(2));
833
829
  const payResult = await pay(uid, cost, config.currency);
@@ -842,7 +838,7 @@ function apply(ctx, config) {
842
838
  if (freezeMinutes < config.minFreezeTime) freezeMinutes = config.minFreezeTime;
843
839
  }
844
840
  const freezeMs = freezeMinutes * 60 * 1e3;
845
- const userPendingOrders = await ctx.database.get("bourse_pending", { userId: visibleUserId }, { sort: { endTime: "desc" }, limit: 1 });
841
+ const userPendingOrders = await ctx.database.get("bourse_pending", { userId: visibleUserId, type: "buy" }, { sort: { endTime: "desc" }, limit: 1 });
846
842
  let startTime = /* @__PURE__ */ new Date();
847
843
  if (userPendingOrders.length > 0) {
848
844
  const lastOrderEndTime = userPendingOrders[0].endTime;
@@ -906,18 +902,14 @@ function apply(ctx, config) {
906
902
  }
907
903
  if (!await isMarketOpen()) return "休市中,无法交易。";
908
904
  const visibleUserId = session.userId;
909
- if (!session.user || session.user.id == null) {
905
+ if (!session.user || session.user.id === void 0 || session.user.id === null) {
910
906
  logger.error(`stock.sell: session.user 不存在或 id 为空 user=${session.userId}`);
911
907
  return "无法获取用户ID,请稍后重试。";
912
908
  }
913
- let uid = session.user.id;
909
+ const uid = session.user.id;
914
910
  if (typeof uid !== "number") {
915
- const parsedUid = Number(uid);
916
- if (!parsedUid || isNaN(parsedUid)) {
917
- logger.error(`stock.sell: 无法获取数字UID user=${session.userId}, rawId=${uid}`);
918
- return "无法获取用户ID,请稍后重试。";
919
- }
920
- uid = parsedUid;
911
+ logger.error(`stock.buy: 无法获取数字UID user=${session.userId}, rawId=${uid}`);
912
+ return "无法获取用户ID,请稍后重试。";
921
913
  }
922
914
  const holding = await ctx.database.get("bourse_holding", { userId: visibleUserId, stockId });
923
915
  if (holding.length === 0 || holding[0].amount < amount) {
@@ -952,7 +944,7 @@ function apply(ctx, config) {
952
944
  if (freezeMinutes < config.minFreezeTime) freezeMinutes = config.minFreezeTime;
953
945
  }
954
946
  const freezeMs = freezeMinutes * 60 * 1e3;
955
- const userPendingOrders = await ctx.database.get("bourse_pending", { userId: visibleUserId }, { sort: { endTime: "desc" }, limit: 1 });
947
+ const userPendingOrders = await ctx.database.get("bourse_pending", { userId: visibleUserId, type: "sell" }, { sort: { endTime: "desc" }, limit: 1 });
956
948
  let startTime = /* @__PURE__ */ new Date();
957
949
  if (userPendingOrders.length > 0) {
958
950
  const lastOrderEndTime = userPendingOrders[0].endTime;
@@ -1018,7 +1010,11 @@ function apply(ctx, config) {
1018
1010
  tradeMeta
1019
1011
  );
1020
1012
  });
1021
- ctx.command("stock.my", "我的持仓").action(async ({ session }) => {
1013
+ ctx.command("stock.my", "我的持仓").userFields(["id"]).action(async ({ session }) => {
1014
+ if (!session.user || session.user.id === void 0 || session.user.id === null) {
1015
+ logger.error(`stock.my: session.user 不存在或 id 为空 user=${session.userId}`);
1016
+ return "无法获取用户ID,请稍后重试。";
1017
+ }
1022
1018
  const userId = session.userId;
1023
1019
  const holdings = await ctx.database.get("bourse_holding", { userId });
1024
1020
  const pending = await ctx.database.get("bourse_pending", { userId });