From 5e1911a2c32bcd1405b7d1cf3acbac4e78aa0b36 Mon Sep 17 00:00:00 2001 From: Jerry Date: Tue, 29 Apr 2025 13:12:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/bot/bot.service.ts | 13 ++++++++++--- src/services/redis/redis.ts | 3 --- src/utils/redis/persistence.ts | 20 ++++++++++---------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/modules/bot/bot.service.ts b/src/modules/bot/bot.service.ts index 6fc1242..cb0eff5 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -2,6 +2,7 @@ import logger from '../../utils/core/logger'; import paths from '../../utils/core/path'; import fs from 'fs/promises'; import path from 'path'; +import redisService from '../../services/redis/redis'; class BotService { public async getBotId() { @@ -16,16 +17,22 @@ class BotService { continue; } try { + const fileContent: string | undefined = await redisService.fetch('crystelfBots', fileName); + if (fileContent) { + uins.push(fileContent); + } + /* const filePath = path.join(botsPath, fileName); const fileContent = await fs.readFile(filePath, 'utf-8'); const jsonData = JSON.parse(fileContent); if (jsonData.uin) { uins.push(jsonData.uin); - } - } catch (e) { - logger.error(`读取或解析${fileName}出错`); + }*/ + } catch (err) { + logger.error(`读取或解析${fileName}出错: ${err}`); } } + logger.debug(uins); return uins; } } diff --git a/src/services/redis/redis.ts b/src/services/redis/redis.ts index 85a249a..a46026e 100644 --- a/src/services/redis/redis.ts +++ b/src/services/redis/redis.ts @@ -181,9 +181,6 @@ class RedisService { return; } - /** - * 测试方法:示例性地获取一个用户数据 - */ public async test(): Promise { const user = await this.fetch('Jerry', 'IUser'); logger.debug('User:', user); diff --git a/src/utils/redis/persistence.ts b/src/utils/redis/persistence.ts index d1b0ad6..104b138 100644 --- a/src/utils/redis/persistence.ts +++ b/src/utils/redis/persistence.ts @@ -5,26 +5,26 @@ import logger from '../core/logger'; import fs from 'fs/promises'; class Persistence { - private static getUserDataPath(username: string, fileName: string): string { - return path.join(paths.get('userData'), username, `${fileName}.json`); + private static getDataPath(dataName: string, fileName: string): string { + return path.join(paths.get('userData'), dataName, `${fileName}.json`); } - private static async ensureUserPath(username: string): Promise { - const userPath = path.join(paths.get('userData'), username); + private static async ensureUserPath(dataName: string): Promise { + const dataPath = path.join(paths.get('userData'), dataName); try { - await fc.createDir(userPath, false); + await fc.createDir(dataPath, false); } catch (err) { logger.error(err); } } public static async writeDataLocal( - username: string, + dataName: string, data: T, fileName: string ): Promise { - await this.ensureUserPath(username); - const filePath = this.getUserDataPath(username, fileName); + await this.ensureUserPath(dataName); + const filePath = this.getDataPath(dataName, fileName); try { await fs.writeFile(filePath, JSON.stringify(data, null, 2), 'utf-8'); @@ -34,8 +34,8 @@ class Persistence { } } - public static async readDataLocal(username: string, fileName: string): Promise { - const filePath = this.getUserDataPath(username, fileName); + public static async readDataLocal(dataName: string, fileName: string): Promise { + const filePath = this.getDataPath(dataName, fileName); try { const data = await fs.readFile(filePath, 'utf-8');