refactor: log

This commit is contained in:
linyuchen committed 2024-04-27 11:45:24 +08:00
1 parent f2b2ea61a1
commit 033a7bffb3
9 files changed
+113 -67

No files matched your search

+2 -34
View File
@@ -2,6 +2,7 @@ import fs from 'node:fs';
import path from 'node:path';
import { selfInfo } from '@/common/data';
import { logDebug, logError } from '@/common/utils/log';
import { ConfigBase } from '@/common/utils/ConfigBase';
export interface OB11Config {
httpPort: number;
@@ -29,7 +30,7 @@ export interface OB11Config {
const ob11ConfigDir = path.resolve(__dirname, 'config');
fs.mkdirSync(ob11ConfigDir, { recursive: true });
class Config implements OB11Config {
class Config extends ConfigBase<OB11Config> implements OB11Config {
httpPort: number = 3000;
httpPostUrls: string[] = [];
httpSecret = '';
@@ -47,42 +48,9 @@ class Config implements OB11Config {
heartInterval = 30000;
token = '';
constructor() {
}
getConfigPath() {
return path.join(ob11ConfigDir, `onebot11_${selfInfo.uin}.json`);
}
read() {
const ob11ConfigPath = this.getConfigPath();
if (!fs.existsSync(ob11ConfigPath)) {
logError(`onebot11配置文件 ${ob11ConfigPath} 不存在, 现已创建,请修改配置文件后重启NapCat`);
this.save();
return this;
}
const data = fs.readFileSync(ob11ConfigPath, 'utf-8');
try {
const jsonData = JSON.parse(data);
logDebug('Onebot 11配置文件已加载', jsonData);
Object.assign(this, jsonData);
// eslint-disable-next-line
} catch (e: any) {
if (e instanceof SyntaxError) {
logError(`配置文件 ${ob11ConfigPath} 格式错误,请检查配置文件:`, e.message);
}else{
logError(`读取配置文件 ${ob11ConfigPath} 时发生错误:`, e.message);
}
}
return this;
}
save(newConfig: OB11Config | null = null) {
if (newConfig) {
Object.assign(this, newConfig);
}
fs.writeFileSync(this.getConfigPath(), JSON.stringify(this, null, 4));
}
}
export const ob11Config = new Config();
+13
View File
@@ -0,0 +1,13 @@
import { OB11Message } from '@/onebot11/types';
import { log } from '@/common/utils/log';
import { getGroup } from '@/common/data';
export async function logMessage(ob11Message: OB11Message){
let prefix = '';
if (ob11Message.message_type === 'group') {
const group = await getGroup(ob11Message.group_id!);
prefix = `群[${group?.groupName}(${ob11Message.group_id})] `;
}
const msgString = `${prefix}${ob11Message.sender.nickname}(${ob11Message.sender.user_id}): ${ob11Message.raw_message}`;
log(msgString);
}
+19 -6
View File
@@ -26,6 +26,7 @@ import { OB11GroupAdminNoticeEvent } from '@/onebot11/event/notice/OB11GroupAdmi
import { GroupDecreaseSubType, OB11GroupDecreaseEvent } from '@/onebot11/event/notice/OB11GroupDecreaseEvent';
import { OB11FriendRecallNoticeEvent } from '@/onebot11/event/notice/OB11FriendRecallNoticeEvent';
import { OB11GroupRecallNoticeEvent } from '@/onebot11/event/notice/OB11GroupRecallNoticeEvent';
import { logMessage } from '@/onebot11/log';
export class NapCatOnebot11 {
@@ -40,6 +41,16 @@ export class NapCatOnebot11 {
public onReady() {
logDebug('ob11 ready');
ob11Config.read();
const serviceInfo = `
HTTP服务 ${ob11Config.enableHttp ? '已启动' : '未启动'}, 端口: ${ob11Config.httpPort}
HTTP上报服务 ${ob11Config.enableHttpPost ? '已启动' : '未启动'}, 上报地址: ${ob11Config.httpPostUrls}
WebSocket服务 ${ob11Config.enableWs ? '已启动' : '未启动'}, 端口: ${ob11Config.wsPort}
WebSocket反向服务 ${ob11Config.enableWsReverse ? '已启动' : '未启动'}, 反向地址: ${ob11Config.wsReverseUrls}
`;
log(serviceInfo);
NTQQUserApi.getUserDetailInfo(selfInfo.uin).then(user => {
selfInfo.nick = user.nick;
}).catch(logError);
if (ob11Config.enableHttp) {
ob11HTTPServer.start(ob11Config.httpPort);
}
@@ -78,19 +89,19 @@ export class NapCatOnebot11 {
new Promise((resolve) => {
dbUtil.addMsg(m).then(msgShortId => {
m.id = msgShortId;
this.postReceiveMsg([m]).then().catch(log);
}).catch(log);
this.postReceiveMsg([m]).then().catch(logError);
}).catch(logError);
}).then();
}
};
msgListener.onMsgInfoListUpdate = (msgList) => {
this.postRecallMsg(msgList).then().catch(log);
this.postRecallMsg(msgList).then().catch(logError);
};
msgListener.onAddSendMsg = (msg) => {
if (ob11Config.reportSelfMessage) {
dbUtil.addMsg(msg).then(id => {
msg.id = id;
this.postReceiveMsg([msg]).then().catch(log);
this.postReceiveMsg([msg]).then().catch(logError);
});
}
};
@@ -100,7 +111,7 @@ export class NapCatOnebot11 {
// BuddyListener
const buddyListener = new BuddyListener();
buddyListener.onBuddyReqChange = ((req) => {
this.postFriendRequest(req.buddyReqs).then().catch(log);
this.postFriendRequest(req.buddyReqs).then().catch(logError);
});
napCatCore.addListener(buddyListener);
logDebug('ob11 buddy listener added');
@@ -126,11 +137,12 @@ export class NapCatOnebot11 {
async postReceiveMsg(msgList: RawMessage[]) {
const { debug, reportSelfMessage } = ob11Config;
for (const message of msgList) {
log('收到新消息', message);
logDebug('收到新消息', message);
// if (message.senderUin !== selfInfo.uin){
// message.msgShortId = await dbUtil.addMsg(message);
// }
OB11Constructor.message(message).then((msg) => {
logMessage(msg).then().catch(logError);
if (debug) {
msg.raw = message;
} else {
@@ -138,6 +150,7 @@ export class NapCatOnebot11 {
return;
}
}
logDebug('收到消息: ', msg);
const isSelfMsg = msg.user_id.toString() == selfInfo.uin;
if (isSelfMsg && !reportSelfMessage) {
return;