steam-family-bot-core
Version:
一个用于新版 Steam 家庭的库存监控 Bot 插件
60 lines • 3.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.libMonitor = void 0;
const lodash_1 = __importDefault(require("lodash"));
const utils_1 = require("../../utils");
const handleTokenInvalid_1 = require("../../schedules/family-lib-syncer/handleTokenInvalid");
const buildMsgs_1 = require("../../schedules/family-lib-syncer/buildMsgs");
const messageSender_1 = require("../../schedules/family-lib-syncer/messageSender");
const getLibChange_1 = require("../../schedules/family-lib-syncer/getLibChange");
const libMonitor = async (c) => {
const { steam, botService, config, logger } = c;
logger.info('trigger lib monitor');
const subscribes = await steam.db.getAllSubscription();
for (const item of subscribes) {
try {
await handleSubScribe(steam, item, botService, config, logger);
}
catch (e) {
logger.error(`some error occur during handle steam subscription familyId: ${item.steamAndFamilyRel.familyId}, ${e?.stack}`);
}
}
};
exports.libMonitor = libMonitor;
const handleSubScribe = async (steam, item, botService, config, logger) => {
logger.debug(`start handle family subscribe ${item.steamAndFamilyRel.familyId}`);
const session = botService.getSessionByChannelInfo(item.channel);
if (!session) {
logger.info(`it's seem that bot [${JSON.stringify(item.channel)}] for family 「${item.steamAndFamilyRel.familyId}」have down. skip it`);
return;
}
// if invalid, will auto-renew account token
const apiServiceResult = await steam.createAPIWithCurAccount(item.account);
// eslint-disable-next-line prefer-spread
const trans = (...args) => session.text.apply(session, args);
if (!apiServiceResult.isSuccess()) {
logger.error(`token invalid ${apiServiceResult.message}`);
await (0, handleTokenInvalid_1.handleTokenInvalid)(logger, item, session, steam);
return;
}
const { memberDict, newWishes, modifiedWishes, deletedWishes, wishesDict, newLibs, modifiedLibs, deletedLibs, libDict, } = await (0, getLibChange_1.getChangedLibs)(apiServiceResult.data, steam, item, logger);
await (0, getLibChange_1.updateDB)(item, logger, steam, newWishes, modifiedWishes, deletedWishes, newLibs, modifiedLibs, deletedLibs);
const msgs = await (0, buildMsgs_1.buildMessages)(memberDict, newWishes, modifiedWishes, deletedWishes, wishesDict, newLibs, modifiedLibs, deletedLibs, libDict, item, trans);
logger.info(`steam 家庭「${item.subscription.steamFamilyId}」库存/愿望单变更 ${msgs.length}`);
const apps = msgs.map((msg) => msg.relateAppId);
const appDetails = (await Promise.all(lodash_1.default.chunk(apps, 30).map((appChunk) => steam.api.Steam.getSteamItems(appChunk)))).flatMap((it) => it.data.storeItems);
const appDetailsDict = lodash_1.default.keyBy(appDetails, 'appid');
const mappedMsgs = msgs.map((msg) => {
const app = appDetailsDict[msg.relateAppId];
const img = (0, utils_1.getGameCapsule)(app, item.subscription.preferGameImgType);
return {
text: msg.text,
img: img,
};
});
await (0, messageSender_1.sendMessages)(mappedMsgs, session);
};
//# sourceMappingURL=index.js.map