Merge remote-tracking branch 'origin/v2' into v2

This commit is contained in:
Wesley F. Young 2024-08-09 22:37:21 +08:00
commit 7e4ebd330c
8 changed files with 176 additions and 165 deletions

View File

@ -5,10 +5,12 @@ export class ConfigBase<T> {
public name: string = 'default_config'; public name: string = 'default_config';
private pathName: string | null = null; // 本次读取的文件路径 private pathName: string | null = null; // 本次读取的文件路径
coreContext: NapCatCore; coreContext: NapCatCore;
configPath:string; configPath: string;
constructor(coreContext: NapCatCore,configPath:string) { config: { [key: string]: any } = {};
constructor(coreContext: NapCatCore, configPath: string) {
this.coreContext = coreContext; this.coreContext = coreContext;
this.configPath = configPath; this.configPath = configPath;
this.read();
} }
protected getKeys(): string[] | null { protected getKeys(): string[] | null {
@ -32,6 +34,9 @@ export class ConfigBase<T> {
// 尝试加载默认配置 // 尝试加载默认配置
return this.read_from_file('', true); return this.read_from_file('', true);
} }
getConfig(): T {
return this.config as T;
}
read_from_file(pathName: string, createIfNotExist: boolean) { read_from_file(pathName: string, createIfNotExist: boolean) {
const logger = this.coreContext.context.logger; const logger = this.coreContext.context.logger;
const configPath = this.getConfigPath(pathName); const configPath = this.getConfigPath(pathName);
@ -39,35 +44,32 @@ export class ConfigBase<T> {
if (!createIfNotExist) return null; if (!createIfNotExist) return null;
this.pathName = pathName; // 记录有效的设置文件 this.pathName = pathName; // 记录有效的设置文件
try { try {
fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2)); fs.writeFileSync(configPath, JSON.stringify(this.config, this.getKeys(), 2));
logger.log(`配置文件${configPath}已创建\n如果修改此文件后需要重启 NapCat 生效`); logger.log(`配置文件${configPath}已创建\n如果修改此文件后需要重启 NapCat 生效`);
} }
catch (e: any) { catch (e: any) {
logger.logError(`创建配置文件 ${configPath} 时发生错误:`, e.message); logger.logError(`创建配置文件 ${configPath} 时发生错误:`, e.message);
} }
return this; return this.config;
} }
try { try {
const data = JSON.parse(fs.readFileSync(configPath, 'utf-8')); this.config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
logger.logDebug(`配置文件${configPath}已加载`, data); logger.logDebug(`配置文件${configPath}已加载`, this.config);
Object.assign(this, data);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error this.save(); // 保存一次,让新版本的字段写入
this.save(this); // 保存一次,让新版本的字段写入 return this.config;
return this;
} catch (e: any) { } catch (e: any) {
if (e instanceof SyntaxError) { if (e instanceof SyntaxError) {
logger.logError(`配置文件 ${configPath} 格式错误,请检查配置文件:`, e.message); logger.logError(`配置文件 ${configPath} 格式错误,请检查配置文件:`, e.message);
} else { } else {
logger.logError(`读取配置文件 ${configPath} 时发生错误:`, e.message); logger.logError(`读取配置文件 ${configPath} 时发生错误:`, e.message);
} }
return this; return {};
} }
} }
save(config: T, overwrite: boolean = false) { save(configData: T = this.config as T, overwrite: boolean = false) {
Object.assign(this, config);
const logger = this.coreContext.context.logger; const logger = this.coreContext.context.logger;
const selfInfo = this.coreContext.selfInfo; const selfInfo = this.coreContext.selfInfo;
if (overwrite) { if (overwrite) {
@ -76,7 +78,7 @@ export class ConfigBase<T> {
} }
const configPath = this.getConfigPath(this.pathName); const configPath = this.getConfigPath(this.pathName);
try { try {
fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2)); fs.writeFileSync(configPath, JSON.stringify(configData, this.getKeys(), 2));
} catch (e: any) { } catch (e: any) {
logger.logError(`保存配置文件 ${configPath} 时发生错误:`, e.message); logger.logError(`保存配置文件 ${configPath} 时发生错误:`, e.message);
} }

6
src/external/napcat.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"fileLog": true,
"consoleLog": true,
"fileLogLevel": "debug",
"consoleLogLevel": "info"
}

31
src/external/onebot11.json vendored Normal file
View File

@ -0,0 +1,31 @@
{
"http": {
"enable": false,
"host": "",
"port": 3000,
"secret": "",
"enableHeart": false,
"enablePost": false,
"postUrls": []
},
"ws": {
"enable": false,
"host": "",
"port": 3001
},
"reverseWs": {
"enable": false,
"urls": []
},
"GroupLocalTime": {
"Record": false,
"RecordList": []
},
"debug": false,
"heartInterval": 30000,
"messagePostFormat": "array",
"enableLocalFile2Url": true,
"musicSignUrl": "",
"reportSelfMessage": false,
"token": ""
}

View File

@ -39,7 +39,7 @@ export async function NCoreInitLiteLoader(session: NodeIQQNTWrapperSession, logi
//启动WebUi //启动WebUi
//初始化LLNC的Onebot实现 //初始化LLNC的Onebot实现
new NapCatOneBot11Adapter(loaderObject.core, loaderObject.context); new NapCatOneBot11Adapter(loaderObject.core, loaderObject.context,pathWrapper);
} }
export class NapCatLiteLoader { export class NapCatLiteLoader {

View File

@ -3,15 +3,17 @@ import { OB11Response } from './OB11Response';
import { OB11Return } from '@/onebot/types'; import { OB11Return } from '@/onebot/types';
import Ajv, { ErrorObject, ValidateFunction } from 'ajv'; import Ajv, { ErrorObject, ValidateFunction } from 'ajv';
import { NapCatCore } from '@/core'; import { NapCatCore } from '@/core';
import { NapCatOneBot11Adapter } from '../main';
class BaseAction<PayloadType, ReturnDataType> { class BaseAction<PayloadType, ReturnDataType> {
actionName: ActionName = ActionName.Unknown; actionName: ActionName = ActionName.Unknown;
CoreContext: NapCatCore; CoreContext: NapCatCore;
private validate: undefined | ValidateFunction<any> = undefined; private validate: undefined | ValidateFunction<any> = undefined;
PayloadSchema: any = undefined; PayloadSchema: any = undefined;
constructor(context: NapCatCore) { OneBotContext: NapCatOneBot11Adapter;
//注入上下文 constructor(onebotContext:NapCatOneBot11Adapter,coreContext: NapCatCore) {
this.CoreContext = context; this.OneBotContext = onebotContext;
this.CoreContext = coreContext;
} }
protected async check(payload: PayloadType): Promise<BaseCheckResult> { protected async check(payload: PayloadType): Promise<BaseCheckResult> {
if (this.PayloadSchema) { if (this.PayloadSchema) {

View File

@ -75,89 +75,90 @@ import { FetchCustomFace } from './extends/FetchCustomFace';
import GoCQHTTPUploadPrivateFile from './go-cqhttp/UploadPrivareFile'; import GoCQHTTPUploadPrivateFile from './go-cqhttp/UploadPrivareFile';
import { FetchEmojioLike } from './extends/FetchEmojioLike'; import { FetchEmojioLike } from './extends/FetchEmojioLike';
import { NapCatCore } from '@/core'; import { NapCatCore } from '@/core';
import { NapCatOneBot11Adapter } from '../main';
export function createActionMap(context: NapCatCore) { export function createActionMap(onebotContext: NapCatOneBot11Adapter, coreContext: NapCatCore) {
const actionHandlers = [ const actionHandlers = [
new FetchEmojioLike(context), new FetchEmojioLike(onebotContext,coreContext),
new GetFile(context), new GetFile(onebotContext,coreContext),
new SetSelfProfile(context), new SetSelfProfile(onebotContext,coreContext),
new shareGroupEx(context), new shareGroupEx(onebotContext,coreContext),
new sharePeer(context), new sharePeer(onebotContext,coreContext),
new CreateCollection(context), new CreateCollection(onebotContext,coreContext),
new SetLongNick(context), new SetLongNick(onebotContext,coreContext),
new ForwardFriendSingleMsg(context), new ForwardFriendSingleMsg(onebotContext,coreContext),
new ForwardGroupSingleMsg(context), new ForwardGroupSingleMsg(onebotContext,coreContext),
new MarkGroupMsgAsRead(context), new MarkGroupMsgAsRead(onebotContext,coreContext),
new MarkPrivateMsgAsRead(context), new MarkPrivateMsgAsRead(onebotContext,coreContext),
new SetQQAvatar(context), new SetQQAvatar(onebotContext,coreContext),
new TranslateEnWordToZn(context), new TranslateEnWordToZn(onebotContext,coreContext),
new GetGroupFileCount(context), new GetGroupFileCount(onebotContext,coreContext),
new GetGroupFileList(context), new GetGroupFileList(onebotContext,coreContext),
new SetGroupFileFolder(context), new SetGroupFileFolder(onebotContext,coreContext),
new DelGroupFile(context), new DelGroupFile(onebotContext,coreContext),
new DelGroupFileFolder(context), new DelGroupFileFolder(onebotContext,coreContext),
// onebot11 // onebot11
new SendLike(context), new SendLike(onebotContext,coreContext),
new GetMsg(context), new GetMsg(onebotContext,coreContext),
new GetLoginInfo(context), new GetLoginInfo(onebotContext,coreContext),
new GetFriendList(context), new GetFriendList(onebotContext,coreContext),
new GetGroupList(context), new GetGroupList(onebotContext,coreContext),
new GetGroupInfo(context), new GetGroupInfo(onebotContext,coreContext),
new GetGroupMemberList(context), new GetGroupMemberList(onebotContext,coreContext),
new GetGroupMemberInfo(context), new GetGroupMemberInfo(onebotContext,coreContext),
new SendGroupMsg(context), new SendGroupMsg(onebotContext,coreContext),
new SendPrivateMsg(context), new SendPrivateMsg(onebotContext,coreContext),
new SendMsg(context), new SendMsg(onebotContext,coreContext),
new DeleteMsg(context), new DeleteMsg(onebotContext,coreContext),
new SetGroupAddRequest(context), new SetGroupAddRequest(onebotContext,coreContext),
new SetFriendAddRequest(context), new SetFriendAddRequest(onebotContext,coreContext),
new SetGroupLeave(context), new SetGroupLeave(onebotContext,coreContext),
new GetVersionInfo(context), new GetVersionInfo(onebotContext,coreContext),
new CanSendRecord(context), new CanSendRecord(onebotContext,coreContext),
new CanSendImage(context), new CanSendImage(onebotContext,coreContext),
new GetStatus(context), new GetStatus(onebotContext,coreContext),
new SetGroupWholeBan(context), new SetGroupWholeBan(onebotContext,coreContext),
new SetGroupBan(context), new SetGroupBan(onebotContext,coreContext),
new SetGroupKick(context), new SetGroupKick(onebotContext,coreContext),
new SetGroupAdmin(context), new SetGroupAdmin(onebotContext,coreContext),
new SetGroupName(context), new SetGroupName(onebotContext,coreContext),
new SetGroupCard(context), new SetGroupCard(onebotContext,coreContext),
new GetImage(context), new GetImage(onebotContext,coreContext),
new GetRecord(context), new GetRecord(onebotContext,coreContext),
new SetMsgEmojiLike(context), new SetMsgEmojiLike(onebotContext,coreContext),
new GetCookies(context), new GetCookies(onebotContext,coreContext),
new SetOnlineStatus(context), new SetOnlineStatus(onebotContext,coreContext),
new GetRobotUinRange(context), new GetRobotUinRange(onebotContext,coreContext),
new GetFriendWithCategory(context), new GetFriendWithCategory(onebotContext,coreContext),
//以下为go-cqhttp api //以下为go-cqhttp api
new GetOnlineClient(context), new GetOnlineClient(onebotContext,coreContext),
new OCRImage(context), new OCRImage(onebotContext,coreContext),
new IOCRImage(context), new IOCRImage(onebotContext,coreContext),
new GetGroupHonorInfo(context), new GetGroupHonorInfo(onebotContext,coreContext),
new SendGroupNotice(context), new SendGroupNotice(onebotContext,coreContext),
new GetGroupNotice(context), new GetGroupNotice(onebotContext,coreContext),
new GetGroupEssence(context), new GetGroupEssence(onebotContext,coreContext),
new GoCQHTTPSendForwardMsg(context), new GoCQHTTPSendForwardMsg(onebotContext,coreContext),
new GoCQHTTPSendGroupForwardMsg(context), new GoCQHTTPSendGroupForwardMsg(onebotContext,coreContext),
new GoCQHTTPSendPrivateForwardMsg(context), new GoCQHTTPSendPrivateForwardMsg(onebotContext,coreContext),
new GoCQHTTPGetStrangerInfo(context), new GoCQHTTPGetStrangerInfo(onebotContext,coreContext),
new GoCQHTTPDownloadFile(context), new GoCQHTTPDownloadFile(onebotContext,coreContext),
new GetGuildList(context), new GetGuildList(onebotContext,coreContext),
new GoCQHTTPMarkMsgAsRead(context), new GoCQHTTPMarkMsgAsRead(onebotContext,coreContext),
new GoCQHTTPUploadGroupFile(context), new GoCQHTTPUploadGroupFile(onebotContext,coreContext),
new GoCQHTTPGetGroupMsgHistory(context), new GoCQHTTPGetGroupMsgHistory(onebotContext,coreContext),
new GoCQHTTPGetForwardMsgAction(context), new GoCQHTTPGetForwardMsgAction(onebotContext,coreContext),
new GetFriendMsgHistory(context), new GetFriendMsgHistory(onebotContext,coreContext),
new GoCQHTTPHandleQuickAction(context), new GoCQHTTPHandleQuickAction(onebotContext,coreContext),
new GetGroupSystemMsg(context), new GetGroupSystemMsg(onebotContext,coreContext),
new DelEssenceMsg(context), new DelEssenceMsg(onebotContext,coreContext),
new SetEssenceMsg(context), new SetEssenceMsg(onebotContext,coreContext),
new GetRecentContact(context), new GetRecentContact(onebotContext,coreContext),
new MarkAllMsgAsRead(context), new MarkAllMsgAsRead(onebotContext,coreContext),
new GetProfileLike(context), new GetProfileLike(onebotContext,coreContext),
new SetGroupHeader(context), new SetGroupHeader(onebotContext,coreContext),
new FetchCustomFace(context), new FetchCustomFace(onebotContext,coreContext),
new GoCQHTTPUploadPrivateFile(context) new GoCQHTTPUploadPrivateFile(onebotContext,coreContext)
]; ];
const actionMap = new Map<string, BaseAction<any, any>>(); const actionMap = new Map<string, BaseAction<any, any>>();
for (const action of actionHandlers) { for (const action of actionHandlers) {

View File

@ -1,75 +1,40 @@
import fs from 'node:fs';
import path from 'node:path';
import { ConfigBase } from '@/common/utils/ConfigBase'; import { ConfigBase } from '@/common/utils/ConfigBase';
// export interface OB11Config { export interface OB11Config {
// http: { http: {
// enable: boolean; enable: boolean;
// host: string; host: string;
// port: number; port: number;
// secret: string; secret: string;
// enableHeart: boolean; enableHeart: boolean;
// enablePost: boolean; enablePost: boolean;
// postUrls: string[]; postUrls: string[];
// }; };
// ws: { ws: {
// enable: boolean; enable: boolean;
// host: string; host: string;
// port: number; port: number;
// }; };
// reverseWs: { reverseWs: {
// enable: boolean; enable: boolean;
// urls: string[]; urls: string[];
// }; };
// debug: boolean; debug: boolean;
// heartInterval: number; heartInterval: number;
// messagePostFormat: 'array' | 'string'; messagePostFormat: 'array' | 'string';
// enableLocalFile2Url: boolean; enableLocalFile2Url: boolean;
// musicSignUrl: string; musicSignUrl: string;
// reportSelfMessage: boolean; reportSelfMessage: boolean;
// token: string; token: string;
// GroupLocalTime: { GroupLocalTime: {
// Record: boolean, Record: boolean,
// RecordList: Array<string> RecordList: Array<string>
// }, }
// read(): OB11Config; }
// save(config: OB11Config): void;
// }
export class OB11Config extends ConfigBase<OB11Config> { export class OB11Config extends ConfigBase<OB11Config> {
name: string = 'onebot11'; name = 'onebot11';
http = {
enable: false,
host: '',
port: 3000,
secret: '',
enableHeart: false,
enablePost: false,
postUrls: [],
};
ws = {
enable: false,
host: '',
port: 3001,
};
reverseWs = {
enable: false,
urls: [],
};
debug = false;
heartInterval = 30000;
messagePostFormat: 'array' | 'string' = 'array';
enableLocalFile2Url = true;
musicSignUrl = '';
reportSelfMessage = false;
token = '';
GroupLocalTime = {
Record: false,
RecordList: [] as Array<string>
};
protected getKeys(): string[] | null { protected getKeys(): string[] | null {
return null; return null;
} }

View File

@ -1,12 +1,16 @@
import { NapCatCore, InstanceContext } from "@/core"; import { NapCatCore, InstanceContext } from "@/core";
import { OB11Config } from "./helper/config";
import { NapCatPathWrapper } from "@/common/framework/napcat";
//OneBot实现类 //OneBot实现类
export class NapCatOneBot11Adapter { export class NapCatOneBot11Adapter {
readonly core: NapCatCore; readonly core: NapCatCore;
readonly context: InstanceContext; readonly context: InstanceContext;
config: OB11Config;
constructor(core: NapCatCore, context: InstanceContext) { constructor(core: NapCatCore, context: InstanceContext, pathWrapper: NapCatPathWrapper) {
this.core = core; this.core = core;
this.context = context; this.context = context;
this.config = new OB11Config(core, pathWrapper.configPath);
} }
} }