@enderhorror/koishi-plugin-steam-friend-status 0.3.3 → 0.3.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.
- package/lib/broadcast.d.ts +5 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.js +137 -14
- package/lib/types.d.ts +3 -0
- package/lib/userdata.d.ts +1 -1
- package/package.json +1 -1
- package/readme.md +22 -1
- package/src/broadcast.ts +137 -14
- package/src/index.ts +30 -1
- package/src/types.ts +3 -0
- package/src/userdata.ts +2 -2
package/lib/broadcast.d.ts
CHANGED
|
@@ -4,6 +4,11 @@ export interface SteamBroadcastConfig {
|
|
|
4
4
|
SteamApiKey: string;
|
|
5
5
|
useSteamName: boolean;
|
|
6
6
|
broadcastWithImage: boolean;
|
|
7
|
+
mergeBroadcast?: boolean;
|
|
8
|
+
mergeWindow?: number;
|
|
9
|
+
stabilityPeriod?: number;
|
|
10
|
+
minStopPlayMinutes?: number;
|
|
11
|
+
immediateBroadcast?: boolean;
|
|
7
12
|
}
|
|
8
13
|
export declare function pollSteamBroadcast(ctx: Context, config: SteamBroadcastConfig, logger: PluginLogger, shouldStop: () => boolean): Promise<void>;
|
|
9
14
|
export declare function updatePlayerHeadshots(ctx: Context, config: SteamBroadcastConfig, logger: PluginLogger): Promise<void>;
|
package/lib/index.d.ts
CHANGED
|
@@ -8,6 +8,11 @@ export interface PluginConfig {
|
|
|
8
8
|
interval: number;
|
|
9
9
|
useSteamName: boolean;
|
|
10
10
|
broadcastWithImage: boolean;
|
|
11
|
+
mergeBroadcast: boolean;
|
|
12
|
+
mergeWindow: number;
|
|
13
|
+
stabilityPeriod: number;
|
|
14
|
+
minStopPlayMinutes: number;
|
|
15
|
+
immediateBroadcast: boolean;
|
|
11
16
|
useApifoxProxy: boolean;
|
|
12
17
|
maxRetries: number;
|
|
13
18
|
showcardmode: "1" | "2";
|
package/lib/index.js
CHANGED
|
@@ -484,11 +484,11 @@ async function saveGameSessionsLocked(ctx, data) {
|
|
|
484
484
|
releaseLock();
|
|
485
485
|
}
|
|
486
486
|
}
|
|
487
|
-
async function startGameSession(ctx, userId, gameName) {
|
|
487
|
+
async function startGameSession(ctx, userId, gameName, startTime = Date.now()) {
|
|
488
488
|
const sessions = await loadGameSessionsLocked(ctx);
|
|
489
489
|
sessions[userId] = {
|
|
490
490
|
gameName,
|
|
491
|
-
startTime
|
|
491
|
+
startTime
|
|
492
492
|
};
|
|
493
493
|
await saveGameSessionsLocked(ctx, sessions);
|
|
494
494
|
}
|
|
@@ -1270,6 +1270,10 @@ async function pollSteamBroadcast(ctx, config, logger, shouldStop) {
|
|
|
1270
1270
|
logger,
|
|
1271
1271
|
shouldStop
|
|
1272
1272
|
);
|
|
1273
|
+
if (config.mergeBroadcast !== false && !config.immediateBroadcast) {
|
|
1274
|
+
await processMergedBroadcast(ctx, config, logger, shouldStop, channels, allUserData, steamUserInfo, changes);
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1273
1277
|
if (Object.keys(changes).length === 0) {
|
|
1274
1278
|
logger.debug("\u672C\u8F6E\u6CA1\u6709\u68C0\u6D4B\u5230\u6E38\u620F\u72B6\u6001\u53D8\u5316");
|
|
1275
1279
|
return;
|
|
@@ -1287,6 +1291,13 @@ async function pollSteamBroadcast(ctx, config, logger, shouldStop) {
|
|
|
1287
1291
|
);
|
|
1288
1292
|
continue;
|
|
1289
1293
|
}
|
|
1294
|
+
if (isShortSession(changeInfo, config)) {
|
|
1295
|
+
if (changeInfo.status === "stop") {
|
|
1296
|
+
await acknowledgeGameChange(ctx, changeInfo, logger);
|
|
1297
|
+
continue;
|
|
1298
|
+
}
|
|
1299
|
+
changeInfo.suppressEnd = true;
|
|
1300
|
+
}
|
|
1290
1301
|
const playerInfo = steamUserInfo.response.players.find(
|
|
1291
1302
|
(player) => player.steamid === user.steamId
|
|
1292
1303
|
);
|
|
@@ -1343,6 +1354,106 @@ async function pollSteamBroadcast(ctx, config, logger, shouldStop) {
|
|
|
1343
1354
|
}
|
|
1344
1355
|
}
|
|
1345
1356
|
}
|
|
1357
|
+
var mergeStates = /* @__PURE__ */ new WeakMap();
|
|
1358
|
+
function getMergeState(ctx) {
|
|
1359
|
+
let state = mergeStates.get(ctx);
|
|
1360
|
+
if (!state) {
|
|
1361
|
+
state = { candidates: /* @__PURE__ */ new Map(), queue: /* @__PURE__ */ new Map() };
|
|
1362
|
+
mergeStates.set(ctx, state);
|
|
1363
|
+
}
|
|
1364
|
+
return state;
|
|
1365
|
+
}
|
|
1366
|
+
function changeSignature(change) {
|
|
1367
|
+
return JSON.stringify([change.oldGame || "", change.newGame || "", change.newGameId || ""]);
|
|
1368
|
+
}
|
|
1369
|
+
async function processMergedBroadcast(ctx, config, logger, shouldStop, channels, users, steamUserInfo, changes) {
|
|
1370
|
+
const state = getMergeState(ctx);
|
|
1371
|
+
const now = Date.now();
|
|
1372
|
+
const stableMs = Math.max(0, config.stabilityPeriod ?? 30) * 1e3;
|
|
1373
|
+
const mergeMs = Math.max(0, config.mergeWindow ?? 180) * 1e3;
|
|
1374
|
+
const observed = new Set(steamUserInfo.response.players.map((p) => p.steamid));
|
|
1375
|
+
const userMap = new Map(users.map((u) => [u.userId, u]));
|
|
1376
|
+
for (const id of /* @__PURE__ */ new Set([...state.candidates.keys(), ...state.queue.keys()])) {
|
|
1377
|
+
const user = userMap.get(id);
|
|
1378
|
+
if (!user || !observed.has(user.steamId) || !changes[id]) {
|
|
1379
|
+
state.candidates.delete(id);
|
|
1380
|
+
if (!user || observed.has(user.steamId) && !changes[id]) state.queue.delete(id);
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
for (const change of Object.values(changes)) {
|
|
1384
|
+
const signature = changeSignature(change);
|
|
1385
|
+
const previous = state.candidates.get(change.userId);
|
|
1386
|
+
if (!previous || previous.signature !== signature) {
|
|
1387
|
+
state.candidates.set(change.userId, { signature, since: now, firstChange: { ...change } });
|
|
1388
|
+
state.queue.delete(change.userId);
|
|
1389
|
+
if (stableMs > 0) continue;
|
|
1390
|
+
} else if (now - previous.since < stableMs) continue;
|
|
1391
|
+
const firstChange = state.candidates.get(change.userId).firstChange;
|
|
1392
|
+
if (isShortSession(firstChange, config)) {
|
|
1393
|
+
if (firstChange.status === "stop") {
|
|
1394
|
+
if (await acknowledgeGameChange(ctx, firstChange, logger)) {
|
|
1395
|
+
state.queue.delete(change.userId);
|
|
1396
|
+
state.candidates.delete(change.userId);
|
|
1397
|
+
}
|
|
1398
|
+
continue;
|
|
1399
|
+
}
|
|
1400
|
+
change.suppressEnd = true;
|
|
1401
|
+
}
|
|
1402
|
+
const queued = state.queue.get(change.userId);
|
|
1403
|
+
if (queued?.signature === signature) continue;
|
|
1404
|
+
const user = userMap.get(change.userId);
|
|
1405
|
+
if (!user) continue;
|
|
1406
|
+
change.observedAt = state.candidates.get(change.userId).since;
|
|
1407
|
+
const pendingChannels = new Set(channels.filter((c) => user.effectGroups.includes(c.id)).map((c) => c.id));
|
|
1408
|
+
if (pendingChannels.size) state.queue.set(change.userId, { signature, change, queuedAt: now, pendingChannels });
|
|
1409
|
+
}
|
|
1410
|
+
for (const channel of channels) {
|
|
1411
|
+
if (shouldStop()) return;
|
|
1412
|
+
const entries = [...state.queue.values()].filter((entry) => {
|
|
1413
|
+
const candidate = state.candidates.get(entry.change.userId);
|
|
1414
|
+
return entry.pendingChannels.has(channel.id) && observed.has(userMap.get(entry.change.userId)?.steamId || "") && candidate?.signature === entry.signature && now - candidate.since >= stableMs;
|
|
1415
|
+
});
|
|
1416
|
+
if (!entries.length || now - Math.min(...entries.map((e) => e.queuedAt)) < mergeMs) continue;
|
|
1417
|
+
const bot = ctx.bots[`${channel.platform}:${channel.assignee}`] || Object.values(ctx.bots).find(
|
|
1418
|
+
(item) => item.platform === channel.platform && (item.selfId === channel.assignee || item.user?.id === channel.assignee)
|
|
1419
|
+
);
|
|
1420
|
+
if (!bot) {
|
|
1421
|
+
logger.warn(`\u672A\u627E\u5230\u7FA4\u7EC4 ${channel.id} \u5BF9\u5E94\u7684\u673A\u5668\u4EBA\uFF0C\u4FDD\u7559\u5F85\u53D1\u9001\u64AD\u62A5`);
|
|
1422
|
+
continue;
|
|
1423
|
+
}
|
|
1424
|
+
try {
|
|
1425
|
+
const lines = [];
|
|
1426
|
+
for (const entry of entries) {
|
|
1427
|
+
const change = entry.change;
|
|
1428
|
+
const display = {
|
|
1429
|
+
...change,
|
|
1430
|
+
oldGame: change.oldGame ? await getLocalizedGameName(ctx, change.oldGameId, change.oldGame) : void 0,
|
|
1431
|
+
newGame: change.newGame ? await getLocalizedGameName(ctx, change.newGameId, change.newGame) : void 0
|
|
1432
|
+
};
|
|
1433
|
+
const nickname = await getUserNickname(ctx, change.userId, channel.id);
|
|
1434
|
+
lines.push(`\u2022 ${buildBroadcastText(nickname || change.userName, display)}`);
|
|
1435
|
+
}
|
|
1436
|
+
if (shouldStop()) return;
|
|
1437
|
+
await bot.sendMessage(channel.id, `\u{1F3AE} Steam \u597D\u53CB\u52A8\u6001
|
|
1438
|
+
${lines.join("\n")}`);
|
|
1439
|
+
for (const entry of entries) entry.pendingChannels.delete(channel.id);
|
|
1440
|
+
} catch (error) {
|
|
1441
|
+
logger.warn(`\u7FA4\u7EC4 ${channel.id} \u5408\u5E76\u64AD\u62A5\u5931\u8D25\uFF0C\u5C06\u5728\u4E0B\u6B21\u8F6E\u8BE2\u91CD\u8BD5:`, error);
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
for (const [id, entry] of state.queue) {
|
|
1445
|
+
const user = userMap.get(id);
|
|
1446
|
+
for (const channelId of entry.pendingChannels) {
|
|
1447
|
+
if (!channels.some((c) => c.id === channelId) || !user?.effectGroups.includes(channelId)) entry.pendingChannels.delete(channelId);
|
|
1448
|
+
}
|
|
1449
|
+
if (!entry.pendingChannels.size) {
|
|
1450
|
+
if (await acknowledgeGameChange(ctx, entry.change, logger)) {
|
|
1451
|
+
state.queue.delete(id);
|
|
1452
|
+
state.candidates.delete(id);
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1346
1457
|
async function updatePlayerHeadshots(ctx, config, logger) {
|
|
1347
1458
|
const allUserData = await ctx.database.select("SteamUser").execute();
|
|
1348
1459
|
const steamUserInfo = await getSteamUserInfoByDatabase(
|
|
@@ -1390,7 +1501,10 @@ async function detectGameChanges(ctx, allUserData, steamUserInfo, useSteamName,
|
|
|
1390
1501
|
}
|
|
1391
1502
|
continue;
|
|
1392
1503
|
}
|
|
1393
|
-
const
|
|
1504
|
+
const session = await getGameSession(ctx, userData.userId);
|
|
1505
|
+
const playDurationMs = session && Number.isFinite(session.startTime) ? Math.max(0, Date.now() - session.startTime) : void 0;
|
|
1506
|
+
const minutes = playDurationMs === void 0 ? 0 : Math.floor(playDurationMs / 6e4);
|
|
1507
|
+
const playTime = minutes > 0 ? formatPlayTime(minutes) : void 0;
|
|
1394
1508
|
const userName = useSteamName ? player.personaname : userData.userName;
|
|
1395
1509
|
if (newGame && !oldGame) {
|
|
1396
1510
|
changes[userData.userId] = {
|
|
@@ -1409,7 +1523,8 @@ async function detectGameChanges(ctx, allUserData, steamUserInfo, useSteamName,
|
|
|
1409
1523
|
oldGameId,
|
|
1410
1524
|
newGame,
|
|
1411
1525
|
newGameId,
|
|
1412
|
-
playTime
|
|
1526
|
+
playTime,
|
|
1527
|
+
playDurationMs
|
|
1413
1528
|
};
|
|
1414
1529
|
} else if (!newGame && oldGame) {
|
|
1415
1530
|
changes[userData.userId] = {
|
|
@@ -1418,20 +1533,19 @@ async function detectGameChanges(ctx, allUserData, steamUserInfo, useSteamName,
|
|
|
1418
1533
|
status: "stop",
|
|
1419
1534
|
oldGame,
|
|
1420
1535
|
oldGameId,
|
|
1421
|
-
playTime
|
|
1536
|
+
playTime,
|
|
1537
|
+
playDurationMs
|
|
1422
1538
|
};
|
|
1423
1539
|
}
|
|
1424
1540
|
}
|
|
1425
1541
|
return changes;
|
|
1426
1542
|
}
|
|
1427
|
-
|
|
1428
|
-
const
|
|
1429
|
-
|
|
1430
|
-
const minutes = Math.floor((Date.now() - session.startTime) / 6e4);
|
|
1431
|
-
return minutes > 0 ? formatPlayTime(minutes) : void 0;
|
|
1543
|
+
function isShortSession(change, config) {
|
|
1544
|
+
const threshold = Math.max(0, config.minStopPlayMinutes ?? 10) * 6e4;
|
|
1545
|
+
return (change.status === "stop" || change.status === "change") && change.playDurationMs !== void 0 && change.playDurationMs < threshold;
|
|
1432
1546
|
}
|
|
1433
1547
|
function buildBroadcastText(displayName, changeInfo) {
|
|
1434
|
-
if (changeInfo.status === "start") {
|
|
1548
|
+
if (changeInfo.status === "start" || changeInfo.status === "change" && changeInfo.suppressEnd) {
|
|
1435
1549
|
return `${displayName} \u5F00\u59CB\u73A9 ${changeInfo.newGame} \u4E86`;
|
|
1436
1550
|
}
|
|
1437
1551
|
if (changeInfo.status === "stop") {
|
|
@@ -1454,10 +1568,10 @@ async function sendBroadcast(ctx, bot, channelId, textMessage, playerInfo, chang
|
|
|
1454
1568
|
async function acknowledgeGameChange(ctx, changeInfo, logger) {
|
|
1455
1569
|
try {
|
|
1456
1570
|
if (changeInfo.status === "start") {
|
|
1457
|
-
await startGameSession(ctx, changeInfo.userId, changeInfo.newGame || "");
|
|
1571
|
+
await startGameSession(ctx, changeInfo.userId, changeInfo.newGame || "", changeInfo.observedAt);
|
|
1458
1572
|
} else if (changeInfo.status === "change") {
|
|
1459
1573
|
await endGameSession(ctx, changeInfo.userId);
|
|
1460
|
-
await startGameSession(ctx, changeInfo.userId, changeInfo.newGame || "");
|
|
1574
|
+
await startGameSession(ctx, changeInfo.userId, changeInfo.newGame || "", changeInfo.observedAt);
|
|
1461
1575
|
} else {
|
|
1462
1576
|
await endGameSession(ctx, changeInfo.userId);
|
|
1463
1577
|
}
|
|
@@ -1466,11 +1580,13 @@ async function acknowledgeGameChange(ctx, changeInfo, logger) {
|
|
|
1466
1580
|
{ userId: changeInfo.userId },
|
|
1467
1581
|
{ lastPlayedGame: changeInfo.newGame || "", lastPlayedGameId: changeInfo.newGameId || "" }
|
|
1468
1582
|
);
|
|
1583
|
+
return true;
|
|
1469
1584
|
} catch (error) {
|
|
1470
1585
|
logger.error(
|
|
1471
1586
|
`\u64AD\u62A5\u5DF2\u53D1\u9001\uFF0C\u4F46\u66F4\u65B0\u7528\u6237 ${changeInfo.userId} \u7684\u6E38\u620F\u72B6\u6001\u5931\u8D25:`,
|
|
1472
1587
|
error
|
|
1473
1588
|
);
|
|
1589
|
+
return false;
|
|
1474
1590
|
}
|
|
1475
1591
|
}
|
|
1476
1592
|
async function downloadPlayerAvatar(ctx, player) {
|
|
@@ -1551,10 +1667,17 @@ var Config = import_koishi4.Schema.intersect([
|
|
|
1551
1667
|
SteamApiKey: import_koishi4.Schema.string().description(
|
|
1552
1668
|
"Steam API Key\uFF0C\u83B7\u53D6\u65B9\u5F0F\uFF1Ahttps://partner.steamgames.com/doc/webapi_overview/auth"
|
|
1553
1669
|
).role("secret").required(),
|
|
1554
|
-
interval: import_koishi4.Schema.number().default(60).min(10).description("\u67E5\u8BE2\u95F4\u9694
|
|
1670
|
+
interval: import_koishi4.Schema.number().default(60).min(10).description("\u67E5\u8BE2\u95F4\u9694\uFF0C\u5355\u4F4D\uFF1A\u79D2\uFF1B\u72B6\u6001\u7A33\u5B9A\u671F\u9700\u7ECF\u540E\u7EED\u8F6E\u8BE2\u786E\u8BA4\uFF0C\u5EFA\u8BAE\u4E0D\u5927\u4E8E\u7A33\u5B9A\u671F"),
|
|
1555
1671
|
useSteamName: import_koishi4.Schema.boolean().default(true).description("\u4F7F\u7528Steam\u6635\u79F0,\u5173\u95ED\u65F6\u4F7F\u7528\u7684QQ\u6635\u79F0"),
|
|
1556
1672
|
broadcastWithImage: import_koishi4.Schema.boolean().default(true).description("\u64AD\u62A5\u65F6\u9644\u5E26\u56FE\u7247")
|
|
1557
1673
|
}).description("\u57FA\u7840\u8BBE\u7F6E"),
|
|
1674
|
+
import_koishi4.Schema.object({
|
|
1675
|
+
mergeBroadcast: import_koishi4.Schema.boolean().default(true).description("\u5408\u5E76\u7A97\u53E3\u5185\u7684\u6E38\u620F\u72B6\u6001\u64AD\u62A5"),
|
|
1676
|
+
mergeWindow: import_koishi4.Schema.number().default(180).min(1).step(1).description("\u5408\u5E76\u64AD\u62A5\u7A97\u53E3\uFF0C\u5355\u4F4D\uFF1A\u79D2"),
|
|
1677
|
+
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"),
|
|
1678
|
+
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
|
+
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
|
+
}).description("\u64AD\u62A5\u8BBE\u7F6E"),
|
|
1558
1681
|
import_koishi4.Schema.object({
|
|
1559
1682
|
useApifoxProxy: import_koishi4.Schema.boolean().default(true).description(
|
|
1560
1683
|
"Steam API \u76F4\u8FDE\u5931\u8D25\u65F6\u4F7F\u7528 APIFOX \u516C\u5171\u4EE3\u7406\u515C\u5E95\uFF08\u8BF7\u6C42 URL \u4F1A\u53D1\u9001\u7ED9\u4EE3\u7406\uFF09"
|
package/lib/types.d.ts
CHANGED
package/lib/userdata.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ interface GameSession {
|
|
|
6
6
|
/**
|
|
7
7
|
* 记录用户开始玩游戏
|
|
8
8
|
*/
|
|
9
|
-
export declare function startGameSession(ctx: Context, userId: string, gameName: string): Promise<void>;
|
|
9
|
+
export declare function startGameSession(ctx: Context, userId: string, gameName: string, startTime?: number): Promise<void>;
|
|
10
10
|
/**
|
|
11
11
|
* 结束游戏会话并返回游玩时长(分钟)
|
|
12
12
|
*/
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
自动查询并播报 Steam 好友游戏状态的 Koishi 插件。本版本基于 [koishi-plugin-steam-friend-status-fork@0.3.1](https://www.npmjs.com/package/koishi-plugin-steam-friend-status-fork/v/0.3.1) 修改。
|
|
4
4
|
|
|
5
|
+
## 更新日志
|
|
6
|
+
|
|
7
|
+
### 0.3.4
|
|
8
|
+
|
|
9
|
+
- 默认开启群组游戏动态合并播报,合并窗口默认为 180 秒;不同群组独立发送,失败群组在后续轮询重试。
|
|
10
|
+
- 新增状态稳定期,默认 30 秒;需后续轮询确认,降低短时间状态波动产生的播报。
|
|
11
|
+
- 新增结束播报时长阈值,默认少于 10 分钟不播报结束;切换游戏时若前一游戏未达到阈值,仅播报新游戏开始。时长未知时仍播报,阈值设为 0 可关闭过滤。
|
|
12
|
+
- 新增即时播报选项及相关配置;合并播报以文字汇总,不附单人图片。
|
|
13
|
+
- 新安装默认轮询间隔为 60 秒;已有配置保持不变。实际检测与播报时间受轮询间隔影响,待发送队列存于内存。
|
|
14
|
+
|
|
5
15
|
## 本版本的主要改动
|
|
6
16
|
|
|
7
17
|
- **长游戏名称布局**:加宽好友列表和活动播报卡片,长游戏名自动换行,减少截断;个人资料游戏名支持长单词换行。
|
|
@@ -46,9 +56,20 @@ npm install @enderhorror/koishi-plugin-steam-friend-status
|
|
|
46
56
|
| 配置项 | 默认值 | 说明 |
|
|
47
57
|
| ------------------ | ------ | ---------------------------------------------------------------------------------------------------------------------------------- |
|
|
48
58
|
| SteamApiKey | 无 | 用户查询信息的apikey,可以从 -> [这里](https://partner.steamgames.com/doc/webapi_overview/auth) 获取 |
|
|
49
|
-
| interval |
|
|
59
|
+
| interval | 60 | 单位(秒),自动查询信息的时间间隔,最小 10 秒 |
|
|
50
60
|
| useSteamName | true | 播报时使用玩家昵称,false为QQ昵称,true为steam昵称 <span style="color:red">red/satori适配器暂时无法获取到用户昵称,建议打开</span> |
|
|
51
61
|
| broadcastWithImage | true | 播报时附带好友列表图片,关闭可以减少因长图片导致的刷屏 |
|
|
62
|
+
| mergeBroadcast | true | 开启合并播报 |
|
|
63
|
+
| mergeWindow | 180 | 合并播报窗口,单位:秒,最小 1 秒 |
|
|
64
|
+
| stabilityPeriod | 30 | 状态稳定期,单位:秒,最小 0 秒 |
|
|
65
|
+
| minStopPlayMinutes | 10 | 少于该时长(分钟)不播报结束;0 表示关闭过滤,时长未知仍播报 |
|
|
66
|
+
| immediateBroadcast | false | 开启后即时播报,跳过合并窗口和状态稳定期 |
|
|
67
|
+
|
|
68
|
+
状态变化通过轮询发现和确认;稳定期不是精确定时,实际确认可能延迟至下一次轮询。默认查询间隔为 60 秒,若希望更及时地确认默认 30 秒稳定期,可将 `interval` 调整至 30 秒或更短(最小 10 秒);更频繁的轮询会增加 Steam API 请求量。已有 `interval` 配置不会自动改变。
|
|
69
|
+
|
|
70
|
+
游戏切换时,如果上一个游戏未满阈值,只播报新游戏开始;结束状态仍会更新。时长以插件首次检测到开始和结束的时间估算,而非 Steam 官方精确游戏时长;轮询间隔可能影响估算,旧会话可能受升级前播报延迟影响。
|
|
71
|
+
|
|
72
|
+
> 合并播报以文字汇总发送,不附单人图片。候选状态和待发送队列保存在内存中,插件重启会清空队列;未成功确认的状态将在后续轮询重新检测。发送失败的群组会在下一次轮询时重试;不同群组独立发送。
|
|
52
73
|
|
|
53
74
|
## 其他说明
|
|
54
75
|
|
package/src/broadcast.ts
CHANGED
|
@@ -16,6 +16,11 @@ export interface SteamBroadcastConfig {
|
|
|
16
16
|
SteamApiKey: string;
|
|
17
17
|
useSteamName: boolean;
|
|
18
18
|
broadcastWithImage: boolean;
|
|
19
|
+
mergeBroadcast?: boolean;
|
|
20
|
+
immediateBroadcast?: boolean;
|
|
21
|
+
mergeWindow?: number;
|
|
22
|
+
stabilityPeriod?: number;
|
|
23
|
+
minStopPlayMinutes?: number;
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
type SteamPlayer = SteamUserInfo["response"]["players"][number];
|
|
@@ -67,6 +72,10 @@ export async function pollSteamBroadcast(
|
|
|
67
72
|
logger,
|
|
68
73
|
shouldStop,
|
|
69
74
|
);
|
|
75
|
+
if (config.mergeBroadcast !== false && !config.immediateBroadcast) {
|
|
76
|
+
await processMergedBroadcast(ctx, config, logger, shouldStop, channels, allUserData, steamUserInfo, changes);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
70
79
|
if (Object.keys(changes).length === 0) {
|
|
71
80
|
logger.debug("本轮没有检测到游戏状态变化");
|
|
72
81
|
return;
|
|
@@ -88,6 +97,13 @@ export async function pollSteamBroadcast(
|
|
|
88
97
|
continue;
|
|
89
98
|
}
|
|
90
99
|
|
|
100
|
+
if (isShortSession(changeInfo, config)) {
|
|
101
|
+
if (changeInfo.status === "stop") {
|
|
102
|
+
await acknowledgeGameChange(ctx, changeInfo, logger);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
changeInfo.suppressEnd = true;
|
|
106
|
+
}
|
|
91
107
|
const playerInfo = steamUserInfo.response.players.find(
|
|
92
108
|
(player) => player.steamid === user.steamId,
|
|
93
109
|
);
|
|
@@ -157,6 +173,111 @@ export async function pollSteamBroadcast(
|
|
|
157
173
|
}
|
|
158
174
|
}
|
|
159
175
|
|
|
176
|
+
// 运行期队列:候选状态需两次成功轮询确认,群组发送成功后不重复投递。
|
|
177
|
+
type Candidate = { signature: string; since: number; firstChange: GameChangeInfo };
|
|
178
|
+
type QueuedChange = { signature: string; change: GameChangeInfo; queuedAt: number; pendingChannels: Set<string> };
|
|
179
|
+
type MergeState = { candidates: Map<string, Candidate>; queue: Map<string, QueuedChange> };
|
|
180
|
+
const mergeStates = new WeakMap<Context, MergeState>();
|
|
181
|
+
function getMergeState(ctx: Context): MergeState {
|
|
182
|
+
let state = mergeStates.get(ctx);
|
|
183
|
+
if (!state) { state = { candidates: new Map(), queue: new Map() }; mergeStates.set(ctx, state); }
|
|
184
|
+
return state;
|
|
185
|
+
}
|
|
186
|
+
function changeSignature(change: GameChangeInfo): string {
|
|
187
|
+
return JSON.stringify([change.oldGame || "", change.newGame || "", change.newGameId || ""]);
|
|
188
|
+
}
|
|
189
|
+
async function processMergedBroadcast(
|
|
190
|
+
ctx: Context, config: SteamBroadcastConfig, logger: PluginLogger, shouldStop: () => boolean,
|
|
191
|
+
channels: any[], users: SteamUser[], steamUserInfo: SteamUserInfo, changes: Record<string, GameChangeInfo>,
|
|
192
|
+
): Promise<void> {
|
|
193
|
+
const state = getMergeState(ctx);
|
|
194
|
+
const now = Date.now();
|
|
195
|
+
const stableMs = Math.max(0, config.stabilityPeriod ?? 30) * 1000;
|
|
196
|
+
const mergeMs = Math.max(0, config.mergeWindow ?? 180) * 1000;
|
|
197
|
+
const observed = new Set(steamUserInfo.response.players.map((p) => p.steamid));
|
|
198
|
+
const userMap = new Map(users.map((u) => [u.userId, u]));
|
|
199
|
+
// 消失、解绑或恢复原状时取消过期状态;未返回玩家时不认定稳定。
|
|
200
|
+
for (const id of new Set([...state.candidates.keys(), ...state.queue.keys()])) {
|
|
201
|
+
const user = userMap.get(id);
|
|
202
|
+
if (!user || !observed.has(user.steamId) || !changes[id]) {
|
|
203
|
+
state.candidates.delete(id);
|
|
204
|
+
if (!user || (observed.has(user.steamId) && !changes[id])) state.queue.delete(id);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (const change of Object.values(changes)) {
|
|
208
|
+
const signature = changeSignature(change);
|
|
209
|
+
const previous = state.candidates.get(change.userId);
|
|
210
|
+
if (!previous || previous.signature !== signature) {
|
|
211
|
+
state.candidates.set(change.userId, { signature, since: now, firstChange: { ...change } });
|
|
212
|
+
state.queue.delete(change.userId);
|
|
213
|
+
if (stableMs > 0) continue;
|
|
214
|
+
} else if (now - previous.since < stableMs) continue;
|
|
215
|
+
const firstChange = state.candidates.get(change.userId)!.firstChange;
|
|
216
|
+
if (isShortSession(firstChange, config)) {
|
|
217
|
+
if (firstChange.status === "stop") {
|
|
218
|
+
if (await acknowledgeGameChange(ctx, firstChange, logger)) {
|
|
219
|
+
state.queue.delete(change.userId);
|
|
220
|
+
state.candidates.delete(change.userId);
|
|
221
|
+
}
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
change.suppressEnd = true;
|
|
225
|
+
}
|
|
226
|
+
const queued = state.queue.get(change.userId);
|
|
227
|
+
if (queued?.signature === signature) continue;
|
|
228
|
+
const user = userMap.get(change.userId);
|
|
229
|
+
if (!user) continue;
|
|
230
|
+
change.observedAt = state.candidates.get(change.userId)!.since;
|
|
231
|
+
const pendingChannels = new Set(channels.filter((c) => user.effectGroups.includes(c.id)).map((c) => c.id as string));
|
|
232
|
+
if (pendingChannels.size) state.queue.set(change.userId, { signature, change, queuedAt: now, pendingChannels });
|
|
233
|
+
}
|
|
234
|
+
// 每个群从其首条稳定动态入队开始计时,到期后一次汇总。
|
|
235
|
+
for (const channel of channels) {
|
|
236
|
+
if (shouldStop()) return;
|
|
237
|
+
const entries = [...state.queue.values()].filter((entry) => {
|
|
238
|
+
const candidate = state.candidates.get(entry.change.userId);
|
|
239
|
+
return entry.pendingChannels.has(channel.id)
|
|
240
|
+
&& observed.has(userMap.get(entry.change.userId)?.steamId || "")
|
|
241
|
+
&& candidate?.signature === entry.signature
|
|
242
|
+
&& now - candidate.since >= stableMs;
|
|
243
|
+
});
|
|
244
|
+
if (!entries.length || now - Math.min(...entries.map((e) => e.queuedAt)) < mergeMs) continue;
|
|
245
|
+
const bot = ctx.bots[`${channel.platform}:${channel.assignee}`] || Object.values(ctx.bots).find(
|
|
246
|
+
(item) => item.platform === channel.platform && (item.selfId === channel.assignee || item.user?.id === channel.assignee),
|
|
247
|
+
);
|
|
248
|
+
if (!bot) { logger.warn(`未找到群组 ${channel.id} 对应的机器人,保留待发送播报`); continue; }
|
|
249
|
+
try {
|
|
250
|
+
const lines: string[] = [];
|
|
251
|
+
for (const entry of entries) {
|
|
252
|
+
const change = entry.change;
|
|
253
|
+
const display: GameChangeInfo = {
|
|
254
|
+
...change,
|
|
255
|
+
oldGame: change.oldGame ? await getLocalizedGameName(ctx, change.oldGameId, change.oldGame) : undefined,
|
|
256
|
+
newGame: change.newGame ? await getLocalizedGameName(ctx, change.newGameId, change.newGame) : undefined,
|
|
257
|
+
};
|
|
258
|
+
const nickname = await getUserNickname(ctx, change.userId, channel.id);
|
|
259
|
+
lines.push(`• ${buildBroadcastText(nickname || change.userName, display)}`);
|
|
260
|
+
}
|
|
261
|
+
if (shouldStop()) return;
|
|
262
|
+
await bot.sendMessage(channel.id, `🎮 Steam 好友动态\n${lines.join("\n")}`);
|
|
263
|
+
for (const entry of entries) entry.pendingChannels.delete(channel.id);
|
|
264
|
+
} catch (error) { logger.warn(`群组 ${channel.id} 合并播报失败,将在下次轮询重试:`, error); }
|
|
265
|
+
}
|
|
266
|
+
for (const [id, entry] of state.queue) {
|
|
267
|
+
const user = userMap.get(id);
|
|
268
|
+
// 群组已关闭或解绑时不再继续投递。
|
|
269
|
+
for (const channelId of entry.pendingChannels) {
|
|
270
|
+
if (!channels.some((c) => c.id === channelId) || !user?.effectGroups.includes(channelId)) entry.pendingChannels.delete(channelId);
|
|
271
|
+
}
|
|
272
|
+
if (!entry.pendingChannels.size) {
|
|
273
|
+
if (await acknowledgeGameChange(ctx, entry.change, logger)) {
|
|
274
|
+
state.queue.delete(id);
|
|
275
|
+
state.candidates.delete(id);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
160
281
|
// 更新所有已绑定用户的头像
|
|
161
282
|
export async function updatePlayerHeadshots(
|
|
162
283
|
ctx: Context,
|
|
@@ -224,7 +345,10 @@ async function detectGameChanges(
|
|
|
224
345
|
continue;
|
|
225
346
|
}
|
|
226
347
|
|
|
227
|
-
const
|
|
348
|
+
const session = await getGameSession(ctx, userData.userId);
|
|
349
|
+
const playDurationMs = session && Number.isFinite(session.startTime) ? Math.max(0, Date.now() - session.startTime) : undefined;
|
|
350
|
+
const minutes = playDurationMs === undefined ? 0 : Math.floor(playDurationMs / 60000);
|
|
351
|
+
const playTime = minutes > 0 ? formatPlayTime(minutes) : undefined;
|
|
228
352
|
const userName = useSteamName ? player.personaname : userData.userName;
|
|
229
353
|
|
|
230
354
|
if (newGame && !oldGame) {
|
|
@@ -245,6 +369,7 @@ async function detectGameChanges(
|
|
|
245
369
|
newGame,
|
|
246
370
|
newGameId,
|
|
247
371
|
playTime,
|
|
372
|
+
playDurationMs,
|
|
248
373
|
};
|
|
249
374
|
} else if (!newGame && oldGame) {
|
|
250
375
|
changes[userData.userId] = {
|
|
@@ -254,6 +379,7 @@ async function detectGameChanges(
|
|
|
254
379
|
oldGame,
|
|
255
380
|
oldGameId,
|
|
256
381
|
playTime,
|
|
382
|
+
playDurationMs,
|
|
257
383
|
};
|
|
258
384
|
}
|
|
259
385
|
}
|
|
@@ -261,22 +387,17 @@ async function detectGameChanges(
|
|
|
261
387
|
return changes;
|
|
262
388
|
}
|
|
263
389
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
const session = await getGameSession(ctx, userId);
|
|
269
|
-
if (!session) return undefined;
|
|
270
|
-
|
|
271
|
-
const minutes = Math.floor((Date.now() - session.startTime) / 60000);
|
|
272
|
-
return minutes > 0 ? formatPlayTime(minutes) : undefined;
|
|
390
|
+
function isShortSession(change: GameChangeInfo, config: SteamBroadcastConfig): boolean {
|
|
391
|
+
const threshold = Math.max(0, config.minStopPlayMinutes ?? 10) * 60_000;
|
|
392
|
+
return (change.status === "stop" || change.status === "change")
|
|
393
|
+
&& change.playDurationMs !== undefined && change.playDurationMs < threshold;
|
|
273
394
|
}
|
|
274
395
|
|
|
275
396
|
function buildBroadcastText(
|
|
276
397
|
displayName: string,
|
|
277
398
|
changeInfo: GameChangeInfo,
|
|
278
399
|
): string {
|
|
279
|
-
if (changeInfo.status === "start") {
|
|
400
|
+
if (changeInfo.status === "start" || (changeInfo.status === "change" && changeInfo.suppressEnd)) {
|
|
280
401
|
return `${displayName} 开始玩 ${changeInfo.newGame} 了`;
|
|
281
402
|
}
|
|
282
403
|
|
|
@@ -320,13 +441,13 @@ async function acknowledgeGameChange(
|
|
|
320
441
|
ctx: Context,
|
|
321
442
|
changeInfo: GameChangeInfo,
|
|
322
443
|
logger: PluginLogger,
|
|
323
|
-
): Promise<
|
|
444
|
+
): Promise<boolean> {
|
|
324
445
|
try {
|
|
325
446
|
if (changeInfo.status === "start") {
|
|
326
|
-
await startGameSession(ctx, changeInfo.userId, changeInfo.newGame || "");
|
|
447
|
+
await startGameSession(ctx, changeInfo.userId, changeInfo.newGame || "", changeInfo.observedAt);
|
|
327
448
|
} else if (changeInfo.status === "change") {
|
|
328
449
|
await endGameSession(ctx, changeInfo.userId);
|
|
329
|
-
await startGameSession(ctx, changeInfo.userId, changeInfo.newGame || "");
|
|
450
|
+
await startGameSession(ctx, changeInfo.userId, changeInfo.newGame || "", changeInfo.observedAt);
|
|
330
451
|
} else {
|
|
331
452
|
await endGameSession(ctx, changeInfo.userId);
|
|
332
453
|
}
|
|
@@ -336,11 +457,13 @@ async function acknowledgeGameChange(
|
|
|
336
457
|
{ userId: changeInfo.userId },
|
|
337
458
|
{ lastPlayedGame: changeInfo.newGame || "", lastPlayedGameId: changeInfo.newGameId || "" },
|
|
338
459
|
);
|
|
460
|
+
return true;
|
|
339
461
|
} catch (error) {
|
|
340
462
|
logger.error(
|
|
341
463
|
`播报已发送,但更新用户 ${changeInfo.userId} 的游戏状态失败:`,
|
|
342
464
|
error,
|
|
343
465
|
);
|
|
466
|
+
return false;
|
|
344
467
|
}
|
|
345
468
|
}
|
|
346
469
|
|
package/src/index.ts
CHANGED
|
@@ -70,7 +70,7 @@ export const Config = Schema.intersect([
|
|
|
70
70
|
interval: Schema.number()
|
|
71
71
|
.default(60)
|
|
72
72
|
.min(10)
|
|
73
|
-
.description("
|
|
73
|
+
.description("查询间隔,单位:秒;状态稳定期需经后续轮询确认,建议不大于稳定期"),
|
|
74
74
|
useSteamName: Schema.boolean()
|
|
75
75
|
.default(true)
|
|
76
76
|
.description("使用Steam昵称,关闭时使用的QQ昵称"),
|
|
@@ -79,6 +79,30 @@ export const Config = Schema.intersect([
|
|
|
79
79
|
.description("播报时附带图片"),
|
|
80
80
|
}).description("基础设置"),
|
|
81
81
|
|
|
82
|
+
Schema.object({
|
|
83
|
+
mergeBroadcast: Schema.boolean()
|
|
84
|
+
.default(true)
|
|
85
|
+
.description("合并窗口内的游戏状态播报"),
|
|
86
|
+
mergeWindow: Schema.number()
|
|
87
|
+
.default(180)
|
|
88
|
+
.min(1)
|
|
89
|
+
.step(1)
|
|
90
|
+
.description("合并播报窗口,单位:秒"),
|
|
91
|
+
stabilityPeriod: Schema.number()
|
|
92
|
+
.default(30)
|
|
93
|
+
.min(0)
|
|
94
|
+
.step(1)
|
|
95
|
+
.description("状态稳定期,单位:秒;需通过后续轮询确认"),
|
|
96
|
+
minStopPlayMinutes: Schema.number()
|
|
97
|
+
.default(10)
|
|
98
|
+
.min(0)
|
|
99
|
+
.step(1)
|
|
100
|
+
.description("结束播报最短游戏时长(分钟),设为 0 关闭过滤;时长未知时仍播报"),
|
|
101
|
+
immediateBroadcast: Schema.boolean()
|
|
102
|
+
.default(false)
|
|
103
|
+
.description("即时播报,跳过合并窗口和状态稳定期"),
|
|
104
|
+
}).description("播报设置"),
|
|
105
|
+
|
|
82
106
|
Schema.object({
|
|
83
107
|
useApifoxProxy: Schema.boolean()
|
|
84
108
|
.default(true)
|
|
@@ -154,6 +178,11 @@ export interface PluginConfig {
|
|
|
154
178
|
interval: number;
|
|
155
179
|
useSteamName: boolean;
|
|
156
180
|
broadcastWithImage: boolean;
|
|
181
|
+
mergeBroadcast: boolean;
|
|
182
|
+
mergeWindow: number;
|
|
183
|
+
stabilityPeriod: number;
|
|
184
|
+
immediateBroadcast: boolean;
|
|
185
|
+
minStopPlayMinutes: number;
|
|
157
186
|
useApifoxProxy: boolean;
|
|
158
187
|
maxRetries: number;
|
|
159
188
|
showcardmode: "1" | "2";
|
package/src/types.ts
CHANGED
package/src/userdata.ts
CHANGED
|
@@ -98,11 +98,11 @@ async function saveGameSessionsLocked(ctx: Context, data: GameSessionsData): Pro
|
|
|
98
98
|
/**
|
|
99
99
|
* 记录用户开始玩游戏
|
|
100
100
|
*/
|
|
101
|
-
export async function startGameSession(ctx: Context, userId: string, gameName: string): Promise<void> {
|
|
101
|
+
export async function startGameSession(ctx: Context, userId: string, gameName: string, startTime: number = Date.now()): Promise<void> {
|
|
102
102
|
const sessions = await loadGameSessionsLocked(ctx);
|
|
103
103
|
sessions[userId] = {
|
|
104
104
|
gameName,
|
|
105
|
-
startTime
|
|
105
|
+
startTime,
|
|
106
106
|
};
|
|
107
107
|
await saveGameSessionsLocked(ctx, sessions);
|
|
108
108
|
}
|