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

View File

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

View File

@ -75,89 +75,90 @@ import { FetchCustomFace } from './extends/FetchCustomFace';
import GoCQHTTPUploadPrivateFile from './go-cqhttp/UploadPrivareFile';
import { FetchEmojioLike } from './extends/FetchEmojioLike';
import { NapCatCore } from '@/core';
import { NapCatOneBot11Adapter } from '../main';
export function createActionMap(context: NapCatCore) {
export function createActionMap(onebotContext: NapCatOneBot11Adapter, coreContext: NapCatCore) {
const actionHandlers = [
new FetchEmojioLike(context),
new GetFile(context),
new SetSelfProfile(context),
new shareGroupEx(context),
new sharePeer(context),
new CreateCollection(context),
new SetLongNick(context),
new ForwardFriendSingleMsg(context),
new ForwardGroupSingleMsg(context),
new MarkGroupMsgAsRead(context),
new MarkPrivateMsgAsRead(context),
new SetQQAvatar(context),
new TranslateEnWordToZn(context),
new GetGroupFileCount(context),
new GetGroupFileList(context),
new SetGroupFileFolder(context),
new DelGroupFile(context),
new DelGroupFileFolder(context),
new FetchEmojioLike(onebotContext,coreContext),
new GetFile(onebotContext,coreContext),
new SetSelfProfile(onebotContext,coreContext),
new shareGroupEx(onebotContext,coreContext),
new sharePeer(onebotContext,coreContext),
new CreateCollection(onebotContext,coreContext),
new SetLongNick(onebotContext,coreContext),
new ForwardFriendSingleMsg(onebotContext,coreContext),
new ForwardGroupSingleMsg(onebotContext,coreContext),
new MarkGroupMsgAsRead(onebotContext,coreContext),
new MarkPrivateMsgAsRead(onebotContext,coreContext),
new SetQQAvatar(onebotContext,coreContext),
new TranslateEnWordToZn(onebotContext,coreContext),
new GetGroupFileCount(onebotContext,coreContext),
new GetGroupFileList(onebotContext,coreContext),
new SetGroupFileFolder(onebotContext,coreContext),
new DelGroupFile(onebotContext,coreContext),
new DelGroupFileFolder(onebotContext,coreContext),
// onebot11
new SendLike(context),
new GetMsg(context),
new GetLoginInfo(context),
new GetFriendList(context),
new GetGroupList(context),
new GetGroupInfo(context),
new GetGroupMemberList(context),
new GetGroupMemberInfo(context),
new SendGroupMsg(context),
new SendPrivateMsg(context),
new SendMsg(context),
new DeleteMsg(context),
new SetGroupAddRequest(context),
new SetFriendAddRequest(context),
new SetGroupLeave(context),
new GetVersionInfo(context),
new CanSendRecord(context),
new CanSendImage(context),
new GetStatus(context),
new SetGroupWholeBan(context),
new SetGroupBan(context),
new SetGroupKick(context),
new SetGroupAdmin(context),
new SetGroupName(context),
new SetGroupCard(context),
new GetImage(context),
new GetRecord(context),
new SetMsgEmojiLike(context),
new GetCookies(context),
new SetOnlineStatus(context),
new GetRobotUinRange(context),
new GetFriendWithCategory(context),
new SendLike(onebotContext,coreContext),
new GetMsg(onebotContext,coreContext),
new GetLoginInfo(onebotContext,coreContext),
new GetFriendList(onebotContext,coreContext),
new GetGroupList(onebotContext,coreContext),
new GetGroupInfo(onebotContext,coreContext),
new GetGroupMemberList(onebotContext,coreContext),
new GetGroupMemberInfo(onebotContext,coreContext),
new SendGroupMsg(onebotContext,coreContext),
new SendPrivateMsg(onebotContext,coreContext),
new SendMsg(onebotContext,coreContext),
new DeleteMsg(onebotContext,coreContext),
new SetGroupAddRequest(onebotContext,coreContext),
new SetFriendAddRequest(onebotContext,coreContext),
new SetGroupLeave(onebotContext,coreContext),
new GetVersionInfo(onebotContext,coreContext),
new CanSendRecord(onebotContext,coreContext),
new CanSendImage(onebotContext,coreContext),
new GetStatus(onebotContext,coreContext),
new SetGroupWholeBan(onebotContext,coreContext),
new SetGroupBan(onebotContext,coreContext),
new SetGroupKick(onebotContext,coreContext),
new SetGroupAdmin(onebotContext,coreContext),
new SetGroupName(onebotContext,coreContext),
new SetGroupCard(onebotContext,coreContext),
new GetImage(onebotContext,coreContext),
new GetRecord(onebotContext,coreContext),
new SetMsgEmojiLike(onebotContext,coreContext),
new GetCookies(onebotContext,coreContext),
new SetOnlineStatus(onebotContext,coreContext),
new GetRobotUinRange(onebotContext,coreContext),
new GetFriendWithCategory(onebotContext,coreContext),
//以下为go-cqhttp api
new GetOnlineClient(context),
new OCRImage(context),
new IOCRImage(context),
new GetGroupHonorInfo(context),
new SendGroupNotice(context),
new GetGroupNotice(context),
new GetGroupEssence(context),
new GoCQHTTPSendForwardMsg(context),
new GoCQHTTPSendGroupForwardMsg(context),
new GoCQHTTPSendPrivateForwardMsg(context),
new GoCQHTTPGetStrangerInfo(context),
new GoCQHTTPDownloadFile(context),
new GetGuildList(context),
new GoCQHTTPMarkMsgAsRead(context),
new GoCQHTTPUploadGroupFile(context),
new GoCQHTTPGetGroupMsgHistory(context),
new GoCQHTTPGetForwardMsgAction(context),
new GetFriendMsgHistory(context),
new GoCQHTTPHandleQuickAction(context),
new GetGroupSystemMsg(context),
new DelEssenceMsg(context),
new SetEssenceMsg(context),
new GetRecentContact(context),
new MarkAllMsgAsRead(context),
new GetProfileLike(context),
new SetGroupHeader(context),
new FetchCustomFace(context),
new GoCQHTTPUploadPrivateFile(context)
new GetOnlineClient(onebotContext,coreContext),
new OCRImage(onebotContext,coreContext),
new IOCRImage(onebotContext,coreContext),
new GetGroupHonorInfo(onebotContext,coreContext),
new SendGroupNotice(onebotContext,coreContext),
new GetGroupNotice(onebotContext,coreContext),
new GetGroupEssence(onebotContext,coreContext),
new GoCQHTTPSendForwardMsg(onebotContext,coreContext),
new GoCQHTTPSendGroupForwardMsg(onebotContext,coreContext),
new GoCQHTTPSendPrivateForwardMsg(onebotContext,coreContext),
new GoCQHTTPGetStrangerInfo(onebotContext,coreContext),
new GoCQHTTPDownloadFile(onebotContext,coreContext),
new GetGuildList(onebotContext,coreContext),
new GoCQHTTPMarkMsgAsRead(onebotContext,coreContext),
new GoCQHTTPUploadGroupFile(onebotContext,coreContext),
new GoCQHTTPGetGroupMsgHistory(onebotContext,coreContext),
new GoCQHTTPGetForwardMsgAction(onebotContext,coreContext),
new GetFriendMsgHistory(onebotContext,coreContext),
new GoCQHTTPHandleQuickAction(onebotContext,coreContext),
new GetGroupSystemMsg(onebotContext,coreContext),
new DelEssenceMsg(onebotContext,coreContext),
new SetEssenceMsg(onebotContext,coreContext),
new GetRecentContact(onebotContext,coreContext),
new MarkAllMsgAsRead(onebotContext,coreContext),
new GetProfileLike(onebotContext,coreContext),
new SetGroupHeader(onebotContext,coreContext),
new FetchCustomFace(onebotContext,coreContext),
new GoCQHTTPUploadPrivateFile(onebotContext,coreContext)
];
const actionMap = new Map<string, BaseAction<any, any>>();
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';
// export interface OB11Config {
// http: {
// enable: boolean;
// host: string;
// port: number;
// secret: string;
// enableHeart: boolean;
// enablePost: boolean;
// postUrls: string[];
// };
// ws: {
// enable: boolean;
// host: string;
// port: number;
// };
// reverseWs: {
// enable: boolean;
// urls: string[];
// };
export interface OB11Config {
http: {
enable: boolean;
host: string;
port: number;
secret: string;
enableHeart: boolean;
enablePost: boolean;
postUrls: string[];
};
ws: {
enable: boolean;
host: string;
port: number;
};
reverseWs: {
enable: boolean;
urls: string[];
};
// debug: boolean;
// heartInterval: number;
// messagePostFormat: 'array' | 'string';
// enableLocalFile2Url: boolean;
// musicSignUrl: string;
// reportSelfMessage: boolean;
// token: string;
// GroupLocalTime: {
// Record: boolean,
// RecordList: Array<string>
// },
// read(): OB11Config;
// save(config: OB11Config): void;
// }
debug: boolean;
heartInterval: number;
messagePostFormat: 'array' | 'string';
enableLocalFile2Url: boolean;
musicSignUrl: string;
reportSelfMessage: boolean;
token: string;
GroupLocalTime: {
Record: boolean,
RecordList: Array<string>
}
}
export class OB11Config extends ConfigBase<OB11Config> {
name: string = '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>
};
name = 'onebot11';
protected getKeys(): string[] | null {
return null;
}

View File

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