koishi-plugin-prism 0.5.3 → 0.6.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/README.md CHANGED
@@ -26,6 +26,7 @@ Koishi 里只填怎么连到店铺:
26
26
  | `baseUrl` | `string` | **必填**。PRiSM Next Server 的访问基准 URL(如 `https://prism-mmw.neri.moe`)。 |
27
27
  | `shopCode` | `string` | 可选。店铺公开编号;接入凭据已经指明店铺,**不需要填**。旧配置保留也能用。 |
28
28
  | `integrationToken` | `string` | **必填**。从 PRiSM 网页后台生成的 Integration API 令牌。 |
29
+ | `syncNames` | `boolean` | 可选,默认开。连上后约一分钟、之后每 6 小时读 Bot 所在每个群的成员昵称发给 PRiSM,补全本店玩家的名字;关掉后只在玩家自己发命令时更新。 |
29
30
 
30
31
  其余一切都在 PRiSM 网页里设置:每个令牌对应一个 Bot,Bot 在店铺里是一个**资源**。在**商户工作台 → 资源 → 这个 Bot → Bot 设置**里可以改:
31
32
 
@@ -41,8 +42,8 @@ Koishi 里只填怎么连到店铺:
41
42
  | 账单私聊店员 | 玩家退场或店员覆盖结账后,把同一份账单私聊给同一聊天平台上的店员。 |
42
43
  | 门锁 | `lock` 打开的门(带门锁能力的资源);店里只有一扇门时默认就是它。没有门时 `lock` 回复本店还没有设置。 |
43
44
  | 入场后自动获取门锁密码 | 默认关闭。打开后 `login`/`入场` 成功时顺带生成一个 8 位 TTLock 临时密码(有效期 3 分钟)一并回复;门锁失败只多一行提示,不会让入场失败。没有门锁时不生效。 |
44
- | 播报与提醒 | 只有一处设置:**播报群**(Koishi 的 `平台:频道` 格式,如群 `onebot:群号`、私聊 `onebot:private:QQ号`)和**提醒提前量**(分钟)。闲置提醒和店内动态都发到这一处,不再分成两个群。 |
45
- | 播报这些 | 在后台按事件类型逐项勾选(`announceEvents`),平台按勾选只发相应的动态,写好要说的话并直接发到播报群(其中的玩家由插件换成群里的称呼)。分三组:**现场发生的事**(开机、关机、刷卡、上机、机台投币、开局扣币、投币计数清零,来自机台和 Home Assistant 的回报,不论在哪里操作的都只说一次)、**通过平台的操作**(通过平台开机/关机、闲置自动关机、通过平台刷卡、通过平台投币,只说明有人在平台上发起了它)和**提醒**(闲置关机提醒,提前量为 0 时不提醒)。这个 Bot 自己执行的命令已经回复过,不再播报。 |
45
+ | 播报与提醒 | 在后台 Bot 设置的「播报」里建**播报组**:每组是一种听众,有自己的目的地(Koishi 的 `平台:频道` 格式,如群 `onebot:群号`、私聊 `onebot:private:QQ号`,私聊店员也这样写),以及一整张事件表:每个事件播不播、只听哪些来源、用什么文案。没有播报组就什么都不播。另有闲置关机提醒的**提前量**(分钟),为 0 时不提醒。 |
46
+ | 播报这些 | 平台按播报组决定发什么、发到哪里,并按那一组的文案写好要说的话,插件直接发出(其中的玩家由插件换成群里的称呼)。事件按话题分组,每个话题里分**尝试**(有人要求做某件事以及结果:已发出、被拒绝、失败,比如开机、投币、虚拟刷卡、结账)和**事实**(真的发生了的事,比如设备开机、游戏读到卡、结账完成),另有闲置关机提醒和系统维护两种**提醒**。这个 Bot 自己执行的命令已经回复过,不再播报。 |
46
47
 
47
48
  播报与闲置提醒直接通过对应平台的第一个在线机器人发送,不依赖 Koishi 的数据库、频道分配或 silent 标记,是否播报由 PRiSM 设置决定。群聊填写 `平台:频道`,私聊填写 `平台:private:用户ID`;频道 ID 内的冒号保持原样。连续播报不会修改缓存的频道列表,单个目标发送失败会记录目标及错误并继续其他目标和后续播报。相同平台有多个机器人时,第一个在线账号须能访问目标群;离线时不缓存补发。
48
49
 
@@ -166,6 +167,12 @@ npm version minor --no-git-tag-version
166
167
 
167
168
  绑定支持 Koishi 提供的各适配器标识,身份值保持字符串,不限定数字格式。店员和账单私聊按「平台标识 + 身份值」认人,相同号码的其他平台不会继承权限或通知。管理员代操作使用目标平台标识,禁止通过当前适配器误操作另一个平台。`/list` 只用当前平台的身份查询昵称,缺失时使用后台名称。源码和 npm 发行包需要分别核对版本。
168
169
 
170
+ ### 0.6.0
171
+
172
+ - 玩家的名字按平台分开存:说到一个玩家时用他在本平台的名字(平台在每个身份上保存的称呼),一个人绑了两个平台也不会被来回改名。
173
+ - 批量补名:连上平台约一分钟后、之后每 6 小时,读 Bot 所在每个群的成员列表,把成员的昵称分批发给平台,让从旧系统导入的「Player 75」这类占位名、以及从没用过 Bot 命令的玩家也有名字。平台只保留本店玩家的名字。配置 `syncNames: false` 可关闭。
174
+ - 需要支持 `names` 帧和身份名字(迁移 `0057`)的 PRiSM。
175
+
169
176
  ### 0.5.3
170
177
 
171
178
  插件改为只转发命令:`login`、`logout`、`wallet` 等命令连同参数原样发给平台(`command` 帧),平台执行、判断权限(只读模式、店员、计费)、写好回复,插件照发(`say` 帧);账单私聊店员、入场附带的门锁密码也由平台决定发到哪。店内动态和闲置提醒同样由平台写好、按 Bot 的监听规则发到规则指定的群或个人,插件不再读取 Bot 设置,也不再收 `shop.event`。插件自己只保留 `versions`、`api测速`、`ws测速` 和 Home Assistant 监听。播报新增机台投币、开局扣币、投币计数清零(需要机台 IO 开启 `cabinetMonitor`),以及来自 Home Assistant 的开机、关机;刷卡改为在游戏取到卡时播报,网页刷卡和实体卡一样只说一次;`下桌` 不必再指定桌号。需要支持命令协议的 PRiSM,店铺所在的 PRiSM 升级之前不要升级到此版本。
package/lib/index.d.ts CHANGED
@@ -28,6 +28,14 @@ export type PrismKoishiPluginConfig = {
28
28
  httpPost?: HttpPost;
29
29
  /** Opens connections to Home Assistant; defaults to Koishi's `ctx.http.ws`. Mainly for tests. */
30
30
  haSocket?: SocketFactory;
31
+ /**
32
+ * Read the members of every group the bots are in and tell the platform what
33
+ * each is called, so a shop's players get their chat names even when they
34
+ * never send the bot a command. On by default; `false` turns it off.
35
+ */
36
+ syncNames?: boolean;
37
+ /** When the first name read runs after starting; a minute by default. Mainly for tests. */
38
+ nameSyncFirstMs?: number;
31
39
  };
32
40
  /** A plain HTTP POST answering with its status, headers and decoded body. */
33
41
  export type HttpPost = (url: string, headers: Record<string, string>, body: unknown) => Promise<{
@@ -62,6 +70,17 @@ export type KoishiLikeContext = {
62
70
  isActive: boolean;
63
71
  sendMessage(channelId: string, content: string): Promise<unknown>;
64
72
  sendPrivateMessage(userId: string, content: string): Promise<unknown>;
73
+ /** Koishi's paged lists: the groups the bot is in, and a group's members. */
74
+ getGuildList?(next?: string): Promise<{
75
+ data: Array<{
76
+ id: string;
77
+ }>;
78
+ next?: string;
79
+ }>;
80
+ getGuildMemberList?(guildId: string, next?: string): Promise<{
81
+ data: GuildMember[];
82
+ next?: string;
83
+ }>;
65
84
  }>;
66
85
  on?(event: "dispose", listener: () => void): void;
67
86
  };
@@ -85,15 +104,26 @@ export type KoishiActionContext = {
85
104
  };
86
105
  };
87
106
  export declare function applyPrismKoishiPlugin(ctx: KoishiLikeContext, config: PrismKoishiPluginConfig): void;
88
- /** A player as the platform names one in what it says. */
107
+ /** A player as the platform names one in what it says; `name` on an identity is what that platform calls them. */
89
108
  type MessagePlayer = {
90
109
  playerId: string;
91
110
  displayName: string | null;
92
111
  identities: Array<{
93
112
  provider: string;
94
113
  subject: string;
114
+ name?: string;
95
115
  }>;
96
116
  };
117
+ /** A group member as a Koishi adapter lists one. */
118
+ type GuildMember = {
119
+ user?: {
120
+ id?: string;
121
+ name?: string;
122
+ nick?: string;
123
+ };
124
+ nick?: string;
125
+ name?: string;
126
+ };
97
127
  /**
98
128
  * What the platform writes for a bot to say (PRiSM's core `bot-message.ts`):
99
129
  * text, and players the bot names as its chat knows them. A segment of a kind
package/lib/index.js CHANGED
@@ -21,6 +21,7 @@ exports.Config = koishi_1.Schema.object({
21
21
  baseUrl: koishi_1.Schema.string().required().description("PRiSM 后端地址,例如 https://link.neri.moe"),
22
22
  shopCode: koishi_1.Schema.string().description("可选:店铺编号。接入凭据已经指明店铺,无需填写;旧配置仍认。"),
23
23
  integrationToken: koishi_1.Schema.string().role("secret").required().description("集成 API Token;其余设置在 PRiSM 商户工作台 → 资源 → 这个 Bot → Bot 设置"),
24
+ syncNames: koishi_1.Schema.boolean().default(true).description("定期读取 Bot 所在群的成员昵称,补全店里玩家的名字(只保留本店玩家)"),
24
25
  });
25
26
  function apply(ctx, config) {
26
27
  applyPrismKoishiPlugin(ctx, config);
@@ -94,6 +95,12 @@ const FIRST_RETRY_MS = 1000;
94
95
  const MAX_RETRY_MS = 30_000;
95
96
  const UNREACHABLE = "操作失败,暂时连不上服务器。";
96
97
  const TIMED_OUT = "服务器没有及时回应,请稍后确认结果";
98
+ /** How often the bots' group members are read again for their names. */
99
+ const NAME_SYNC_MS = 6 * 60 * 60_000;
100
+ /** The first read waits for the connection to settle. */
101
+ const NAME_SYNC_FIRST_MS = 60_000;
102
+ /** Names per report; the platform takes at most 500. */
103
+ const NAMES_PER_REPORT = 200;
97
104
  /**
98
105
  * The one connection to PRiSM. Commands go out as `command` frames and the
99
106
  * platform answers with `say` frames naming them; the shop's news comes as
@@ -408,11 +415,53 @@ class PrismKoishiService {
408
415
  return { status: response.status, headers: response.headers, data: response.data };
409
416
  });
410
417
  this.socket.start();
418
+ this.scheduleNames(options.nameSyncFirstMs ?? NAME_SYNC_FIRST_MS);
411
419
  }
412
420
  dispose() {
421
+ if (this.nameTimer)
422
+ clearTimeout(this.nameTimer);
413
423
  this.watcher?.stop();
414
424
  this.socket.stop();
415
425
  }
426
+ nameTimer = null;
427
+ /** Read names now and then, from the first read on, unless turned off. */
428
+ scheduleNames(delay) {
429
+ if (this.options.syncNames === false)
430
+ return;
431
+ this.nameTimer = setTimeout(() => {
432
+ void this.syncNames()
433
+ .catch((error) => this.logger?.warn("PRiSM name sync failed", error))
434
+ .finally(() => this.scheduleNames(NAME_SYNC_MS));
435
+ }, delay);
436
+ }
437
+ /**
438
+ * Every group each bot is in, member by member: what the platform calls them
439
+ * (their nickname, not a group card, which differs from group to group),
440
+ * sent to the platform in batches. The platform keeps the names of the
441
+ * shop's players among them and ignores everyone else.
442
+ */
443
+ async syncNames() {
444
+ const names = new Map();
445
+ for (const bot of this.ctx.bots ?? []) {
446
+ if (!bot.isActive || !bot.getGuildList || !bot.getGuildMemberList)
447
+ continue;
448
+ const provider = String(bot.platform || "").toLowerCase();
449
+ if (!/^[a-z][a-z0-9_-]{0,63}$/.test(provider))
450
+ continue;
451
+ for await (const guild of pages((next) => bot.getGuildList(next))) {
452
+ for await (const member of pages((next) => bot.getGuildMemberList(guild.id, next))) {
453
+ const subject = String(member.user?.id ?? "").trim();
454
+ const name = String(member.user?.name || member.user?.nick || member.name || member.nick || "").trim();
455
+ if (subject && name)
456
+ names.set(`${provider}:${subject}`, { provider, subject, name });
457
+ }
458
+ }
459
+ }
460
+ const list = [...names.values()];
461
+ for (let start = 0; start < list.length; start += NAMES_PER_REPORT)
462
+ this.socket.send({ type: "names", names: list.slice(start, start + NAMES_PER_REPORT) });
463
+ return list.length;
464
+ }
416
465
  /* ----------------------------- commands -------------------------------- */
417
466
  /**
418
467
  * Hand a command to the platform and answer with the first thing it says
@@ -547,6 +596,10 @@ class PrismKoishiService {
547
596
  async playerName(player, provider) {
548
597
  const identities = Array.isArray(player.identities) ? player.identities : [];
549
598
  const identity = identities.find((candidate) => candidate.provider === provider) ?? identities[0];
599
+ // What this chat's platform calls them, as the platform keeps it for each identity.
600
+ const own = identities.find((candidate) => candidate.provider === provider && candidate.name);
601
+ if (own?.name)
602
+ return own.name;
550
603
  if (identity && this.options.resolveDisplayName) {
551
604
  try {
552
605
  const name = await this.options.resolveDisplayName(identity.subject, identity.provider);
@@ -687,3 +740,20 @@ exports.default = {
687
740
  ConfigSchema: exports.Config,
688
741
  apply,
689
742
  };
743
+ /** Every item of a Koishi paged list, page after page; a page that fails ends the list. */
744
+ async function* pages(read) {
745
+ let next;
746
+ for (let page = 0; page < 1000; page++) {
747
+ let result;
748
+ try {
749
+ result = await read(next);
750
+ }
751
+ catch {
752
+ return;
753
+ }
754
+ yield* result.data ?? [];
755
+ if (!result.next || result.next === next)
756
+ return;
757
+ next = result.next;
758
+ }
759
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-prism",
3
- "version": "0.5.3",
3
+ "version": "0.6.0",
4
4
  "description": "PRiSM Next 计费与设备管理系统的 Koishi 机器人集成插件",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {