koishi-plugin-adapter-onebot-multi 2.0.0 → 2.0.1

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 (2) hide show
  1. package/lib/index.js +48 -12
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -2184,6 +2184,14 @@ function apply(ctx, _config) {
2184
2184
  const managedBotIds = /* @__PURE__ */ new Set();
2185
2185
  const botForks = /* @__PURE__ */ new Map();
2186
2186
  const getRuntimeConfig = /* @__PURE__ */ __name(() => configStore.getRuntime(), "getRuntimeConfig");
2187
+ const createPendingSelfId = /* @__PURE__ */ __name(() => {
2188
+ let candidate = `pending_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 6)}`;
2189
+ const exists = /* @__PURE__ */ __name((id) => configStore.getBots().some((bot) => bot.selfId === id), "exists");
2190
+ while (exists(candidate)) {
2191
+ candidate = `pending_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 6)}`;
2192
+ }
2193
+ return candidate;
2194
+ }, "createPendingSelfId");
2187
2195
  const isReverseTemplateBot = /* @__PURE__ */ __name((bot) => {
2188
2196
  return (bot.protocol || "ws-reverse") === "ws-reverse" && String(bot.selfId || "").startsWith("pending_");
2189
2197
  }, "isReverseTemplateBot");
@@ -2194,6 +2202,31 @@ function apply(ctx, _config) {
2194
2202
  return (bot.path || "/onebot") === path2;
2195
2203
  });
2196
2204
  }, "findReverseTemplate");
2205
+ const findReverseSeedBot = /* @__PURE__ */ __name((path2) => {
2206
+ const bots = configStore.getBots().filter((bot) => {
2207
+ if (bot.enabled === false) return false;
2208
+ return (bot.protocol || "ws-reverse") === "ws-reverse" && (bot.path || "/onebot") === path2;
2209
+ });
2210
+ return bots.find(isReverseTemplateBot) || bots[0];
2211
+ }, "findReverseSeedBot");
2212
+ const ensureReverseTemplateRecord = /* @__PURE__ */ __name((path2) => {
2213
+ const normalizedPath = path2 || "/onebot";
2214
+ if (findReverseTemplate(normalizedPath)) return;
2215
+ const seedBot = findReverseSeedBot(normalizedPath);
2216
+ if (!seedBot) return;
2217
+ configStore.setBots([
2218
+ ...configStore.getBots(),
2219
+ {
2220
+ selfId: createPendingSelfId(),
2221
+ token: seedBot.token,
2222
+ protocol: "ws-reverse",
2223
+ path: normalizedPath,
2224
+ name: "服务器",
2225
+ enabled: true
2226
+ }
2227
+ ]);
2228
+ logger.info(`已为 path=${normalizedPath} 自动补齐 ws 服务器模板记录`);
2229
+ }, "ensureReverseTemplateRecord");
2197
2230
  const getGlobalConfig = /* @__PURE__ */ __name(() => {
2198
2231
  const runtime = getRuntimeConfig();
2199
2232
  return {
@@ -2306,16 +2339,20 @@ function apply(ctx, _config) {
2306
2339
  return ctx.bots.find((bot) => bot.selfId === normalizedSelfId && bot.platform === "onebot");
2307
2340
  }
2308
2341
  const template = findReverseTemplate(normalizedPath);
2309
- if (!template) {
2310
- logger.warn(`未找到 path=${normalizedPath} 的 ws-reverse 模板,拒绝动态创建 Bot ${normalizedSelfId}`);
2342
+ const seedBot = template || findReverseSeedBot(normalizedPath);
2343
+ if (!seedBot) {
2344
+ logger.warn(`未找到 path=${normalizedPath} 的 ws-reverse 配置,拒绝动态创建 Bot ${normalizedSelfId}`);
2311
2345
  return;
2312
2346
  }
2347
+ if (!template) {
2348
+ ensureReverseTemplateRecord(normalizedPath);
2349
+ }
2313
2350
  const dynamicBot = {
2314
2351
  selfId: normalizedSelfId,
2315
- token: template.token,
2352
+ token: seedBot.token,
2316
2353
  protocol: "ws-reverse",
2317
2354
  path: normalizedPath,
2318
- name: template.name && template.name !== "服务器" ? template.name : void 0,
2355
+ name: seedBot.name && seedBot.name !== "服务器" ? seedBot.name : void 0,
2319
2356
  enabled: true
2320
2357
  };
2321
2358
  configStore.setBots([...configStore.getBots(), dynamicBot]);
@@ -2347,6 +2384,13 @@ function apply(ctx, _config) {
2347
2384
  startBot(target);
2348
2385
  }, "restartBot");
2349
2386
  const syncBots = /* @__PURE__ */ __name(async () => {
2387
+ const currentBots = configStore.getBots();
2388
+ const reversePaths = new Set(
2389
+ currentBots.filter((bot) => (bot.protocol || "ws-reverse") === "ws-reverse").map((bot) => bot.path || "/onebot")
2390
+ );
2391
+ for (const path2 of reversePaths) {
2392
+ ensureReverseTemplateRecord(path2);
2393
+ }
2350
2394
  const bots = configStore.getBots();
2351
2395
  const enabledIds = new Set(bots.filter((b) => b.enabled !== false).map((b) => b.selfId));
2352
2396
  for (const selfId of Array.from(managedBotIds)) {
@@ -2362,14 +2406,6 @@ function apply(ctx, _config) {
2362
2406
  }
2363
2407
  }, "syncBots");
2364
2408
  const upsertBot = /* @__PURE__ */ __name((nextBot, oldSelfId) => {
2365
- const createPendingSelfId = /* @__PURE__ */ __name(() => {
2366
- let candidate = `pending_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 6)}`;
2367
- const exists = /* @__PURE__ */ __name((id) => configStore.getBots().some((bot) => bot.selfId === id), "exists");
2368
- while (exists(candidate)) {
2369
- candidate = `pending_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 6)}`;
2370
- }
2371
- return candidate;
2372
- }, "createPendingSelfId");
2373
2409
  const inputSelfId = String(nextBot.selfId || "").trim();
2374
2410
  const nextSelfId = inputSelfId || oldSelfId || createPendingSelfId();
2375
2411
  const all = configStore.getBots();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-adapter-onebot-multi",
3
3
  "description": "奶龙bot定制版onebot适配器,支持自动负载均衡与黑白名单,控制台侧边栏管理",
4
- "version": "2.0.0",
4
+ "version": "2.0.1",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [