UNPKG

koishi-plugin-steam-family-bot

Version:

一个用于新版 Steam 家庭的库存监控 koishi Bot 插件

85 lines 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SteamFamilyLibDAO = void 0; const koishi_1 = require("koishi"); class SteamFamilyLibDAO { db; constructor(db) { this.db = db; } async getSteamFamilyLibByFamilyId(familyId, type) { const res = await this.db .join(['SteamFamilyLib', 'SteamGameInfo'], (lib, info) => koishi_1.$.eq(lib.appId, info.appid), [false, true]) .where((row) => { let condition = koishi_1.$.or(koishi_1.$.eq(row.SteamFamilyLib.type, 'lib'), koishi_1.$.eq(row.SteamFamilyLib.type, 'wish')); if (type) { condition = koishi_1.$.eq(row.SteamFamilyLib.type, type); } return koishi_1.$.and(condition, koishi_1.$.eq(row.SteamFamilyLib.familyId, familyId)); }) .execute(); return res.map((it) => ({ ...it.SteamFamilyLib, info: it.SteamGameInfo, })); } async batchUpsertLibInfo(infos) { await this.db.upsert('SteamGameInfo', infos); } async getUnSyncedLib(limit = 500) { const res = await this.db .join(['SteamFamilyLib', 'SteamGameInfo'], (lib, info) => koishi_1.$.eq(lib.appId, info.appid), [false, true]) .where((row) => { return koishi_1.$.or(koishi_1.$.lt(koishi_1.$.number(row.SteamGameInfo.lastRefreshedAt), koishi_1.$.add(Math.floor(Date.now() / 1000), -14 * 24 * 60 * 60)), koishi_1.$.eq(row.SteamGameInfo.appid, null)); }) .limit(limit) .execute(); return res.map((it) => it.SteamFamilyLib); } async getLibByKeywordAndFamilyId(familyId, queryKey) { const res = await this.db .join(['SteamFamilyLib', 'SteamGameInfo'], (lib, info) => koishi_1.$.eq(lib.appId, info.appid), [false, true]) .where((row) => { return koishi_1.$.and(koishi_1.$.or(koishi_1.$.eq(row.SteamFamilyLib.appId.toString(), queryKey), koishi_1.$.regex(row.SteamFamilyLib.name, new RegExp(escapeRegExp(queryKey), 'i')), koishi_1.$.regex(row.SteamGameInfo.aliases, new RegExp(escapeRegExp(queryKey), 'i'))), koishi_1.$.eq(row.SteamFamilyLib.familyId, familyId), koishi_1.$.eq(row.SteamFamilyLib.type, 'lib')); }) .execute(); return res.map((it) => ({ ...it.SteamFamilyLib, info: it.SteamGameInfo })); } getFamilyWishes(familyId) { return this.db.get('SteamFamilyLib', { familyId: familyId, type: 'wish', }); } async batchRemoveByAppIdAndFamilyId(steamFamilyId, appIds, type = 'lib') { await this.db.remove('SteamFamilyLib', { appId: appIds, familyId: steamFamilyId, type: type, }); } async batchUpsertFamilyLib(libs) { await this.db.upsert('SteamFamilyLib', libs); } async refreshLibByFamilyId(familyId, dbContent, withWishes = false) { await this.db.withTransaction(async (database) => { // @ts-ignore const res = await database.remove('SteamFamilyLib', { // @ts-ignore familyId: familyId, // @ts-ignore type: withWishes ? ['lib', 'wish'] : 'lib', }); const insertRes = await database.upsert('SteamFamilyLib', dbContent); }); } } exports.SteamFamilyLibDAO = SteamFamilyLibDAO; function escapeRegExp(str) { const escaped = str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); const ignoreCaseInput = escaped.replace(/[a-z]/gi, (letter) => { return `[${letter.toUpperCase()}${letter.toLowerCase()}]`; }); return ignoreCaseInput; } //# sourceMappingURL=steamFamilyLibDAO.js.map