mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 06:39:18 +00:00
文案重载
This commit is contained in:
parent
cdbe05be1a
commit
fb9be8ded7
@ -93,7 +93,7 @@ class BotService {
|
|||||||
* @param message 消息
|
* @param message 消息
|
||||||
*/
|
*/
|
||||||
public async sendMessage(groupId: number, message: string): Promise<boolean> {
|
public async sendMessage(groupId: number, message: string): Promise<boolean> {
|
||||||
logger.debug('sendGroupMessage..');
|
logger.info(`发送${message}到${groupId}..`);
|
||||||
const sendBot: number | undefined = await this.getGroupBot(groupId);
|
const sendBot: number | undefined = await this.getGroupBot(groupId);
|
||||||
if (!sendBot) {
|
if (!sendBot) {
|
||||||
logger.warn(`不存在能向群聊${groupId}发送消息的Bot!`);
|
logger.warn(`不存在能向群聊${groupId}发送消息的Bot!`);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import WordsService from './words.service';
|
import WordsService from './words.service';
|
||||||
import response from '../../utils/core/response';
|
import response from '../../utils/core/response';
|
||||||
|
import tools from '../../utils/modules/tools';
|
||||||
|
|
||||||
class WordsController {
|
class WordsController {
|
||||||
private readonly router: express.Router;
|
private readonly router: express.Router;
|
||||||
@ -16,12 +17,18 @@ class WordsController {
|
|||||||
|
|
||||||
private init(): void {
|
private init(): void {
|
||||||
this.router.get('/getText/:id', this.getText);
|
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<void> => {
|
private getText = async (req: express.Request, res: express.Response): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const id = req.params.id;
|
const id = req.params.id;
|
||||||
const texts = await WordsService.loadWordById(id);
|
const texts = await WordsService.loadWordById(id.toString());
|
||||||
if (!texts || texts.length === 0) {
|
if (!texts || texts.length === 0) {
|
||||||
return await response.error(res, `文案${id}不存在或为空..`, 404);
|
return await response.error(res, `文案${id}不存在或为空..`, 404);
|
||||||
}
|
}
|
||||||
@ -32,5 +39,28 @@ class WordsController {
|
|||||||
await response.error(res);
|
await response.error(res);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重载文案
|
||||||
|
* @param req
|
||||||
|
* @param res
|
||||||
|
*/
|
||||||
|
private reloadWord = async (req: express.Request, res: express.Response): Promise<void> => {
|
||||||
|
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();
|
export default new WordsController();
|
||||||
|
@ -4,8 +4,14 @@ import fs from 'fs/promises';
|
|||||||
import logger from '../../utils/core/logger';
|
import logger from '../../utils/core/logger';
|
||||||
|
|
||||||
class WordsService {
|
class WordsService {
|
||||||
private wordCache: Record<string, string[]> = {};
|
private wordCache: Record<string, string[]> = {}; //缓存
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从本地加载json到内存&返回
|
||||||
|
* @param id 文件名
|
||||||
|
*/
|
||||||
public async loadWordById(id: string): Promise<string[] | null> {
|
public async loadWordById(id: string): Promise<string[] | null> {
|
||||||
|
logger.info(`Loading words ${id}..`);
|
||||||
if (this.wordCache[id]) return this.wordCache[id];
|
if (this.wordCache[id]) return this.wordCache[id];
|
||||||
const filePath = path.join(paths.get('words'), `${id}.json`);
|
const filePath = path.join(paths.get('words'), `${id}.json`);
|
||||||
try {
|
try {
|
||||||
@ -23,5 +29,27 @@ class WordsService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重载json到内存
|
||||||
|
* @param id 文件名
|
||||||
|
*/
|
||||||
|
public async reloadWord(id: string): Promise<boolean> {
|
||||||
|
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();
|
export default new WordsService();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user