koishi-plugin-adapter-onebot-multi 1.0.2 → 1.0.4

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 +29 -18
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1460,13 +1460,15 @@ var LoadBalancer = class {
1460
1460
  * 检查群是否应该被管理(根据白名单/黑名单模式)
1461
1461
  */
1462
1462
  shouldManageChannel(channelId) {
1463
+ const normalizedChannelId = String(channelId || "").trim();
1464
+ if (!normalizedChannelId) return false;
1463
1465
  const mode = this.config.channelFilterMode || "blacklist";
1464
1466
  if (mode === "whitelist") {
1465
1467
  const whitelist = this.config.channelWhitelist || [];
1466
- return whitelist.length === 0 || whitelist.includes(channelId);
1468
+ return whitelist.length === 0 || whitelist.includes(normalizedChannelId);
1467
1469
  } else {
1468
1470
  const blacklist = this.config.channelBlacklist || [];
1469
- return !blacklist.includes(channelId);
1471
+ return !blacklist.includes(normalizedChannelId);
1470
1472
  }
1471
1473
  }
1472
1474
  /**
@@ -1519,14 +1521,16 @@ var LoadBalancer = class {
1519
1521
  this.listenerDisposers.push(this.ctx.on("guild-added", (session) => {
1520
1522
  if (!this.config.enabled) return;
1521
1523
  if (session.platform !== "onebot") return;
1524
+ const guildId = String(session.guildId || "").trim();
1525
+ if (!guildId) return;
1522
1526
  const botChannelSet = this.botChannels.get(session.selfId) || /* @__PURE__ */ new Set();
1523
- const isNewChannel = !botChannelSet.has(session.guildId);
1524
- botChannelSet.add(session.guildId);
1527
+ const isNewChannel = !botChannelSet.has(guildId);
1528
+ botChannelSet.add(guildId);
1525
1529
  this.botChannels.set(session.selfId, botChannelSet);
1526
- const channelBots = this.allChannels.get(session.guildId) || /* @__PURE__ */ new Set();
1530
+ const channelBots = this.allChannels.get(guildId) || /* @__PURE__ */ new Set();
1527
1531
  channelBots.add(session.selfId);
1528
- this.allChannels.set(session.guildId, channelBots);
1529
- this.logger.info(`Bot ${session.selfId} 加入群 ${session.guildId}`);
1532
+ this.allChannels.set(guildId, channelBots);
1533
+ this.logger.info(`Bot ${session.selfId} 加入群 ${guildId}`);
1530
1534
  if (isNewChannel) {
1531
1535
  this.scheduleLoadBalancing(1e4);
1532
1536
  }
@@ -1534,20 +1538,22 @@ var LoadBalancer = class {
1534
1538
  this.listenerDisposers.push(this.ctx.on("guild-removed", (session) => {
1535
1539
  if (!this.config.enabled) return;
1536
1540
  if (session.platform !== "onebot") return;
1541
+ const guildId = String(session.guildId || "").trim();
1542
+ if (!guildId) return;
1537
1543
  const botChannelSet = this.botChannels.get(session.selfId);
1538
1544
  if (botChannelSet) {
1539
- botChannelSet.delete(session.guildId);
1545
+ botChannelSet.delete(guildId);
1540
1546
  }
1541
- const channelBots = this.allChannels.get(session.guildId);
1547
+ const channelBots = this.allChannels.get(guildId);
1542
1548
  const hadOtherBots = channelBots && channelBots.size > 1;
1543
1549
  if (channelBots) {
1544
1550
  channelBots.delete(session.selfId);
1545
1551
  if (channelBots.size === 0) {
1546
- this.allChannels.delete(session.guildId);
1547
- this.lastAssignees.delete(session.guildId);
1552
+ this.allChannels.delete(guildId);
1553
+ this.lastAssignees.delete(guildId);
1548
1554
  }
1549
1555
  }
1550
- this.logger.info(`Bot ${session.selfId} 退出群 ${session.guildId}`);
1556
+ this.logger.info(`Bot ${session.selfId} 退出群 ${guildId}`);
1551
1557
  if (hadOtherBots) {
1552
1558
  this.scheduleLoadBalancing(5e3);
1553
1559
  }
@@ -1591,7 +1597,7 @@ var LoadBalancer = class {
1591
1597
  }
1592
1598
  const guilds = await bot.getGuildList();
1593
1599
  const channelIds = new Set(
1594
- Array.from(guilds.data || []).map((g) => g.id)
1600
+ Array.from(guilds.data || []).map((g) => String(g?.id || "").trim()).filter(Boolean)
1595
1601
  );
1596
1602
  this.botChannels.set(bot.selfId, channelIds);
1597
1603
  for (const channelId of channelIds) {
@@ -1642,7 +1648,7 @@ var LoadBalancer = class {
1642
1648
  return;
1643
1649
  }
1644
1650
  const channels = [];
1645
- const prioritySet = new Set(this.config.priorityChannels || []);
1651
+ const prioritySet = new Set((this.config.priorityChannels || []).map((id) => String(id).trim()).filter(Boolean));
1646
1652
  for (const [channelId, bots] of this.allChannels.entries()) {
1647
1653
  channels.push({
1648
1654
  channelId,
@@ -1717,8 +1723,13 @@ var LoadBalancer = class {
1717
1723
  }
1718
1724
  }
1719
1725
  if (assignments.length > 0) {
1720
- for (const { channelId, assignee } of assignments) {
1721
- this.lastAssignees.set(channelId, assignee);
1726
+ const batchSize = 100;
1727
+ for (let i = 0; i < assignments.length; i += batchSize) {
1728
+ const slice = assignments.slice(i, i + batchSize);
1729
+ await Promise.all(slice.map(async ({ channelId, assignee }) => {
1730
+ await this.ctx.database.setChannel("onebot", channelId, { assignee });
1731
+ this.lastAssignees.set(channelId, assignee);
1732
+ }));
1722
1733
  }
1723
1734
  }
1724
1735
  const botsWithData = onlineBots.filter((id) => {
@@ -1800,7 +1811,7 @@ var LoadBalancer = class {
1800
1811
  if (!this.config.enabled) return true;
1801
1812
  if (session.platform !== "onebot") return true;
1802
1813
  if (session.type !== "message") return true;
1803
- const channelId = session.guildId || session.channelId;
1814
+ const channelId = String(session.guildId || session.channelId || "").trim();
1804
1815
  if (!channelId) return true;
1805
1816
  if (!this.shouldManageChannel(channelId)) return true;
1806
1817
  const assignee = this.lastAssignees.get(channelId);
@@ -2120,7 +2131,7 @@ var ConfigStore = class {
2120
2131
  var Config = import_koishi9.Schema.object({}).description("配置已迁移到 data/adapter-onebot-multi/config.yaml");
2121
2132
  var name = "adapter-onebot-multi";
2122
2133
  var inject = {
2123
- required: ["server"],
2134
+ required: ["server", "database"],
2124
2135
  optional: ["console"]
2125
2136
  };
2126
2137
  function apply(ctx, _config) {
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": "1.0.2",
4
+ "version": "1.0.4",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [