diff --git a/src/modules/bot/bot.service.ts b/src/modules/bot/bot.service.ts index dc9dff6..2eab80e 100644 --- a/src/modules/bot/bot.service.ts +++ b/src/modules/bot/bot.service.ts @@ -93,7 +93,7 @@ class BotService { * @param message 消息 */ public async sendMessage(groupId: number, message: string): Promise { - logger.debug('sendGroupMessage..'); + logger.info(`发送${message}到${groupId}..`); const sendBot: number | undefined = await this.getGroupBot(groupId); if (!sendBot) { logger.warn(`不存在能向群聊${groupId}发送消息的Bot!`); diff --git a/src/modules/words/words.controller.ts b/src/modules/words/words.controller.ts index 9cb0cef..30852b9 100644 --- a/src/modules/words/words.controller.ts +++ b/src/modules/words/words.controller.ts @@ -1,6 +1,7 @@ import express from 'express'; import WordsService from './words.service'; import response from '../../utils/core/response'; +import tools from '../../utils/modules/tools'; class WordsController { private readonly router: express.Router; @@ -16,12 +17,18 @@ class WordsController { private init(): void { this.router.get('/getText/:id', this.getText); + this.router.post('/reloadText', this.reloadWord); } + /** + * 获取随机文案 + * @param req + * @param res + */ private getText = async (req: express.Request, res: express.Response): Promise => { try { const id = req.params.id; - const texts = await WordsService.loadWordById(id); + const texts = await WordsService.loadWordById(id.toString()); if (!texts || texts.length === 0) { return await response.error(res, `文案${id}不存在或为空..`, 404); } @@ -32,5 +39,28 @@ class WordsController { await response.error(res); } }; + + /** + * 重载文案 + * @param req + * @param res + */ + private reloadWord = async (req: express.Request, res: express.Response): Promise => { + try { + const id = req.params.id; + const token = req.params.token; + if (tools.checkToken(token)) { + if (await WordsService.reloadWord(id.toString())) { + await response.success(res, '成功重载..'); + } else { + await response.error(res, '重载失败..'); + } + } else { + await tools.tokenCheckFailed(res, token); + } + } catch (e) { + await response.error(res); + } + }; } export default new WordsController(); diff --git a/src/modules/words/words.service.ts b/src/modules/words/words.service.ts index 65cb9bd..0b42bd2 100644 --- a/src/modules/words/words.service.ts +++ b/src/modules/words/words.service.ts @@ -4,8 +4,14 @@ import fs from 'fs/promises'; import logger from '../../utils/core/logger'; class WordsService { - private wordCache: Record = {}; + private wordCache: Record = {}; //缓存 + + /** + * 从本地加载json到内存&返回 + * @param id 文件名 + */ public async loadWordById(id: string): Promise { + logger.info(`Loading words ${id}..`); if (this.wordCache[id]) return this.wordCache[id]; const filePath = path.join(paths.get('words'), `${id}.json`); try { @@ -23,5 +29,27 @@ class WordsService { return null; } } + + /** + * 重载json到内存 + * @param id 文件名 + */ + public async reloadWord(id: string): Promise { + logger.info(`Reloading word: ${id}..`); + const filePath = path.join(paths.get('words'), `${id}.json`); + try { + const content = await fs.readFile(filePath, 'utf-8'); + const parsed = JSON.parse(content); + if (Array.isArray(parsed)) { + this.wordCache[id] = parsed.filter((item) => typeof item === 'string'); + return true; + } else { + return false; + } + } catch (e) { + logger.error(`Failed to reloadWordById: ${id}..`); + return false; + } + } } export default new WordsService();