From fa07dfb720ee6c3ebf765d6b791289bbc280cd72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Fri, 9 Aug 2024 21:54:14 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E6=B3=A8=E5=85=A5OneBot=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/action/BaseAction.ts | 8 +- src/onebot/action/index.ts | 159 ++++++++++++++++---------------- 2 files changed, 85 insertions(+), 82 deletions(-) diff --git a/src/onebot/action/BaseAction.ts b/src/onebot/action/BaseAction.ts index c2d33551..03e23bc1 100644 --- a/src/onebot/action/BaseAction.ts +++ b/src/onebot/action/BaseAction.ts @@ -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 { actionName: ActionName = ActionName.Unknown; CoreContext: NapCatCore; private validate: undefined | ValidateFunction = 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 { if (this.PayloadSchema) { diff --git a/src/onebot/action/index.ts b/src/onebot/action/index.ts index 27940f64..0d80862f 100644 --- a/src/onebot/action/index.ts +++ b/src/onebot/action/index.ts @@ -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>(); for (const action of actionHandlers) { From c271a4b2cb9452623ac90f1ac82c4fa2269a82b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Fri, 9 Aug 2024 22:14:45 +0800 Subject: [PATCH 2/4] fix --- src/liteloader/napcat.ts | 2 +- src/onebot/main.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/liteloader/napcat.ts b/src/liteloader/napcat.ts index 98e86593..cb890212 100644 --- a/src/liteloader/napcat.ts +++ b/src/liteloader/napcat.ts @@ -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 { diff --git a/src/onebot/main.ts b/src/onebot/main.ts index b5fb45c4..15e64197 100644 --- a/src/onebot/main.ts +++ b/src/onebot/main.ts @@ -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); } } From 3f6f1dcd78d75ae3f22df9547aee94bf7d55ab00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Fri, 9 Aug 2024 22:29:01 +0800 Subject: [PATCH 3/4] chore: config --- src/common/utils/ConfigBase.ts | 30 ++++++----- src/onebot/helper/config.ts | 99 +++++++++++----------------------- 2 files changed, 48 insertions(+), 81 deletions(-) diff --git a/src/common/utils/ConfigBase.ts b/src/common/utils/ConfigBase.ts index 7745806b..3a35d9ef 100644 --- a/src/common/utils/ConfigBase.ts +++ b/src/common/utils/ConfigBase.ts @@ -5,10 +5,12 @@ export class ConfigBase { 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 { // 尝试加载默认配置 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 { 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 { } 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); } diff --git a/src/onebot/helper/config.ts b/src/onebot/helper/config.ts index e2275163..581f87fd 100644 --- a/src/onebot/helper/config.ts +++ b/src/onebot/helper/config.ts @@ -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 -// }, -// 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 + } +} export class OB11Config extends ConfigBase { - 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 - }; - + name = 'onebot11'; protected getKeys(): string[] | null { return null; } From ee85f3e824926e353b1eeb7e3032b0c2246d9075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Fri, 9 Aug 2024 22:29:51 +0800 Subject: [PATCH 4/4] chore: defaultConfig --- src/external/napcat.json | 6 ++++++ src/external/onebot11.json | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/external/napcat.json create mode 100644 src/external/onebot11.json diff --git a/src/external/napcat.json b/src/external/napcat.json new file mode 100644 index 00000000..9412e77a --- /dev/null +++ b/src/external/napcat.json @@ -0,0 +1,6 @@ +{ + "fileLog": true, + "consoleLog": true, + "fileLogLevel": "debug", + "consoleLogLevel": "info" +} \ No newline at end of file diff --git a/src/external/onebot11.json b/src/external/onebot11.json new file mode 100644 index 00000000..eadb0142 --- /dev/null +++ b/src/external/onebot11.json @@ -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": "" +} \ No newline at end of file