'use strict'; const fs = require('fs'); const path = require('path'); /** * 机器人注册表持久化:保存「非敏感」的机器人设置。 * * 存的是:是否启用、备注名、启动后是否自动开始扩地、以及每个机器人自己的扩地配置覆盖。 * **绝不存账号密码** —— 密码只从 `.env` 读取(见 {@link module:config/botsConfig})。 * * 默认路径 `data/bots.json`(`data/` 已在 .gitignore 中忽略)。 */ class BotStore { /** * @param {string} [filePath] 配置文件绝对路径 */ constructor(filePath) { this.filePath = filePath || path.join(__dirname, '..', '..', 'data', 'bots.json'); } /** * 读取注册表;文件不存在或损坏时返回空表。 * @returns {{bots: Record}} */ load() { try { const parsed = JSON.parse(fs.readFileSync(this.filePath, 'utf8')); if (parsed && typeof parsed === 'object' && parsed.bots && typeof parsed.bots === 'object') { return { bots: parsed.bots }; } } catch { // 首次运行或文件损坏:回退到空表 } return { bots: {} }; } /** * 写入注册表。 * @param {{bots: Record}} data * @returns {boolean} 是否写入成功 */ save(data) { try { fs.mkdirSync(path.dirname(this.filePath), { recursive: true }); fs.writeFileSync(this.filePath, JSON.stringify({ bots: data.bots || {} }, null, 2), 'utf8'); return true; } catch { return false; } } } module.exports = { BotStore };