@enderhorror/koishi-plugin-steam-friend-status 0.3.4 → 0.3.5

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.
@@ -7,6 +7,7 @@ export interface SteamBroadcastConfig {
7
7
  mergeBroadcast?: boolean;
8
8
  mergeWindow?: number;
9
9
  stabilityPeriod?: number;
10
+ gameBroadcastCooldown?: number;
10
11
  minStopPlayMinutes?: number;
11
12
  immediateBroadcast?: boolean;
12
13
  }
package/lib/index.d.ts CHANGED
@@ -11,6 +11,7 @@ export interface PluginConfig {
11
11
  mergeBroadcast: boolean;
12
12
  mergeWindow: number;
13
13
  stabilityPeriod: number;
14
+ gameBroadcastCooldown: number;
14
15
  minStopPlayMinutes: number;
15
16
  immediateBroadcast: boolean;
16
17
  useApifoxProxy: boolean;
package/lib/index.js CHANGED
@@ -1236,6 +1236,37 @@ async function getGameChangeImg(ctx, playerInfo, changeInfo) {
1236
1236
 
1237
1237
  // package/src/broadcast.ts
1238
1238
  var import_koishi3 = require("koishi");
1239
+ var broadcastCooldowns = /* @__PURE__ */ new WeakMap();
1240
+ function cooldownKey(channelId, userId, id, name2) {
1241
+ return JSON.stringify([channelId, userId, id ? `id:${id}` : `name:${(name2 || "").trim().toLowerCase()}`]);
1242
+ }
1243
+ function eligibleChange(ctx, channelId, change, config) {
1244
+ let cache = broadcastCooldowns.get(ctx);
1245
+ if (!cache) {
1246
+ cache = /* @__PURE__ */ new Map();
1247
+ broadcastCooldowns.set(ctx, cache);
1248
+ }
1249
+ const now = Date.now();
1250
+ const duration = Math.max(0, config.gameBroadcastCooldown ?? 30) * 6e4;
1251
+ for (const [key, expires] of cache) if (expires <= now) cache.delete(key);
1252
+ const oldKey = change.oldGame ? cooldownKey(channelId, change.userId, change.oldGameId, change.oldGame) : "";
1253
+ const newKey = change.newGame ? cooldownKey(channelId, change.userId, change.newGameId, change.newGame) : "";
1254
+ const oldAllowed = !!oldKey && !change.suppressEnd && (!duration || !cache.has(oldKey));
1255
+ const newAllowed = !!newKey && (!duration || !cache.has(newKey));
1256
+ if (change.status === "start") return newAllowed ? { change, keys: [newKey] } : null;
1257
+ if (change.status === "stop") return oldAllowed ? { change, keys: [oldKey] } : null;
1258
+ if (oldAllowed && newAllowed) return { change, keys: [.../* @__PURE__ */ new Set([oldKey, newKey])] };
1259
+ if (newAllowed) return { change: { ...change, status: "start", oldGame: void 0, oldGameId: void 0, playTime: void 0 }, keys: [newKey] };
1260
+ if (oldAllowed) return { change: { ...change, status: "stop", newGame: void 0, newGameId: void 0 }, keys: [oldKey] };
1261
+ return null;
1262
+ }
1263
+ function recordCooldown(ctx, keys, config) {
1264
+ const duration = Math.max(0, config.gameBroadcastCooldown ?? 30) * 6e4;
1265
+ if (!duration) return;
1266
+ const cache = broadcastCooldowns.get(ctx);
1267
+ const expires = Date.now() + duration;
1268
+ for (const key of keys) cache.set(key, expires);
1269
+ }
1239
1270
  async function pollSteamBroadcast(ctx, config, logger, shouldStop) {
1240
1271
  const channels = await ctx.database.get("channel", { usingSteam: true });
1241
1272
  if (channels.length === 0) {
@@ -1310,6 +1341,14 @@ async function pollSteamBroadcast(ctx, config, logger, shouldStop) {
1310
1341
  let allSucceeded = true;
1311
1342
  for (const channel of targetChannels) {
1312
1343
  if (shouldStop()) return;
1344
+ const eligible = eligibleChange(ctx, channel.id, changeInfo, config);
1345
+ if (!eligible) continue;
1346
+ const channelDisplay = {
1347
+ ...displayChange,
1348
+ ...eligible.change,
1349
+ oldGame: eligible.change.oldGame ? displayChange.oldGame : void 0,
1350
+ newGame: eligible.change.newGame ? displayChange.newGame : void 0
1351
+ };
1313
1352
  const directBot = ctx.bots[`${channel.platform}:${channel.assignee}`];
1314
1353
  const bot = directBot || Object.values(ctx.bots).find(
1315
1354
  (item) => item.platform === channel.platform && (item.selfId === channel.assignee || item.user?.id === channel.assignee)
@@ -1328,7 +1367,7 @@ async function pollSteamBroadcast(ctx, config, logger, shouldStop) {
1328
1367
  );
1329
1368
  const textMessage = buildBroadcastText(
1330
1369
  channelNickname || changeInfo.userName,
1331
- displayChange
1370
+ channelDisplay
1332
1371
  );
1333
1372
  try {
1334
1373
  await sendBroadcast(
@@ -1337,10 +1376,11 @@ async function pollSteamBroadcast(ctx, config, logger, shouldStop) {
1337
1376
  channel.id,
1338
1377
  textMessage,
1339
1378
  playerInfo,
1340
- displayChange,
1379
+ channelDisplay,
1341
1380
  config.broadcastWithImage,
1342
1381
  logger
1343
1382
  );
1383
+ recordCooldown(ctx, eligible.keys, config);
1344
1384
  logger.debug(
1345
1385
  `\u5DF2\u5411\u7FA4\u7EC4 ${channel.id} \u64AD\u62A5\u7528\u6237 ${changeInfo.userId}: ${textMessage}`
1346
1386
  );
@@ -1414,6 +1454,10 @@ async function processMergedBroadcast(ctx, config, logger, shouldStop, channels,
1414
1454
  return entry.pendingChannels.has(channel.id) && observed.has(userMap.get(entry.change.userId)?.steamId || "") && candidate?.signature === entry.signature && now - candidate.since >= stableMs;
1415
1455
  });
1416
1456
  if (!entries.length || now - Math.min(...entries.map((e) => e.queuedAt)) < mergeMs) continue;
1457
+ const eligibleEntries = entries.map((entry) => ({ entry, eligible: eligibleChange(ctx, channel.id, entry.change, config) }));
1458
+ for (const item of eligibleEntries) if (!item.eligible) item.entry.pendingChannels.delete(channel.id);
1459
+ const toSend = eligibleEntries.filter((item) => !!item.eligible);
1460
+ if (!toSend.length) continue;
1417
1461
  const bot = ctx.bots[`${channel.platform}:${channel.assignee}`] || Object.values(ctx.bots).find(
1418
1462
  (item) => item.platform === channel.platform && (item.selfId === channel.assignee || item.user?.id === channel.assignee)
1419
1463
  );
@@ -1423,8 +1467,8 @@ async function processMergedBroadcast(ctx, config, logger, shouldStop, channels,
1423
1467
  }
1424
1468
  try {
1425
1469
  const lines = [];
1426
- for (const entry of entries) {
1427
- const change = entry.change;
1470
+ for (const item of toSend) {
1471
+ const change = item.eligible.change;
1428
1472
  const display = {
1429
1473
  ...change,
1430
1474
  oldGame: change.oldGame ? await getLocalizedGameName(ctx, change.oldGameId, change.oldGame) : void 0,
@@ -1436,7 +1480,10 @@ async function processMergedBroadcast(ctx, config, logger, shouldStop, channels,
1436
1480
  if (shouldStop()) return;
1437
1481
  await bot.sendMessage(channel.id, `\u{1F3AE} Steam \u597D\u53CB\u52A8\u6001
1438
1482
  ${lines.join("\n")}`);
1439
- for (const entry of entries) entry.pendingChannels.delete(channel.id);
1483
+ for (const item of toSend) {
1484
+ recordCooldown(ctx, item.eligible.keys, config);
1485
+ item.entry.pendingChannels.delete(channel.id);
1486
+ }
1440
1487
  } catch (error) {
1441
1488
  logger.warn(`\u7FA4\u7EC4 ${channel.id} \u5408\u5E76\u64AD\u62A5\u5931\u8D25\uFF0C\u5C06\u5728\u4E0B\u6B21\u8F6E\u8BE2\u91CD\u8BD5:`, error);
1442
1489
  }
@@ -1675,6 +1722,7 @@ var Config = import_koishi4.Schema.intersect([
1675
1722
  mergeBroadcast: import_koishi4.Schema.boolean().default(true).description("\u5408\u5E76\u7A97\u53E3\u5185\u7684\u6E38\u620F\u72B6\u6001\u64AD\u62A5"),
1676
1723
  mergeWindow: import_koishi4.Schema.number().default(180).min(1).step(1).description("\u5408\u5E76\u64AD\u62A5\u7A97\u53E3\uFF0C\u5355\u4F4D\uFF1A\u79D2"),
1677
1724
  stabilityPeriod: import_koishi4.Schema.number().default(30).min(0).step(1).description("\u72B6\u6001\u7A33\u5B9A\u671F\uFF0C\u5355\u4F4D\uFF1A\u79D2\uFF1B\u9700\u901A\u8FC7\u540E\u7EED\u8F6E\u8BE2\u786E\u8BA4"),
1725
+ gameBroadcastCooldown: import_koishi4.Schema.number().default(30).min(0).step(1).description("\u540C\u4E00\u7FA4\u4E2D\u540C\u4E00\u7528\u6237\u540C\u4E00\u6E38\u620F\u7684\u64AD\u62A5\u51B7\u5374\u65F6\u95F4\uFF08\u5206\u949F\uFF09\uFF0C0 \u4E3A\u5173\u95ED\uFF1B\u5F00\u59CB\u548C\u7ED3\u675F\u5171\u7528\u51B7\u5374"),
1678
1726
  minStopPlayMinutes: import_koishi4.Schema.number().default(10).min(0).step(1).description("\u7ED3\u675F\u64AD\u62A5\u6700\u77ED\u6E38\u620F\u65F6\u957F\uFF08\u5206\u949F\uFF09\uFF0C\u8BBE\u4E3A 0 \u5173\u95ED\u8FC7\u6EE4\uFF1B\u65F6\u957F\u672A\u77E5\u65F6\u4ECD\u64AD\u62A5"),
1679
1727
  immediateBroadcast: import_koishi4.Schema.boolean().default(false).description("\u5373\u65F6\u64AD\u62A5\uFF0C\u8DF3\u8FC7\u5408\u5E76\u7A97\u53E3\u548C\u72B6\u6001\u7A33\u5B9A\u671F")
1680
1728
  }).description("\u64AD\u62A5\u8BBE\u7F6E"),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@enderhorror/koishi-plugin-steam-friend-status",
3
3
  "description": "查询并播报 Steam 好友状态,支持游戏活动播报名称本地化",
4
- "version": "0.3.4",
4
+ "version": "0.3.5",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/readme.md CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  ## 更新日志
6
6
 
7
+ ### 0.3.5
8
+
9
+ - 增加同一群中同一用户同一游戏的播报冷却,默认 30 分钟,开始和结束事件共用;可配置为 0 关闭。
10
+ - 冷却期间仍更新游戏状态;切换游戏分别检查新旧游戏,发送失败不会启动冷却。
11
+
7
12
  ### 0.3.4
8
13
 
9
14
  - 默认开启群组游戏动态合并播报,合并窗口默认为 180 秒;不同群组独立发送,失败群组在后续轮询重试。
@@ -62,11 +67,14 @@ npm install @enderhorror/koishi-plugin-steam-friend-status
62
67
  | mergeBroadcast | true | 开启合并播报 |
63
68
  | mergeWindow | 180 | 合并播报窗口,单位:秒,最小 1 秒 |
64
69
  | stabilityPeriod | 30 | 状态稳定期,单位:秒,最小 0 秒 |
70
+ | gameBroadcastCooldown | 30 | 同一群同一用户同一游戏的开始与结束共用冷却(分钟);0 关闭,冷却在内存中,重启清空 |
65
71
  | minStopPlayMinutes | 10 | 少于该时长(分钟)不播报结束;0 表示关闭过滤,时长未知仍播报 |
66
72
  | immediateBroadcast | false | 开启后即时播报,跳过合并窗口和状态稳定期 |
67
73
 
68
74
  状态变化通过轮询发现和确认;稳定期不是精确定时,实际确认可能延迟至下一次轮询。默认查询间隔为 60 秒,若希望更及时地确认默认 30 秒稳定期,可将 `interval` 调整至 30 秒或更短(最小 10 秒);更频繁的轮询会增加 Steam API 请求量。已有 `interval` 配置不会自动改变。
69
75
 
76
+ 同一游戏的开始和结束播报共用冷却:成功发送任意一种后,冷却内不再播报该用户该游戏,但仍更新状态。切换游戏时分别检查新旧游戏;冷却按群隔离,发送失败不计入冷却,插件重启后清空。
77
+
70
78
  游戏切换时,如果上一个游戏未满阈值,只播报新游戏开始;结束状态仍会更新。时长以插件首次检测到开始和结束的时间估算,而非 Steam 官方精确游戏时长;轮询间隔可能影响估算,旧会话可能受升级前播报延迟影响。
71
79
 
72
80
  > 合并播报以文字汇总发送,不附单人图片。候选状态和待发送队列保存在内存中,插件重启会清空队列;未成功确认的状态将在后续轮询重新检测。发送失败的群组会在下一次轮询时重试;不同群组独立发送。
package/src/broadcast.ts CHANGED
@@ -21,10 +21,41 @@ export interface SteamBroadcastConfig {
21
21
  mergeWindow?: number;
22
22
  stabilityPeriod?: number;
23
23
  minStopPlayMinutes?: number;
24
+ gameBroadcastCooldown?: number;
24
25
  }
25
26
 
26
27
  type SteamPlayer = SteamUserInfo["response"]["players"][number];
27
28
 
29
+ // 冷却仅针对已成功发送的动态,群组和用户之间相互隔离。
30
+ const broadcastCooldowns = new WeakMap<Context, Map<string, number>>();
31
+ function cooldownKey(channelId: string, userId: string, id: string | undefined, name: string | undefined): string {
32
+ return JSON.stringify([channelId, userId, id ? `id:${id}` : `name:${(name || "").trim().toLowerCase()}`]);
33
+ }
34
+ function eligibleChange(ctx: Context, channelId: string, change: GameChangeInfo, config: SteamBroadcastConfig): { change: GameChangeInfo; keys: string[] } | null {
35
+ let cache = broadcastCooldowns.get(ctx);
36
+ if (!cache) { cache = new Map(); broadcastCooldowns.set(ctx, cache); }
37
+ const now = Date.now();
38
+ const duration = Math.max(0, config.gameBroadcastCooldown ?? 30) * 60_000;
39
+ for (const [key, expires] of cache) if (expires <= now) cache.delete(key);
40
+ const oldKey = change.oldGame ? cooldownKey(channelId, change.userId, change.oldGameId, change.oldGame) : "";
41
+ const newKey = change.newGame ? cooldownKey(channelId, change.userId, change.newGameId, change.newGame) : "";
42
+ const oldAllowed = !!oldKey && !change.suppressEnd && (!duration || !cache.has(oldKey));
43
+ const newAllowed = !!newKey && (!duration || !cache.has(newKey));
44
+ if (change.status === "start") return newAllowed ? { change, keys: [newKey] } : null;
45
+ if (change.status === "stop") return oldAllowed ? { change, keys: [oldKey] } : null;
46
+ if (oldAllowed && newAllowed) return { change, keys: [...new Set([oldKey, newKey])] };
47
+ if (newAllowed) return { change: { ...change, status: "start", oldGame: undefined, oldGameId: undefined, playTime: undefined }, keys: [newKey] };
48
+ if (oldAllowed) return { change: { ...change, status: "stop", newGame: undefined, newGameId: undefined }, keys: [oldKey] };
49
+ return null;
50
+ }
51
+ function recordCooldown(ctx: Context, keys: string[], config: SteamBroadcastConfig): void {
52
+ const duration = Math.max(0, config.gameBroadcastCooldown ?? 30) * 60_000;
53
+ if (!duration) return;
54
+ const cache = broadcastCooldowns.get(ctx)!;
55
+ const expires = Date.now() + duration;
56
+ for (const key of keys) cache.set(key, expires);
57
+ }
58
+
28
59
  // 轮询 Steam 状态,并按用户绑定过的群组进行播报
29
60
  export async function pollSteamBroadcast(
30
61
  ctx: Context,
@@ -119,6 +150,12 @@ export async function pollSteamBroadcast(
119
150
  for (const channel of targetChannels) {
120
151
  if (shouldStop()) return;
121
152
 
153
+ const eligible = eligibleChange(ctx, channel.id, changeInfo, config);
154
+ if (!eligible) continue;
155
+ const channelDisplay: GameChangeInfo = { ...displayChange, ...eligible.change,
156
+ oldGame: eligible.change.oldGame ? displayChange.oldGame : undefined,
157
+ newGame: eligible.change.newGame ? displayChange.newGame : undefined,
158
+ };
122
159
  const directBot = ctx.bots[`${channel.platform}:${channel.assignee}`];
123
160
  const bot =
124
161
  directBot ||
@@ -144,7 +181,7 @@ export async function pollSteamBroadcast(
144
181
  );
145
182
  const textMessage = buildBroadcastText(
146
183
  channelNickname || changeInfo.userName,
147
- displayChange,
184
+ channelDisplay,
148
185
  );
149
186
 
150
187
  try {
@@ -154,10 +191,11 @@ export async function pollSteamBroadcast(
154
191
  channel.id,
155
192
  textMessage,
156
193
  playerInfo,
157
- displayChange,
194
+ channelDisplay,
158
195
  config.broadcastWithImage,
159
196
  logger,
160
197
  );
198
+ recordCooldown(ctx, eligible.keys, config);
161
199
  logger.debug(
162
200
  `已向群组 ${channel.id} 播报用户 ${changeInfo.userId}: ${textMessage}`,
163
201
  );
@@ -242,14 +280,18 @@ async function processMergedBroadcast(
242
280
  && now - candidate.since >= stableMs;
243
281
  });
244
282
  if (!entries.length || now - Math.min(...entries.map((e) => e.queuedAt)) < mergeMs) continue;
283
+ const eligibleEntries = entries.map((entry) => ({ entry, eligible: eligibleChange(ctx, channel.id, entry.change, config) }));
284
+ for (const item of eligibleEntries) if (!item.eligible) item.entry.pendingChannels.delete(channel.id);
285
+ const toSend = eligibleEntries.filter((item) => !!item.eligible);
286
+ if (!toSend.length) continue;
245
287
  const bot = ctx.bots[`${channel.platform}:${channel.assignee}`] || Object.values(ctx.bots).find(
246
288
  (item) => item.platform === channel.platform && (item.selfId === channel.assignee || item.user?.id === channel.assignee),
247
289
  );
248
290
  if (!bot) { logger.warn(`未找到群组 ${channel.id} 对应的机器人,保留待发送播报`); continue; }
249
291
  try {
250
292
  const lines: string[] = [];
251
- for (const entry of entries) {
252
- const change = entry.change;
293
+ for (const item of toSend) {
294
+ const change = item.eligible!.change;
253
295
  const display: GameChangeInfo = {
254
296
  ...change,
255
297
  oldGame: change.oldGame ? await getLocalizedGameName(ctx, change.oldGameId, change.oldGame) : undefined,
@@ -260,7 +302,10 @@ async function processMergedBroadcast(
260
302
  }
261
303
  if (shouldStop()) return;
262
304
  await bot.sendMessage(channel.id, `🎮 Steam 好友动态\n${lines.join("\n")}`);
263
- for (const entry of entries) entry.pendingChannels.delete(channel.id);
305
+ for (const item of toSend) {
306
+ recordCooldown(ctx, item.eligible!.keys, config);
307
+ item.entry.pendingChannels.delete(channel.id);
308
+ }
264
309
  } catch (error) { logger.warn(`群组 ${channel.id} 合并播报失败,将在下次轮询重试:`, error); }
265
310
  }
266
311
  for (const [id, entry] of state.queue) {
package/src/index.ts CHANGED
@@ -93,6 +93,11 @@ export const Config = Schema.intersect([
93
93
  .min(0)
94
94
  .step(1)
95
95
  .description("状态稳定期,单位:秒;需通过后续轮询确认"),
96
+ gameBroadcastCooldown: Schema.number()
97
+ .default(30)
98
+ .min(0)
99
+ .step(1)
100
+ .description("同一群中同一用户同一游戏的播报冷却时间(分钟),0 为关闭;开始和结束共用冷却"),
96
101
  minStopPlayMinutes: Schema.number()
97
102
  .default(10)
98
103
  .min(0)
@@ -183,6 +188,7 @@ export interface PluginConfig {
183
188
  stabilityPeriod: number;
184
189
  immediateBroadcast: boolean;
185
190
  minStopPlayMinutes: number;
191
+ gameBroadcastCooldown: number;
186
192
  useApifoxProxy: boolean;
187
193
  maxRetries: number;
188
194
  showcardmode: "1" | "2";