From ff29b6239839b3767711cac2310789fdc808a284 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 16:22:35 +0800 Subject: [PATCH] chore: OBAPI --- src/onebot/action/group/SetGroupKick.ts | 2 - src/onebot/action/group/SetGroupName.ts | 2 +- src/onebot/action/group/SetGroupWholeBan.ts | 3 +- src/onebot/action/msg/DeleteMsg.ts | 6 +- src/onebot/action/msg/ForwardSingleMsg.ts | 5 +- src/onebot/action/msg/GetMsg.ts | 4 +- src/onebot/action/msg/MarkMsgAsRead.ts | 3 + .../action/msg/SendMsg/check-send-message.ts | 2 +- src/onebot/action/msg/SetMsgEmojiLike.ts | 1 + src/onebot/action/system/CleanCache.ts | 93 ------------------- src/onebot/action/system/GetLoginInfo.ts | 8 +- src/onebot/action/system/GetStatus.ts | 11 +-- src/onebot/action/system/Reboot.ts | 43 --------- src/onebot/action/user/GetCookies.ts | 6 +- src/onebot/action/user/SendLike.ts | 3 +- src/onebot/action/user/SetFriendAddRequest.ts | 1 + 16 files changed, 28 insertions(+), 165 deletions(-) delete mode 100644 src/onebot/action/system/CleanCache.ts delete mode 100644 src/onebot/action/system/Reboot.ts diff --git a/src/onebot/action/group/SetGroupKick.ts b/src/onebot/action/group/SetGroupKick.ts index 349b6569..87f1a013 100644 --- a/src/onebot/action/group/SetGroupKick.ts +++ b/src/onebot/action/group/SetGroupKick.ts @@ -20,9 +20,7 @@ export default class SetGroupKick extends BaseAction { actionName = ActionName.SetGroupKick; PayloadSchema = SchemaData; protected async _handle(payload: Payload): Promise { - const NTQQUserApi = this.CoreContext.getApiContext().UserApi; const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi; - const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi; const rejectReq = payload.reject_add_request?.toString() == 'true'; await NTQQGroupApi.kickMember(payload.group_id.toString(), [member.uid], rejectReq); return null; diff --git a/src/onebot/action/group/SetGroupName.ts b/src/onebot/action/group/SetGroupName.ts index d65383cb..60958702 100644 --- a/src/onebot/action/group/SetGroupName.ts +++ b/src/onebot/action/group/SetGroupName.ts @@ -17,7 +17,7 @@ export default class SetGroupName extends BaseAction { actionName = ActionName.SetGroupName; PayloadSchema = SchemaData; protected async _handle(payload: Payload): Promise { - + const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi; await NTQQGroupApi.setGroupName(payload.group_id.toString(), payload.group_name); return null; } diff --git a/src/onebot/action/group/SetGroupWholeBan.ts b/src/onebot/action/group/SetGroupWholeBan.ts index 38b4ffc7..e2e0efd6 100644 --- a/src/onebot/action/group/SetGroupWholeBan.ts +++ b/src/onebot/action/group/SetGroupWholeBan.ts @@ -1,8 +1,6 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { NTQQGroupApi } from '@/core/apis/group'; - const SchemaData = { type: 'object', properties: { @@ -19,6 +17,7 @@ export default class SetGroupWholeBan extends BaseAction { PayloadSchema = SchemaData; protected async _handle(payload: Payload): Promise { const enable = payload.enable?.toString() !== 'false'; + const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi; await NTQQGroupApi.banGroup(payload.group_id.toString(), enable); return null; } diff --git a/src/onebot/action/msg/DeleteMsg.ts b/src/onebot/action/msg/DeleteMsg.ts index f180329c..f5444c1c 100644 --- a/src/onebot/action/msg/DeleteMsg.ts +++ b/src/onebot/action/msg/DeleteMsg.ts @@ -1,10 +1,7 @@ -import { NTQQMsgApi } from '@/core/apis'; import { ActionName } from '../types'; import BaseAction from '../BaseAction'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { MessageUnique } from '@/common/utils/MessageUnique'; -import { sleep } from '@/common/utils/helper'; -import { NTEventDispatch } from '@/common/utils/EventTask'; import { NodeIKernelMsgListener } from '@/core'; const SchemaData = { @@ -26,9 +23,10 @@ class DeleteMsg extends BaseAction { actionName = ActionName.DeleteMsg; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { + const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi; const msg = MessageUnique.getMsgIdAndPeerByShortId(Number(payload.message_id)); if (msg) { - let ret = NTEventDispatch.RegisterListen + let ret = this.CoreContext.eventWrapper.RegisterListen ( 'NodeIKernelMsgListener/onMsgInfoListUpdate', 1, diff --git a/src/onebot/action/msg/ForwardSingleMsg.ts b/src/onebot/action/msg/ForwardSingleMsg.ts index 1c200437..952f8c36 100644 --- a/src/onebot/action/msg/ForwardSingleMsg.ts +++ b/src/onebot/action/msg/ForwardSingleMsg.ts @@ -1,5 +1,4 @@ import BaseAction from '../BaseAction'; -import { NTQQMsgApi, NTQQUserApi } from '@/core/apis'; import { ChatType, Peer } from '@/core/entities'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; @@ -19,6 +18,7 @@ type Payload = FromSchema; class ForwardSingleMsg extends BaseAction { protected async getTargetPeer(payload: Payload): Promise { + const NTQQUserApi = this.CoreContext.getApiContext().UserApi; if (payload.user_id) { const peerUid = await NTQQUserApi.getUidByUin(payload.user_id.toString()); if (!peerUid) { @@ -30,7 +30,8 @@ class ForwardSingleMsg extends BaseAction { } protected async _handle(payload: Payload): Promise { - const msg = await MessageUnique.getMsgIdAndPeerByShortId(payload.message_id); + const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi; + const msg = MessageUnique.getMsgIdAndPeerByShortId(payload.message_id); if (!msg) { throw new Error(`无法找到消息${payload.message_id}`); } diff --git a/src/onebot/action/msg/GetMsg.ts b/src/onebot/action/msg/GetMsg.ts index 29edde9e..189114fe 100644 --- a/src/onebot/action/msg/GetMsg.ts +++ b/src/onebot/action/msg/GetMsg.ts @@ -1,10 +1,9 @@ import { OB11Message } from '../../types'; -import { OB11Constructor } from '../../constructor'; +import { OB11Constructor } from '../../helper/constructor'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { MessageUnique } from '@/common/utils/MessageUnique'; -import { NTQQMsgApi } from '@/core'; export type ReturnDataType = OB11Message @@ -23,6 +22,7 @@ class GetMsg extends BaseAction { actionName = ActionName.GetMsg; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { + const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi; // log("history msg ids", Object.keys(msgHistory)); if (!payload.message_id) { throw Error('参数message_id不能为空'); diff --git a/src/onebot/action/msg/MarkMsgAsRead.ts b/src/onebot/action/msg/MarkMsgAsRead.ts index 740be9cf..05f82ed4 100644 --- a/src/onebot/action/msg/MarkMsgAsRead.ts +++ b/src/onebot/action/msg/MarkMsgAsRead.ts @@ -16,6 +16,7 @@ type PlayloadType = FromSchema; class MarkMsgAsRead extends BaseAction { async getPeer(payload: PlayloadType): Promise { + const NTQQUserApi = this.CoreContext.getApiContext().UserApi; if (payload.user_id) { const peerUid = await NTQQUserApi.getUidByUin(payload.user_id.toString()); if (!peerUid) { @@ -30,6 +31,7 @@ class MarkMsgAsRead extends BaseAction { return { chatType: ChatType.group, peerUid: payload.group_id.toString() }; } protected async _handle(payload: PlayloadType): Promise { + const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi; // 调用API const ret = await NTQQMsgApi.setMsgRead(await this.getPeer(payload)); if (ret.result != 0) { @@ -65,6 +67,7 @@ export class MarkAllMsgAsRead extends BaseAction { actionName = ActionName._MarkAllMsgAsRead; protected async _handle(payload: Payload): Promise { + const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi; await NTQQMsgApi.markallMsgAsRead(); return null; } diff --git a/src/onebot/action/msg/SendMsg/check-send-message.ts b/src/onebot/action/msg/SendMsg/check-send-message.ts index 299f41dc..76100a5e 100644 --- a/src/onebot/action/msg/SendMsg/check-send-message.ts +++ b/src/onebot/action/msg/SendMsg/check-send-message.ts @@ -1,4 +1,4 @@ -import { OB11MessageData } from '@/onebot11/types'; +import { OB11MessageData } from '@/onebot/types'; function checkSendMessage(sendMsgList: OB11MessageData[]) { function checkUri(uri: string): boolean { diff --git a/src/onebot/action/msg/SetMsgEmojiLike.ts b/src/onebot/action/msg/SetMsgEmojiLike.ts index edb8edcc..da9de4b4 100644 --- a/src/onebot/action/msg/SetMsgEmojiLike.ts +++ b/src/onebot/action/msg/SetMsgEmojiLike.ts @@ -19,6 +19,7 @@ export class SetMsgEmojiLike extends BaseAction { actionName = ActionName.SetMsgEmojiLike; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { + const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi; const msg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(payload.message_id.toString())); if (!msg) { throw new Error('msg not found'); diff --git a/src/onebot/action/system/CleanCache.ts b/src/onebot/action/system/CleanCache.ts deleted file mode 100644 index 0e0cafc2..00000000 --- a/src/onebot/action/system/CleanCache.ts +++ /dev/null @@ -1,93 +0,0 @@ -import BaseAction from '../BaseAction'; -import { ActionName } from '../types'; -import fs from 'fs'; -import Path from 'path'; -import { - ChatType, - ChatCacheListItemBasic, - CacheFileType -} from '@/core/entities'; -import { NTQQFileApi, NTQQFileCacheApi } from '@/core/apis/file'; -import { logError } from '@/common/utils/log'; - -export default class CleanCache extends BaseAction { - actionName = ActionName.CleanCache; - - protected _handle(): Promise { - return new Promise(async (res, rej) => { - try { - // dbUtil.clearCache(); - const cacheFilePaths: string[] = []; - - await NTQQFileCacheApi.setCacheSilentScan(false); - - cacheFilePaths.push((await NTQQFileCacheApi.getHotUpdateCachePath())); - cacheFilePaths.push((await NTQQFileCacheApi.getDesktopTmpPath())); - (await NTQQFileCacheApi.getCacheSessionPathList()).forEach((e: { value: string; }) => cacheFilePaths.push(e.value)); - - // await NTQQApi.addCacheScannedPaths(); // XXX: 调用就崩溃,原因目前还未知 - const cacheScanResult = await NTQQFileCacheApi.scanCache(); - const cacheSize = parseInt(cacheScanResult.size[6]); - - if (cacheScanResult.result !== 0) { - throw('Something went wrong while scanning cache. Code: ' + cacheScanResult.result); - } - - await NTQQFileCacheApi.setCacheSilentScan(true); - if (cacheSize > 0 && cacheFilePaths.length > 2) { // 存在缓存文件且大小不为 0 时执行清理动作 - // await NTQQApi.clearCache([ 'tmp', 'hotUpdate', ...cacheScanResult ]) // XXX: 也是调用就崩溃,调用 fs 删除得了 - deleteCachePath(cacheFilePaths); - } - - // 获取聊天记录列表 - // NOTE: 以防有人不需要删除聊天记录,暂时先注释掉,日后加个开关 - // const privateChatCache = await getCacheList(ChatType.friend); // 私聊消息 - // const groupChatCache = await getCacheList(ChatType.group); // 群聊消息 - // const chatCacheList = [ ...privateChatCache, ...groupChatCache ]; - const chatCacheList: ChatCacheListItemBasic[] = []; - - // 获取聊天缓存文件列表 - const cacheFileList: string[] = []; - - for (const name in CacheFileType) { - if (!isNaN(parseInt(name))) continue; - - const fileTypeAny: any = CacheFileType[name]; - const fileType: CacheFileType = fileTypeAny; - - cacheFileList.push(...(await NTQQFileCacheApi.getFileCacheInfo(fileType)).infos.map((file: { fileKey: any; }) => file.fileKey)); - } - - // 一并清除 - await NTQQFileCacheApi.clearChatCache(chatCacheList, cacheFileList); - res(); - } catch(e) { - logError('清理缓存时发生了错误'); - rej(e); - } - }); - } -} - -function deleteCachePath(pathList: string[]) { - const emptyPath = (path: string) => { - if (!fs.existsSync(path)) return; - const files = fs.readdirSync(path); - files.forEach(file => { - const filePath = Path.resolve(path, file); - const stats = fs.statSync(filePath); - if (stats.isDirectory()) emptyPath(filePath); - else fs.unlinkSync(filePath); - }); - fs.rmdirSync(path); - }; - - for (const path of pathList) { - emptyPath(path); - } -} - -function getCacheList(type: ChatType) { // NOTE: 做这个方法主要是因为目前还不支持针对频道消息的清理 - return new Promise>((res, rej) => { - }); -} diff --git a/src/onebot/action/system/GetLoginInfo.ts b/src/onebot/action/system/GetLoginInfo.ts index 7d43d3b7..4a73664b 100644 --- a/src/onebot/action/system/GetLoginInfo.ts +++ b/src/onebot/action/system/GetLoginInfo.ts @@ -1,16 +1,14 @@ -import { selfInfo } from '@/core/data'; + import { OB11User } from '../../types'; -import { OB11Constructor } from '../../constructor'; +import { OB11Constructor } from '../../helper/constructor'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { napCatCore } from '@/core'; - class GetLoginInfo extends BaseAction { actionName = ActionName.GetLoginInfo; protected async _handle(payload: null) { - return OB11Constructor.selfInfo(selfInfo); + return OB11Constructor.selfInfo(this.CoreContext.selfInfo); } } diff --git a/src/onebot/action/system/GetStatus.ts b/src/onebot/action/system/GetStatus.ts index affbe13f..715e5b1b 100644 --- a/src/onebot/action/system/GetStatus.ts +++ b/src/onebot/action/system/GetStatus.ts @@ -1,17 +1,14 @@ import BaseAction from '../BaseAction'; -import { OB11Status } from '../../types'; import { ActionName } from '../types'; -import { selfInfo, stat } from '@/core/data'; - -export default class GetStatus extends BaseAction { +export default class GetStatus extends BaseAction { actionName = ActionName.GetStatus; - protected async _handle(payload: any): Promise { + protected async _handle(payload: any): Promise { return { - online: !!selfInfo.online, + online: !!this.CoreContext.selfInfo.online, good: true, - stat + stat:{} }; } } diff --git a/src/onebot/action/system/Reboot.ts b/src/onebot/action/system/Reboot.ts deleted file mode 100644 index 19d6ad9c..00000000 --- a/src/onebot/action/system/Reboot.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { rebootWithNormolLogin, rebootWithQuickLogin } from '@/common/utils/reboot'; -import BaseAction from '../BaseAction'; -import { ActionName } from '../types'; -import { selfInfo } from '@/core/data'; -import { FromSchema, JSONSchema } from 'json-schema-to-ts'; - -const SchemaData = { - type: 'object', - properties: { - delay: { type: 'number' } - } -} as const satisfies JSONSchema; - -type Payload = FromSchema; - -export class Reboot extends BaseAction { - actionName = ActionName.Reboot; - - protected async _handle(payload: Payload): Promise { - if (payload.delay) { - setTimeout(() => { - rebootWithQuickLogin(selfInfo.uin); - }, payload.delay); - } else { - rebootWithQuickLogin(selfInfo.uin); - } - return null; - } -} -export class RebootNormal extends BaseAction { - actionName = ActionName.RebootNormal; - - protected async _handle(payload: Payload): Promise { - if (payload.delay) { - setTimeout(() => { - rebootWithNormolLogin(); - }, payload.delay); - } else { - rebootWithNormolLogin(); - } - return null; - } -} diff --git a/src/onebot/action/user/GetCookies.ts b/src/onebot/action/user/GetCookies.ts index cbd852f1..7d136fc8 100644 --- a/src/onebot/action/user/GetCookies.ts +++ b/src/onebot/action/user/GetCookies.ts @@ -1,6 +1,6 @@ + import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { NTQQUserApi, WebApi } from '@/core/apis'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; interface Response { cookies: string, @@ -20,6 +20,8 @@ export class GetCookies extends BaseAction { actionName = ActionName.GetCookies; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { + const NTQQUserApi = this.CoreContext.getApiContext().UserApi; + const NTQQWebApi = this.CoreContext.getApiContext().WebApi; // if (!payload.domain) { // throw new Error('缺少参数 domain'); // } @@ -63,7 +65,7 @@ export class GetCookies extends BaseAction { const cookiesObject = await NTQQUserApi.getCookies(payload.domain); //把获取到的cookiesObject转换成 k=v; 格式字符串拼接在一起 const cookies = Object.entries(cookiesObject).map(([key, value]) => `${key}=${value}`).join('; '); - const bkn = WebApi.genBkn(cookiesObject.p_skey); + const bkn = NTQQWebApi.getBknFromCookie(cookiesObject.p_skey); return { cookies, bkn }; } } diff --git a/src/onebot/action/user/SendLike.ts b/src/onebot/action/user/SendLike.ts index a3197523..9105f030 100644 --- a/src/onebot/action/user/SendLike.ts +++ b/src/onebot/action/user/SendLike.ts @@ -1,4 +1,4 @@ -import { NTQQUserApi } from '@/core/apis'; + import BaseAction from '../BaseAction'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; @@ -18,6 +18,7 @@ export default class SendLike extends BaseAction { actionName = ActionName.SendLike; PayloadSchema = SchemaData; protected async _handle(payload: Payload): Promise { + const NTQQUserApi = this.CoreContext.getApiContext().UserApi; //logDebug('点赞参数', payload); try { const qq = payload.user_id.toString(); diff --git a/src/onebot/action/user/SetFriendAddRequest.ts b/src/onebot/action/user/SetFriendAddRequest.ts index 1befb65a..12e7550f 100644 --- a/src/onebot/action/user/SetFriendAddRequest.ts +++ b/src/onebot/action/user/SetFriendAddRequest.ts @@ -19,6 +19,7 @@ export default class SetFriendAddRequest extends BaseAction { actionName = ActionName.SetFriendAddRequest; PayloadSchema = SchemaData; protected async _handle(payload: Payload): Promise { + const NTQQFriendApi = this.CoreContext.getApiContext().FriendApi; const approve = payload.approve?.toString() !== 'false'; await NTQQFriendApi.handleFriendRequest(payload.flag, approve); return null;