koishi-plugin-starfx-bot 0.13.1 → 0.13.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.
Files changed (3) hide show
  1. package/lib/index.js +17 -20
  2. package/package.json +1 -1
  3. package/readme.md +2 -0
package/lib/index.js CHANGED
@@ -72,25 +72,22 @@ async function getRecord(cfg, gid, tag) {
72
72
  if (!import_fs.default.existsSync(recordDir)) return null;
73
73
  const files = import_fs.default.readdirSync(recordDir).filter((file) => /\.(png|jpe?g|webp|gif)$/i.test(file));
74
74
  if (!files.length) return null;
75
- let weightedFiles = [];
76
- if (tag && import_fs.default.existsSync(tagConfigPath)) {
77
- const tagConfigJson = JSON.parse(import_fs.default.readFileSync(tagConfigPath, "utf8") || "{}");
78
- files.forEach((file) => {
79
- const name2 = import_node_path.default.parse(file).name;
80
- const tags = tagConfigJson[name2] || [];
81
- if (tags.includes(tag)) {
82
- for (let i = 0; i < cfg.tagWeight; i++) {
83
- weightedFiles.push(file);
84
- }
85
- } else {
86
- weightedFiles.push(file);
87
- }
88
- });
89
- } else {
90
- weightedFiles = files;
75
+ const tagConfigJson = import_fs.default.existsSync(tagConfigPath) ? JSON.parse(import_fs.default.readFileSync(tagConfigPath, "utf8") || "{}") : {};
76
+ const weighted = files.map((file) => {
77
+ const name2 = import_node_path.default.parse(file).name;
78
+ const tags = tagConfigJson[name2] || [];
79
+ const weight = tag && tags.includes(tag) ? cfg.tagWeight : 1;
80
+ return { file, weight };
81
+ });
82
+ const totalWeight = weighted.reduce((acc, cur) => acc + cur.weight, 0);
83
+ let rand = Math.random() * totalWeight;
84
+ for (const item of weighted) {
85
+ rand -= item.weight;
86
+ if (rand <= 0) {
87
+ return import_node_path.default.join(recordDir, item.file);
88
+ }
91
89
  }
92
- const selected = import_koishi.Random.pick(weightedFiles);
93
- return selected ? import_node_path.default.join(recordDir, selected) : null;
90
+ return null;
94
91
  }
95
92
  __name(getRecord, "getRecord");
96
93
  function getTodayPrefix() {
@@ -505,13 +502,13 @@ function apply(ctx, cfg) {
505
502
  if (cfg.openLock) {
506
503
  ctx.command("封印 [param]").action(async ({ session }, param) => {
507
504
  if (detectControl(controlJson, session.guildId, "lock"))
508
- return await drawLock(ctx, await getImageSrc(session, param));
505
+ await session.send(await drawLock(ctx, await getImageSrc(session, param)));
509
506
  });
510
507
  }
511
508
  if (cfg.openSold) {
512
509
  ctx.command("卖掉了 [param]").action(async ({ session }, param) => {
513
510
  if (detectControl(controlJson, session.guildId, "sold"))
514
- return await drawSold(ctx, await getImageSrc(session, param));
511
+ await session.send(await drawSold(ctx, await getImageSrc(session, param)));
515
512
  });
516
513
  }
517
514
  if (cfg.roll) {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "contributors": [
5
5
  "StarFreedomX <starfreedomx@outlook.com>"
6
6
  ],
7
- "version": "0.13.1",
7
+ "version": "0.13.3",
8
8
  "main": "lib/index.js",
9
9
  "typings": "lib/index.d.ts",
10
10
  "files": [
package/readme.md CHANGED
@@ -99,3 +99,5 @@ StarFreedomX机器人的小功能,自用
99
99
  | `0.12.0` | 加入echo功能 |
100
100
  | `0.13.0` | 语录支持tag以及调整权重 |
101
101
  | `0.13.1` | 修复pure类型绘图名称错误 |
102
+ | `0.13.2` | 优化了tag在权重很大情况下的筛选效率 |
103
+ | `0.13.3` | 不再return图片对象,防止$()可能导致的参数溢出 |