mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
Merge pull request #57 from Icexbb/main
fix: 整理日志、添加颜色、使用统一的日志函数以提高日志可读性
This commit is contained in:
commit
73045a1b21
@ -19,6 +19,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@log4js-node/log4js-api": "^1.0.2",
|
"@log4js-node/log4js-api": "^1.0.2",
|
||||||
|
"@protobuf-ts/plugin": "^2.9.4",
|
||||||
"@rollup/plugin-node-resolve": "^15.2.3",
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
||||||
"@rollup/plugin-typescript": "^11.1.6",
|
"@rollup/plugin-typescript": "^11.1.6",
|
||||||
"@types/cors": "^2.8.17",
|
"@types/cors": "^2.8.17",
|
||||||
@ -43,11 +44,11 @@
|
|||||||
"vite": "^5.2.6",
|
"vite": "^5.2.6",
|
||||||
"vite-plugin-cp": "^4.0.8",
|
"vite-plugin-cp": "^4.0.8",
|
||||||
"vite-plugin-dts": "^3.8.2",
|
"vite-plugin-dts": "^3.8.2",
|
||||||
"vite-tsconfig-paths": "^4.3.2",
|
"vite-tsconfig-paths": "^4.3.2"
|
||||||
"@protobuf-ts/plugin": "^2.9.4"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ajv": "^8.13.0",
|
"ajv": "^8.13.0",
|
||||||
|
"chalk": "^5.3.0",
|
||||||
"commander": "^12.0.0",
|
"commander": "^12.0.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^5.0.0-beta.2",
|
"express": "^5.0.0-beta.2",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { sleep } from '@/common/utils/helper';
|
import { sleep } from '@/common/utils/helper';
|
||||||
|
import { logError } from './log';
|
||||||
type AsyncQueueTask = (() => void) | (()=>Promise<void>);
|
type AsyncQueueTask = (() => void) | (()=>Promise<void>);
|
||||||
|
|
||||||
|
|
||||||
@ -26,7 +26,8 @@ export class AsyncQueue {
|
|||||||
await taskRet;
|
await taskRet;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
// console.error(e);
|
||||||
|
logError(e);
|
||||||
}
|
}
|
||||||
this.tasks.shift();
|
this.tasks.shift();
|
||||||
await sleep(100);
|
await sleep(100);
|
||||||
|
@ -2,6 +2,7 @@ import path from 'node:path';
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import { systemPlatform } from '@/common/utils/system';
|
import { systemPlatform } from '@/common/utils/system';
|
||||||
|
import { logError } from '@/common/utils/log';
|
||||||
|
|
||||||
export const exePath = process.execPath;
|
export const exePath = process.execPath;
|
||||||
|
|
||||||
@ -49,7 +50,7 @@ if (fs.existsSync(configVersionInfoPath)) {
|
|||||||
const _ =JSON.parse(fs.readFileSync(configVersionInfoPath).toString());
|
const _ =JSON.parse(fs.readFileSync(configVersionInfoPath).toString());
|
||||||
_qqVersionConfigInfo = Object.assign(_qqVersionConfigInfo, _);
|
_qqVersionConfigInfo = Object.assign(_qqVersionConfigInfo, _);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Load QQ version config info failed, Use default version', e);
|
logError('Load QQ version config info failed, Use default version', e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ export async function encodeSilk(filePath: string) {
|
|||||||
const fileHeader = buffer.toString('hex', 0, bytesToRead);
|
const fileHeader = buffer.toString('hex', 0, bytesToRead);
|
||||||
return fileHeader;
|
return fileHeader;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('读取文件错误:', err);
|
logError('读取文件错误:', err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import fs from 'fs';
|
|||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
export function getModuleWithArchName(moduleName: string) {
|
export function getModuleWithArchName(moduleName: string) {
|
||||||
|
@ -498,7 +498,7 @@ class DBUtil extends DBUtilBase {
|
|||||||
if (err)
|
if (err)
|
||||||
logError(err),
|
logError(err),
|
||||||
Promise.reject(),
|
Promise.reject(),
|
||||||
console.log('插入入群时间失败', userId, groupId);
|
logError('插入入群时间失败', userId, groupId);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import fsPromise from 'fs/promises';
|
|||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import util from 'util';
|
import util from 'util';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { log } from './log';
|
import { log, logError } from './log';
|
||||||
import { dbUtil } from '@/common/utils/db';
|
import { dbUtil } from '@/common/utils/db';
|
||||||
import * as fileType from 'file-type';
|
import * as fileType from 'file-type';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
@ -262,12 +262,12 @@ export async function copyFolder(sourcePath: string, destPath: string) {
|
|||||||
try {
|
try {
|
||||||
await fsPromise.copyFile(srcPath, dstPath);
|
await fsPromise.copyFile(srcPath, dstPath);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`无法复制文件 '${srcPath}' 到 '${dstPath}': ${error}`);
|
logError(`无法复制文件 '${srcPath}' 到 '${dstPath}': ${error}`);
|
||||||
// 这里可以决定是否要继续复制其他文件
|
// 这里可以决定是否要继续复制其他文件
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('复制文件夹时出错:', error);
|
logError('复制文件夹时出错:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import path from 'node:path';
|
|||||||
import { SelfInfo } from '@/core';
|
import { SelfInfo } from '@/core';
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import chalk from 'chalk';
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
@ -42,14 +42,14 @@ const logConfig: Configuration = {
|
|||||||
maxLoogSize: 10485760, // 日志文件的最大大小(单位:字节),这里设置为10MB
|
maxLoogSize: 10485760, // 日志文件的最大大小(单位:字节),这里设置为10MB
|
||||||
layout: {
|
layout: {
|
||||||
type: 'pattern',
|
type: 'pattern',
|
||||||
pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] - %m'
|
pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] %X{userInfo} | %m'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ConsoleAppender: { // 输出到控制台的appender
|
ConsoleAppender: { // 输出到控制台的appender
|
||||||
type: 'console',
|
type: 'console',
|
||||||
layout: {
|
layout: {
|
||||||
type: 'pattern',
|
type: 'pattern',
|
||||||
pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] - %m'
|
pattern: `%d{yyyy-MM-dd hh:mm:ss} [%[%p%]] ${chalk.magenta('%X{userInfo}')} | %m`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -61,7 +61,9 @@ const logConfig: Configuration = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
log4js.configure(logConfig);
|
log4js.configure(logConfig);
|
||||||
|
const loggerConsole = log4js.getLogger('console');
|
||||||
|
const loggerFile = log4js.getLogger('file');
|
||||||
|
const loggerDefault = log4js.getLogger('default');
|
||||||
|
|
||||||
export function setLogLevel(fileLogLevel: LogLevel, consoleLogLevel: LogLevel) {
|
export function setLogLevel(fileLogLevel: LogLevel, consoleLogLevel: LogLevel) {
|
||||||
logConfig.categories.file.level = fileLogLevel;
|
logConfig.categories.file.level = fileLogLevel;
|
||||||
@ -70,12 +72,12 @@ export function setLogLevel(fileLogLevel: LogLevel, consoleLogLevel: LogLevel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function setLogSelfInfo(selfInfo: SelfInfo) {
|
export function setLogSelfInfo(selfInfo: SelfInfo) {
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
const userInfo = `${selfInfo.nick}(${selfInfo.uin})`;
|
||||||
// @ts-expect-error
|
loggerConsole.addContext('userInfo', userInfo);
|
||||||
logConfig.appenders.FileAppender.layout.pattern = logConfig.appenders.ConsoleAppender.layout.pattern =
|
loggerFile.addContext('userInfo', userInfo);
|
||||||
`%d{yyyy-MM-dd hh:mm:ss} [%p] ${selfInfo.nick}(${selfInfo.uin}) %m`;
|
loggerDefault.addContext('userInfo', userInfo);
|
||||||
log4js.configure(logConfig);
|
|
||||||
}
|
}
|
||||||
|
setLogSelfInfo({ nick: '', uin: '', uid: '' });
|
||||||
|
|
||||||
let fileLogEnabled = true;
|
let fileLogEnabled = true;
|
||||||
let consoleLogEnabled = true;
|
let consoleLogEnabled = true;
|
||||||
@ -86,7 +88,7 @@ export function enableConsoleLog(enable: boolean) {
|
|||||||
consoleLogEnabled = enable;
|
consoleLogEnabled = enable;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatMsg(msg: any[]){
|
function formatMsg(msg: any[]) {
|
||||||
let logMsg = '';
|
let logMsg = '';
|
||||||
for (const msgItem of msg) {
|
for (const msgItem of msg) {
|
||||||
// 判断是否是对象
|
// 判断是否是对象
|
||||||
@ -97,15 +99,18 @@ function formatMsg(msg: any[]){
|
|||||||
}
|
}
|
||||||
logMsg += msgItem + ' ';
|
logMsg += msgItem + ' ';
|
||||||
}
|
}
|
||||||
return '\n' + logMsg + '\n';
|
return logMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _log(level: LogLevel, ...args: any[]){
|
// eslint-disable-next-line no-control-regex
|
||||||
if (consoleLogEnabled){
|
const colorEscape = /\x1B[@-_][0-?]*[ -/]*[@-~]/g;
|
||||||
log4js.getLogger('console')[level](formatMsg(args));
|
|
||||||
|
function _log(level: LogLevel, ...args: any[]) {
|
||||||
|
if (consoleLogEnabled) {
|
||||||
|
loggerConsole[level](formatMsg(args));
|
||||||
}
|
}
|
||||||
if (fileLogEnabled){
|
if (fileLogEnabled) {
|
||||||
log4js.getLogger('file')[level](formatMsg(args));
|
loggerFile[level](formatMsg(args).replace(colorEscape, ''));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,3 +126,11 @@ export function logDebug(...args: any[]) {
|
|||||||
export function logError(...args: any[]) {
|
export function logError(...args: any[]) {
|
||||||
_log(LogLevel.ERROR, ...args);
|
_log(LogLevel.ERROR, ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function logWarn(...args: any[]) {
|
||||||
|
_log(LogLevel.WARN, ...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function logFatal(...args: any[]) {
|
||||||
|
_log(LogLevel.FATAL, ...args);
|
||||||
|
}
|
@ -24,7 +24,7 @@ export async function getVideoInfo(filePath: string) {
|
|||||||
} else {
|
} else {
|
||||||
const videoStream = metadata.streams.find((s: { codec_type: string; }) => s.codec_type === 'video');
|
const videoStream = metadata.streams.find((s: { codec_type: string; }) => s.codec_type === 'video');
|
||||||
if (videoStream) {
|
if (videoStream) {
|
||||||
console.log(`视频尺寸: ${videoStream.width}x${videoStream.height}`);
|
log(`视频尺寸: ${videoStream.width}x${videoStream.height}`);
|
||||||
} else {
|
} else {
|
||||||
return reject('未找到视频流信息。');
|
return reject('未找到视频流信息。');
|
||||||
}
|
}
|
||||||
|
25
src/index.ts
25
src/index.ts
@ -5,18 +5,19 @@ import fs from 'fs/promises';
|
|||||||
import fsSync from 'fs';
|
import fsSync from 'fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { checkVersion } from '@/common/utils/version';
|
import { checkVersion } from '@/common/utils/version';
|
||||||
import { log, logDebug, logError, LogLevel, setLogLevel } from '@/common/utils/log';
|
import { log, logDebug, logError, LogLevel, logWarn, setLogLevel } from '@/common/utils/log';
|
||||||
import { NapCatOnebot11 } from '@/onebot11/main';
|
import { NapCatOnebot11 } from '@/onebot11/main';
|
||||||
import { InitWebUi } from './webui/index';
|
import { InitWebUi } from './webui/index';
|
||||||
import { WebUiDataRuntime } from './webui/src/helper/Data';
|
import { WebUiDataRuntime } from './webui/src/helper/Data';
|
||||||
import { UpdateConfig } from './common/utils/helper';
|
import { UpdateConfig } from './common/utils/helper';
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import chalk from 'chalk';
|
||||||
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
const __dirname = dirname(__filename);
|
const __dirname = dirname(__filename);
|
||||||
|
const tagColor = chalk.cyan;
|
||||||
program
|
program
|
||||||
.option('-q, --qq <type>', 'QQ号')
|
.option('-q, --qq <type>', 'QQ号')
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
@ -31,19 +32,19 @@ checkVersion().then(async (remoteVersion: string) => {
|
|||||||
const localVersion = JSON.parse(fsSync.readFileSync(path.join(__dirname, 'package.json')).toString()).version;
|
const localVersion = JSON.parse(fsSync.readFileSync(path.join(__dirname, 'package.json')).toString()).version;
|
||||||
const localVersionList = localVersion.split('.');
|
const localVersionList = localVersion.split('.');
|
||||||
const remoteVersionList = remoteVersion.split('.');
|
const remoteVersionList = remoteVersion.split('.');
|
||||||
log('[NapCat] 当前版本:', localVersion);
|
log(tagColor('[NapCat]'), '当前版本:', localVersion);
|
||||||
for (const k of [0, 1, 2]) {
|
for (const k of [0, 1, 2]) {
|
||||||
if (parseInt(remoteVersionList[k]) > parseInt(localVersionList[k])) {
|
if (parseInt(remoteVersionList[k]) > parseInt(localVersionList[k])) {
|
||||||
console.log(`[NapCat] 检测到更新,请前往 https://github.com/NapNeko/NapCatQQ 下载 NapCatQQ V ${remoteVersion}`);
|
logWarn(tagColor('[NapCat]'), `检测到更新,请前往 https://github.com/NapNeko/NapCatQQ 下载 NapCatQQ V ${remoteVersion}`);
|
||||||
return;
|
return;
|
||||||
} else if (parseInt(remoteVersionList[k]) < parseInt(localVersionList[k])) {
|
} else if (parseInt(remoteVersionList[k]) < parseInt(localVersionList[k])) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logDebug('[NapCat] 当前已是最新版本');
|
logDebug(tagColor('[NapCat]'),'当前已是最新版本');
|
||||||
return;
|
return;
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
logError('[NapCat] 检测更新失败', e);
|
logError(tagColor('[NapCat]'),'检测更新失败', e);
|
||||||
});
|
});
|
||||||
// 不是很好待优化
|
// 不是很好待优化
|
||||||
const NapCat_OneBot11 = new NapCatOnebot11();
|
const NapCat_OneBot11 = new NapCatOnebot11();
|
||||||
@ -51,18 +52,18 @@ const NapCat_OneBot11 = new NapCatOnebot11();
|
|||||||
WebUiDataRuntime.setOB11ConfigCall(NapCat_OneBot11.SetConfig);
|
WebUiDataRuntime.setOB11ConfigCall(NapCat_OneBot11.SetConfig);
|
||||||
|
|
||||||
napCatCore.onLoginSuccess((uin, uid) => {
|
napCatCore.onLoginSuccess((uin, uid) => {
|
||||||
console.log('登录成功!');
|
log('登录成功!');
|
||||||
WebUiDataRuntime.setQQLoginStatus(true);
|
WebUiDataRuntime.setQQLoginStatus(true);
|
||||||
WebUiDataRuntime.setQQLoginUin(uin.toString());
|
WebUiDataRuntime.setQQLoginUin(uin.toString());
|
||||||
});
|
});
|
||||||
const showQRCode = async (url: string, base64: string, buffer: Buffer) => {
|
const showQRCode = async (url: string, base64: string, buffer: Buffer) => {
|
||||||
await WebUiDataRuntime.setQQLoginQrcodeURL(url);
|
await WebUiDataRuntime.setQQLoginQrcodeURL(url);
|
||||||
console.log('请扫描下面的二维码,然后在手Q上授权登录:');
|
logWarn('请扫描下面的二维码,然后在手Q上授权登录:');
|
||||||
const qrcodePath = path.join(__dirname, 'qrcode.png');
|
const qrcodePath = path.join(__dirname, 'qrcode.png');
|
||||||
qrcode.generate(url, { small: true }, (res) => {
|
qrcode.generate(url, { small: true }, (res) => {
|
||||||
console.log(`${res}\n二维码解码URL: ${url}\n如果控制台二维码无法扫码,可以复制解码url到二维码生成网站生成二维码再扫码,也可以打开下方的二维码路径图片进行扫码`);
|
logWarn(`\n${res}\n二维码解码URL: ${url}\n如果控制台二维码无法扫码,可以复制解码url到二维码生成网站生成二维码再扫码,也可以打开下方的二维码路径图片进行扫码`);
|
||||||
fs.writeFile(qrcodePath, buffer).then(() => {
|
fs.writeFile(qrcodePath, buffer).then(() => {
|
||||||
console.log('二维码已保存到', qrcodePath);
|
logWarn('二维码已保存到', qrcodePath);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -86,7 +87,7 @@ WebUiDataRuntime.setQQQuickLoginCall(async (uin: string) => {
|
|||||||
}
|
}
|
||||||
resolve({ result: true, message: '' });
|
resolve({ result: true, message: '' });
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
console.error(e);
|
logError(e);
|
||||||
resolve({ result: false, message: '快速登录发生错误' });
|
resolve({ result: false, message: '快速登录发生错误' });
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@ -104,7 +105,7 @@ if (quickLoginQQ) {
|
|||||||
logError('快速登录错误:', res.loginErrorInfo.errMsg);
|
logError('快速登录错误:', res.loginErrorInfo.errMsg);
|
||||||
}
|
}
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
console.error(e);
|
logError(e);
|
||||||
napCatCore.qrLogin(showQRCode);
|
napCatCore.qrLogin(showQRCode);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -9,6 +9,7 @@ import {
|
|||||||
} from '@/core/entities';
|
} from '@/core/entities';
|
||||||
import { dbUtil } from '@/common/utils/db';
|
import { dbUtil } from '@/common/utils/db';
|
||||||
import { NTQQFileApi, NTQQFileCacheApi } from '@/core/apis/file';
|
import { NTQQFileApi, NTQQFileCacheApi } from '@/core/apis/file';
|
||||||
|
import { logError } from '@/common/utils/log';
|
||||||
|
|
||||||
export default class CleanCache extends BaseAction<void, void> {
|
export default class CleanCache extends BaseAction<void, void> {
|
||||||
actionName = ActionName.CleanCache;
|
actionName = ActionName.CleanCache;
|
||||||
@ -62,7 +63,7 @@ export default class CleanCache extends BaseAction<void, void> {
|
|||||||
await NTQQFileCacheApi.clearChatCache(chatCacheList, cacheFileList);
|
await NTQQFileCacheApi.clearChatCache(chatCacheList, cacheFileList);
|
||||||
res();
|
res();
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error('清理缓存时发生了错误');
|
logError('清理缓存时发生了错误');
|
||||||
rej(e);
|
rej(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -3,19 +3,23 @@ import { log } from '@/common/utils/log';
|
|||||||
import { getGroup, getGroupMember, selfInfo } from '@/core/data';
|
import { getGroup, getGroupMember, selfInfo } from '@/core/data';
|
||||||
import exp from 'constants';
|
import exp from 'constants';
|
||||||
import { Group } from '@/core';
|
import { Group } from '@/core';
|
||||||
|
import chalk from 'chalk';
|
||||||
|
|
||||||
|
const spSegColor = chalk.blue;// for special segment
|
||||||
|
const spColor = chalk.cyan;// for special
|
||||||
|
|
||||||
// todo: 应该放到core去用RawMessage解析打印
|
// todo: 应该放到core去用RawMessage解析打印
|
||||||
export async function logMessage(ob11Message: OB11Message){
|
export async function logMessage(ob11Message: OB11Message) {
|
||||||
const isSelfSent = ob11Message.sender.user_id.toString() === selfInfo.uin;
|
const isSelfSent = ob11Message.sender.user_id.toString() === selfInfo.uin;
|
||||||
let prefix = '';
|
let prefix = '';
|
||||||
let group: Group | undefined;
|
let group: Group | undefined;
|
||||||
if (isSelfSent){
|
if (isSelfSent) {
|
||||||
prefix = '发送消息 ';
|
prefix = '发送消息 ';
|
||||||
if (ob11Message.message_type === 'private'){
|
if (ob11Message.message_type === 'private') {
|
||||||
prefix += '给私聊 ';
|
prefix += '给私聊 ';
|
||||||
prefix += `${ob11Message.target_id}`;
|
prefix += `${ob11Message.target_id}`;
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
prefix += '给群聊 ';
|
prefix += '给群聊 ';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -25,57 +29,60 @@ export async function logMessage(ob11Message: OB11Message){
|
|||||||
}
|
}
|
||||||
let msgChain = '';
|
let msgChain = '';
|
||||||
if (Array.isArray(ob11Message.message)) {
|
if (Array.isArray(ob11Message.message)) {
|
||||||
|
const msgParts = [];
|
||||||
for (const segment of ob11Message.message) {
|
for (const segment of ob11Message.message) {
|
||||||
if (segment.type === 'text') {
|
if (segment.type === 'text') {
|
||||||
msgChain += segment.data.text + '\n';
|
msgParts.push(segment.data.text);
|
||||||
}
|
}
|
||||||
else if (segment.type === 'at') {
|
else if (segment.type === 'at') {
|
||||||
const groupMember = await getGroupMember(ob11Message.group_id!, segment.data.qq!);
|
const groupMember = await getGroupMember(ob11Message.group_id!, segment.data.qq!);
|
||||||
msgChain += `@${groupMember?.cardName || groupMember?.nick}(${segment.data.qq}) `;
|
msgParts.push(spSegColor(`[@${groupMember?.cardName || groupMember?.nick}(${segment.data.qq})]`));
|
||||||
}
|
}
|
||||||
else if (segment.type === 'reply') {
|
else if (segment.type === 'reply') {
|
||||||
msgChain += `回复消息(id:${segment.data.id}) `;
|
msgParts.push(spSegColor(`[回复消息|id:${segment.data.id}]`));
|
||||||
}
|
}
|
||||||
else if (segment.type === 'image') {
|
else if (segment.type === 'image') {
|
||||||
msgChain += `\n[图片]${segment.data.url}\n`;
|
msgParts.push(spSegColor(`[图片|${segment.data.url}]`));
|
||||||
}
|
}
|
||||||
else if (segment.type === 'face'){
|
else if (segment.type === 'face') {
|
||||||
msgChain += `[表情](id:${segment.data.id}) `;
|
msgParts.push(spSegColor(`[表情|id:${segment.data.id}]`));
|
||||||
}
|
}
|
||||||
else if (segment.type === 'mface'){
|
else if (segment.type === 'mface') {
|
||||||
msgChain += `\n[商城表情]${segment.data.url}\n`;
|
// @ts-expect-error 商城表情 url
|
||||||
|
msgParts.push(spSegColor(`[商城表情|${segment.data.url}]`));
|
||||||
}
|
}
|
||||||
else if (segment.type === 'record') {
|
else if (segment.type === 'record') {
|
||||||
msgChain += `[语音]${segment.data.file} `;
|
msgParts.push(spSegColor(`[语音|${segment.data.file}]`));
|
||||||
}
|
}
|
||||||
else if (segment.type === 'file') {
|
else if (segment.type === 'file') {
|
||||||
msgChain += `[文件]${segment.data.file} `;
|
msgParts.push(spSegColor(`[文件|${segment.data.file}]`));
|
||||||
}
|
}
|
||||||
else if (segment.type === 'json') {
|
else if (segment.type === 'json') {
|
||||||
msgChain += `\n[json]${JSON.stringify(segment.data)}\n`;
|
msgParts.push(spSegColor(`[json|${JSON.stringify(segment.data)}]`));
|
||||||
}
|
}
|
||||||
else if (segment.type === 'markdown') {
|
else if (segment.type === 'markdown') {
|
||||||
msgChain += `\n[json]${segment.data.content}\n`;
|
msgParts.push(spSegColor(`[markdown|${segment.data.content}]`));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
msgChain += `${JSON.stringify(segment)}`;
|
msgParts.push(spSegColor(`[未实现|${JSON.stringify(segment)}]`));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
msgChain = msgParts.join(' ');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
msgChain = ob11Message.message;
|
msgChain = ob11Message.message;
|
||||||
}
|
}
|
||||||
let msgString = `${prefix}${ob11Message.sender.nickname}(${ob11Message.sender.user_id}): ${msgChain}`;
|
let msgString = `${prefix}${ob11Message.sender.nickname}(${ob11Message.sender.user_id}): ${msgChain}`;
|
||||||
if (isSelfSent){
|
if (isSelfSent) {
|
||||||
msgString = `${prefix}: ${msgChain}`;
|
msgString = `${prefix}: ${msgChain}`;
|
||||||
}
|
}
|
||||||
log(msgString);
|
log(msgString);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function logNotice(ob11Notice: any){
|
export async function logNotice(ob11Notice: any) {
|
||||||
log('[notice]', ob11Notice);
|
log(spColor('[Notice]'), ob11Notice);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function logRequest(ob11Request: any){
|
export async function logRequest(ob11Request: any) {
|
||||||
log('[request]', ob11Request);
|
log(spColor('[Request]'), ob11Request);
|
||||||
}
|
}
|
||||||
|
@ -112,7 +112,7 @@ export class NapCatOnebot11 {
|
|||||||
try {
|
try {
|
||||||
// 生产环境会自己去掉
|
// 生产环境会自己去掉
|
||||||
if (import.meta.env.MODE == 'development') {
|
if (import.meta.env.MODE == 'development') {
|
||||||
console.log(buf2hex(Buffer.from(protobufData)));
|
logDebug(buf2hex(Buffer.from(protobufData)));
|
||||||
}
|
}
|
||||||
const sysMsg = SysData.fromBinary(Buffer.from(protobufData));
|
const sysMsg = SysData.fromBinary(Buffer.from(protobufData));
|
||||||
const peeruin = sysMsg.header[0].peerNumber;
|
const peeruin = sysMsg.header[0].peerNumber;
|
||||||
@ -258,7 +258,7 @@ export class NapCatOnebot11 {
|
|||||||
for (const member of members.values()) {
|
for (const member of members.values()) {
|
||||||
//console.log(member?.isDelete, role, isPrivilege);
|
//console.log(member?.isDelete, role, isPrivilege);
|
||||||
if (member?.isDelete && !isPrivilege /*&& selfInfo.uin !== member.uin*/) {
|
if (member?.isDelete && !isPrivilege /*&& selfInfo.uin !== member.uin*/) {
|
||||||
console.log('[群聊] 群组 ', groupCode, ' 成员' + member.uin + '退出');
|
log('[群聊] 群组 ', groupCode, ' 成员' + member.uin + '退出');
|
||||||
const groupDecreaseEvent = new OB11GroupDecreaseEvent(parseInt(groupCode), parseInt(member.uin), 0, 'leave');// 不知道怎么出去的
|
const groupDecreaseEvent = new OB11GroupDecreaseEvent(parseInt(groupCode), parseInt(member.uin), 0, 'leave');// 不知道怎么出去的
|
||||||
postOB11Event(groupDecreaseEvent, true);
|
postOB11Event(groupDecreaseEvent, true);
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import { WebUiConfig } from './src/helper/config';
|
|||||||
const app = express();
|
const app = express();
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import { log } from '@/common/utils/log';
|
||||||
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
@ -32,8 +33,8 @@ export async function InitWebUi() {
|
|||||||
//挂载API接口
|
//挂载API接口
|
||||||
app.use('/api', ALLRouter);
|
app.use('/api', ALLRouter);
|
||||||
app.listen(config.port, async () => {
|
app.listen(config.port, async () => {
|
||||||
console.log(`[NapCat] [WebUi] Current WebUi is running at IP:${config.port}`);
|
log(`[NapCat] [WebUi] Current WebUi is running at IP:${config.port}`);
|
||||||
console.log(`[NapCat] [WebUi] Login Token is ${config.token}`);
|
log(`[NapCat] [WebUi] Login Token is ${config.token}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
@ -3,6 +3,7 @@ import { resolve } from 'node:path';
|
|||||||
import * as net from 'node:net';
|
import * as net from 'node:net';
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
|
import { logError } from '@/common/utils/log';
|
||||||
|
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
@ -78,7 +79,7 @@ class WebUiConfigWrapper {
|
|||||||
this.WebUiConfigData = parsedConfig;
|
this.WebUiConfigData = parsedConfig;
|
||||||
return this.WebUiConfigData;
|
return this.WebUiConfigData;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('读取配置文件失败', e);
|
logError('读取配置文件失败', e);
|
||||||
}
|
}
|
||||||
return {} as WebUiConfigType; // 理论上这行代码到不了,为了保持函数完整性而保留
|
return {} as WebUiConfigType; // 理论上这行代码到不了,为了保持函数完整性而保留
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user