Skip to content

Commit

Permalink
refactor: normalize naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Wesley-Young committed Aug 26, 2024
1 parent 0bba329 commit bc62feb
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/core/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeQQNTWrapperUtil, NTApiContext, WrapperNodeApi } from '@/core/wrapper';
import { NodeQQNTWrapperUtil, StableNTApiWrapper, WrapperNodeApi } from '@/core/wrapper';
import path from 'node:path';
import fs from 'node:fs';
import { InstanceContext } from './wrapper';
Expand Down Expand Up @@ -30,7 +30,7 @@ export function loadQQWrapper(QQVersion: string): WrapperNodeApi {

export class NapCatCore {
readonly context: InstanceContext;
readonly apis: NTApiContext;
readonly apis: StableNTApiWrapper;
readonly eventWrapper: LegacyNTEventWrapper;
// readonly eventChannel: NTEventChannel;
NapCatDataPath: string;
Expand Down
2 changes: 1 addition & 1 deletion src/core/wrapper/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface InstanceContext {
readonly pathWrapper: NapCatPathWrapper;
}

export interface NTApiContext {
export interface StableNTApiWrapper {
FileApi: NTQQFileApi,
SystemApi: NTQQSystemApi,
CollectionApi: NTQQCollectionApi,
Expand Down
8 changes: 2 additions & 6 deletions src/onebot/action/go-cqhttp/GetForwardMsg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BaseAction from '../BaseAction';
import { OB11ForwardMessage, OB11Message, OB11MessageData } from '@/onebot';
import { OB11ForwardMessage } from '@/onebot';
import { ActionName } from '../types';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { MessageUnique } from '@/common/utils/MessageUnique';
Expand All @@ -14,10 +14,6 @@ const SchemaData = {

type Payload = FromSchema<typeof SchemaData>;

interface Response {
messages: (OB11Message & { content: OB11MessageData })[];
}

export class GoCQHTTPGetForwardMsgAction extends BaseAction<Payload, any> {
actionName = ActionName.GoCQHTTP_GetForwardMsg;
payloadSchema = SchemaData;
Expand All @@ -39,7 +35,7 @@ export class GoCQHTTPGetForwardMsgAction extends BaseAction<Payload, any> {
}
const msgList = data.msgList;
const messages = (await Promise.all(msgList.map(async msg => {
const resMsg = await this.obContext.apiContext.MsgApi
const resMsg = await this.obContext.apis.MsgApi
.parseMessage(msg);
if (!resMsg) return;
resMsg.message_id = MessageUnique.createMsg({
Expand Down
2 changes: 1 addition & 1 deletion src/onebot/action/go-cqhttp/GetFriendMsgHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class GetFriendMsgHistory extends BaseAction<Payload, Response> {
}));
//转换消息
const ob11MsgList = (await Promise.all(
msgList.map(msg => this.obContext.apiContext.MsgApi.parseMessage(msg)))
msgList.map(msg => this.obContext.apis.MsgApi.parseMessage(msg)))
).filter(msg => msg !== undefined);
return { 'messages': ob11MsgList };
}
Expand Down
2 changes: 1 addition & 1 deletion src/onebot/action/go-cqhttp/GetGroupMsgHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class GoCQHTTPGetGroupMsgHistory extends BaseAction<Payload, Resp

//转换消息
const ob11MsgList = (await Promise.all(
msgList.map(msg => this.obContext.apiContext.MsgApi.parseMessage(msg)))
msgList.map(msg => this.obContext.apis.MsgApi.parseMessage(msg)))
).filter(msg => msg !== undefined);
return { 'messages': ob11MsgList };
}
Expand Down
2 changes: 1 addition & 1 deletion src/onebot/action/go-cqhttp/UploadGroupFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class GoCQHTTPUploadGroupFile extends BaseAction<Payload, null> {
throw new Error(downloadResult.errMsg);
}
const sendFileEle = await this.core.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name, payload.folder_id);
await this.obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId({
await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId({
chatType: ChatType.KCHATTYPEGROUP,
peerUid: payload.group_id.toString(),
}, [sendFileEle], [], true);
Expand Down
2 changes: 1 addition & 1 deletion src/onebot/action/go-cqhttp/UploadPrivateFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class GoCQHTTPUploadPrivateFile extends BaseAction<Payload, null>
throw new Error(downloadResult.errMsg);
}
const sendFileEle: SendFileElement = await this.core.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name);
await this.obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId(await this.getPeer(payload), [sendFileEle], [], true);
await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(await this.getPeer(payload), [sendFileEle], [], true);
return null;
}
}
2 changes: 1 addition & 1 deletion src/onebot/action/msg/GetMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GetMsg extends BaseAction<Payload, OB11Message> {
const msg = await NTQQMsgApi.getMsgsByMsgId(
peer,
[msgIdWithPeer?.MsgId || payload.message_id.toString()]);
const retMsg = await this.obContext.apiContext.MsgApi.parseMessage(msg.msgList[0], 'array');
const retMsg = await this.obContext.apis.MsgApi.parseMessage(msg.msgList[0], 'array');
if (!retMsg) throw Error('消息为空');
try {
retMsg.message_id = MessageUnique.createMsg(peer, msg.msgList[0].msgId)!;
Expand Down
8 changes: 4 additions & 4 deletions src/onebot/action/msg/SendMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
}
// log("send msg:", peer, sendElements)

const { sendElements, deleteAfterSentFiles } = await this.obContext.apiContext.MsgApi
const { sendElements, deleteAfterSentFiles } = await this.obContext.apis.MsgApi
.createSendElements(messages, peer);
const returnMsg = await this.obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles);
const returnMsg = await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles);
return { message_id: returnMsg!.id! };
}

Expand Down Expand Up @@ -180,15 +180,15 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
//完成子卡片生成跳过后续
continue;
}
const { sendElements } = await this.obContext.apiContext.MsgApi
const { sendElements } = await this.obContext.apis.MsgApi
.createSendElements(OB11Data, destPeer);
//拆分消息
const MixElement = sendElements.filter(element => element.elementType !== ElementType.FILE && element.elementType !== ElementType.VIDEO);
const SingleElement = sendElements.filter(element => element.elementType === ElementType.FILE || element.elementType === ElementType.VIDEO).map(e => [e]);
const AllElement: SendMessageElement[][] = [MixElement, ...SingleElement].filter(e => e !== undefined && e.length !== 0);
const MsgNodeList: Promise<RawMessage | undefined>[] = [];
for (const sendElementsSplitElement of AllElement) {
MsgNodeList.push(this.obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId(selfPeer, sendElementsSplitElement, [], true).catch(_ => undefined));
MsgNodeList.push(this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(selfPeer, sendElementsSplitElement, [], true).catch(_ => undefined));
}
(await Promise.allSettled(MsgNodeList)).map((result) => {
if (result.status === 'fulfilled' && result.value) {
Expand Down
2 changes: 1 addition & 1 deletion src/onebot/action/user/GetRecentContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class GetRecentContact extends BaseAction<Payload, any> {
const FastMsg = await NTQQMsgApi.getMsgsByMsgId({ chatType: t.chatType, peerUid: t.peerUid }, [t.msgId]);
if (FastMsg.msgList.length > 0) {
//扩展ret.info.changedList
const lastestMsg = await this.obContext.apiContext.MsgApi.parseMessage(FastMsg.msgList[0], 'array');
const lastestMsg = await this.obContext.apis.MsgApi.parseMessage(FastMsg.msgList[0], 'array');
return {
lastestMsg: lastestMsg,
peerUin: t.peerUin,
Expand Down
12 changes: 6 additions & 6 deletions src/onebot/helper/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function NT2PrivateEvent(core: NapCatCore, obContext: NapCatOneBot1
if (element.grayTipElement) {
if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_JSON) {
if (element.grayTipElement.jsonGrayTipElement.busiId == 1061) {
const PokeEvent = await obContext.apiContext.FriendApi.parsePrivatePokeEvent(element.grayTipElement);
const PokeEvent = await obContext.apis.FriendApi.parsePrivatePokeEvent(element.grayTipElement);
if (PokeEvent) return PokeEvent;
}
}
Expand Down Expand Up @@ -56,15 +56,15 @@ export async function NT2GroupEvent(core: NapCatCore, obContext: NapCatOneBot11A
if (element.grayTipElement && element.grayTipElement.groupElement) {
const groupElement = element.grayTipElement.groupElement;
if (groupElement.type == TipGroupElementType.memberIncrease) {
const MemberIncreaseEvent = await obContext.apiContext.GroupApi.parseGroupMemberIncreaseEvent(msg.peerUid, element.grayTipElement);
const MemberIncreaseEvent = await obContext.apis.GroupApi.parseGroupMemberIncreaseEvent(msg.peerUid, element.grayTipElement);
if (MemberIncreaseEvent) return MemberIncreaseEvent;
} else if (groupElement.type === TipGroupElementType.ban) {
const BanEvent = await obContext.apiContext.GroupApi.parseGroupBanEvent(msg.peerUid, element.grayTipElement);
const BanEvent = await obContext.apis.GroupApi.parseGroupBanEvent(msg.peerUid, element.grayTipElement);
if (BanEvent) return BanEvent;
} else if (groupElement.type == TipGroupElementType.kicked) {
NTQQGroupApi.quitGroup(msg.peerUid).then();
try {
const KickEvent = await obContext.apiContext.GroupApi.parseGroupKickEvent(msg.peerUid, element.grayTipElement);
const KickEvent = await obContext.apis.GroupApi.parseGroupKickEvent(msg.peerUid, element.grayTipElement);
if (KickEvent) return KickEvent;
} catch (e) {
return new OB11GroupDecreaseEvent(
Expand All @@ -90,11 +90,11 @@ export async function NT2GroupEvent(core: NapCatCore, obContext: NapCatOneBot11A
}
if (element.grayTipElement) {
if (element.grayTipElement.xmlElement?.templId === '10382') {
const emojiLikeEvent = await obContext.apiContext.GroupApi.parseGroupEmjioLikeEvent(msg.peerUid, element.grayTipElement);
const emojiLikeEvent = await obContext.apis.GroupApi.parseGroupEmjioLikeEvent(msg.peerUid, element.grayTipElement);
if (emojiLikeEvent) return emojiLikeEvent;
}
if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_XMLMSG) {
const GroupIncreaseEvent = await obContext.apiContext.GroupApi.parseGroupIncreaseEvent(msg.peerUid, element.grayTipElement);
const GroupIncreaseEvent = await obContext.apis.GroupApi.parseGroupIncreaseEvent(msg.peerUid, element.grayTipElement);
if (GroupIncreaseEvent) return GroupIncreaseEvent;
}

Expand Down
4 changes: 2 additions & 2 deletions src/onebot/helper/quick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ async function handleMsg(core: NapCatCore, obContext: NapCatOneBot11Adapter, msg
}
}
replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape));
const { sendElements, deleteAfterSentFiles } = await obContext.apiContext.MsgApi.createSendElements(replyMessage, peer);
obContext.apiContext.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles, false).then().catch(core.context.logger.logError);
const { sendElements, deleteAfterSentFiles } = await obContext.apis.MsgApi.createSendElements(replyMessage, peer);
obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles, false).then().catch(core.context.logger.logError);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/onebot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
NodeIKernelGroupListener,
} from '@/core';
import { OB11Config, OB11ConfigLoader } from '@/onebot/helper/config';
import { OneBotApiContextType } from '@/onebot/types';
import { StableOneBotApiWrapper } from '@/onebot/types';
import {
OB11ActiveHttpAdapter,
OB11ActiveWebSocketAdapter,
Expand Down Expand Up @@ -44,7 +44,7 @@ export class NapCatOneBot11Adapter {
readonly context: InstanceContext;

configLoader: OB11ConfigLoader;
apiContext: OneBotApiContextType;
apis: StableOneBotApiWrapper;
networkManager: OB11NetworkManager;
actions: ActionMap;

Expand All @@ -54,7 +54,7 @@ export class NapCatOneBot11Adapter {
this.core = core;
this.context = context;
this.configLoader = new OB11ConfigLoader(core, pathWrapper.configPath);
this.apiContext = {
this.apis = {
GroupApi: new OneBotGroupApi(this, core),
UserApi: new OneBotUserApi(this, core),
FriendApi: new OneBotFriendApi(this, core),
Expand Down Expand Up @@ -259,7 +259,7 @@ export class NapCatOneBot11Adapter {
if (msg.sendStatus == SendStatusType.KSEND_STATUS_SUCCESS && !msgIdSend.get(msg.msgId)) {
msgIdSend.put(msg.msgId, true);
// 完成后再post
this.apiContext.MsgApi.parseMessage(msg)
this.apis.MsgApi.parseMessage(msg)
.then((ob11Msg) => {
if (!ob11Msg) return;
ob11Msg.target_id = parseInt(msg.peerUin);
Expand Down Expand Up @@ -457,7 +457,7 @@ export class NapCatOneBot11Adapter {
private async emitMsg(message: RawMessage) {
const { debug, reportSelfMessage, messagePostFormat } = this.configLoader.configData;
this.context.logger.logDebug('收到新消息 RawMessage', message);
this.apiContext.MsgApi.parseMessage(message, messagePostFormat).then((ob11Msg) => {
this.apis.MsgApi.parseMessage(message, messagePostFormat).then((ob11Msg) => {
if (!ob11Msg) return;
this.context.logger.logDebug('转化为 OB11Message', ob11Msg);
if (debug) {
Expand Down
2 changes: 1 addition & 1 deletion src/onebot/types/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OneBotFriendApi, OneBotGroupApi, OneBotMsgApi, OneBotUserApi } from '../api';

export interface OneBotApiContextType {
export interface StableOneBotApiWrapper {
FriendApi: OneBotFriendApi;
UserApi: OneBotUserApi;
GroupApi: OneBotGroupApi;
Expand Down

0 comments on commit bc62feb

Please sign in to comment.