mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-12 16:00:27 +00:00
style: 强类型大法
This commit is contained in:
@@ -5,7 +5,6 @@ import { OB11FriendPokeEvent } from '@/onebot/event/notice/OB11PokeEvent';
|
||||
export class OneBotFriendApi {
|
||||
obContext: NapCatOneBot11Adapter;
|
||||
core: NapCatCore;
|
||||
friendList: Map<string, any> = new Map();//此处作为缓存 uin->info
|
||||
constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore) {
|
||||
this.obContext = obContext;
|
||||
this.core = core;
|
||||
@@ -17,7 +16,7 @@ export class OneBotFriendApi {
|
||||
const pokedetail: Array<{ uid: string }> = json.items;
|
||||
//筛选item带有uid的元素
|
||||
const poke_uid = pokedetail.filter(item => item.uid);
|
||||
if (poke_uid.length == 2) {
|
||||
if (poke_uid.length == 2 && poke_uid[0]?.uid && poke_uid[1]?.uid) {
|
||||
return new OB11FriendPokeEvent(
|
||||
this.core,
|
||||
uin,
|
||||
|
||||
@@ -29,7 +29,6 @@ import { OB11GroupIncreaseEvent } from '../event/notice/OB11GroupIncreaseEvent';
|
||||
export class OneBotGroupApi {
|
||||
obContext: NapCatOneBot11Adapter;
|
||||
core: NapCatCore;
|
||||
GroupMemberList: Map<string, any> = new Map();//此处作为缓存 group_id->memberUin->info
|
||||
constructor(obContext: NapCatOneBot11Adapter, core: NapCatCore) {
|
||||
this.obContext = obContext;
|
||||
this.core = core;
|
||||
@@ -130,10 +129,10 @@ export class OneBotGroupApi {
|
||||
|
||||
//判断业务类型
|
||||
//Poke事件
|
||||
const pokedetail: any[] = json.items;
|
||||
const pokedetail: Array<{ uid: string }> = json.items;
|
||||
//筛选item带有uid的元素
|
||||
const poke_uid = pokedetail.filter(item => item.uid);
|
||||
if (poke_uid.length == 2) {
|
||||
if (poke_uid.length == 2 && poke_uid[0]?.uid && poke_uid[1]?.uid) {
|
||||
return new OB11GroupPokeEvent(
|
||||
this.core,
|
||||
parseInt(msg.peerUid),
|
||||
@@ -164,6 +163,7 @@ export class OneBotGroupApi {
|
||||
} else {
|
||||
context.logger.logWarn('收到未知的灰条消息', json);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
async parseEssenceMsg(msg: RawMessage, jsonStr: string) {
|
||||
@@ -181,13 +181,16 @@ export class OneBotGroupApi {
|
||||
const msgData = await this.core.apis.MsgApi.getMsgsBySeqAndCount(Peer, msgSeq.toString(), 1, true, true);
|
||||
const msgList = (await this.core.apis.WebApi.getGroupEssenceMsgAll(Group)).flatMap((e) => e.data.msg_list);
|
||||
const realMsg = msgList.find((e) => e.msg_seq.toString() == msgSeq);
|
||||
return new OB11GroupEssenceEvent(
|
||||
this.core,
|
||||
parseInt(msg.peerUid),
|
||||
MessageUnique.getShortIdByMsgId(msgData.msgList[0].msgId)!,
|
||||
parseInt(msgData.msgList[0].senderUin),
|
||||
parseInt(realMsg?.add_digest_uin ?? '0'),
|
||||
);
|
||||
if (msgData.msgList[0]) {
|
||||
return new OB11GroupEssenceEvent(
|
||||
this.core,
|
||||
parseInt(msg.peerUid),
|
||||
MessageUnique.getShortIdByMsgId(msgData.msgList[0].msgId)!,
|
||||
parseInt(msgData.msgList[0].senderUin),
|
||||
parseInt(realMsg?.add_digest_uin ?? '0'),
|
||||
);
|
||||
}
|
||||
return;
|
||||
// 获取MsgSeq+Peer可获取具体消息
|
||||
}
|
||||
|
||||
@@ -233,6 +236,7 @@ export class OneBotGroupApi {
|
||||
);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
async parseSelfInviteEvent(msg: RawMessage, inviterUin: string, inviteeUin: string) {
|
||||
|
||||
@@ -44,7 +44,7 @@ type RawToOb11Converters = {
|
||||
msg: RawMessage,
|
||||
elementWrapper: MessageElement,
|
||||
context: RecvMessageContext
|
||||
) => PromiseLike<OB11MessageData | null>
|
||||
) => Promise<OB11MessageData | null | undefined>
|
||||
}
|
||||
|
||||
type Ob11ToRawConverters = {
|
||||
@@ -128,9 +128,9 @@ export class OneBotMsgApi {
|
||||
file_size: element.fileSize,
|
||||
},
|
||||
};
|
||||
} catch (e: any) {
|
||||
this.core.context.logger.logError('获取图片url失败', e.stack);
|
||||
return null;
|
||||
} catch (e) {
|
||||
this.core.context.logger.logError('获取图片url失败', (e as Error).stack);
|
||||
return;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -296,7 +296,7 @@ export class OneBotMsgApi {
|
||||
peerUid: msg.peerUid,
|
||||
guildId: '0',
|
||||
}, msg.parentMsgIdList[0] ?? msg.msgId, elementWrapper.elementId);
|
||||
} catch (error) {
|
||||
} catch {
|
||||
this.core.context.logger.logWarn('合并获取视频 URL 失败');
|
||||
}
|
||||
} else {
|
||||
@@ -306,7 +306,7 @@ export class OneBotMsgApi {
|
||||
peerUid: msg.peerUid,
|
||||
guildId: '0',
|
||||
}, msg.msgId, elementWrapper.elementId);
|
||||
} catch (error) {
|
||||
} catch {
|
||||
this.core.context.logger.logWarn('获取视频 URL 失败');
|
||||
}
|
||||
}
|
||||
@@ -358,7 +358,7 @@ export class OneBotMsgApi {
|
||||
};
|
||||
},
|
||||
|
||||
multiForwardMsgElement: async (_, msg, wrapper, context) => {
|
||||
multiForwardMsgElement: async (_, msg, _wrapper, context) => {
|
||||
const parentMsgPeer = msg.parentMsgPeer ?? {
|
||||
chatType: msg.chatType,
|
||||
guildId: '',
|
||||
@@ -465,7 +465,13 @@ export class OneBotMsgApi {
|
||||
const parsedFaceId = +id;
|
||||
// 从face_config.json中获取表情名称
|
||||
const sysFaces = faceConfig.sysface;
|
||||
const face: any = sysFaces.find((systemFace) => systemFace.QSid === parsedFaceId.toString());
|
||||
const face: {
|
||||
QSid?: string,
|
||||
QDes?: string,
|
||||
AniStickerId?: string,
|
||||
AniStickerType?: number,
|
||||
AniStickerPackId?: string,
|
||||
} | undefined = sysFaces.find((systemFace) => systemFace.QSid === parsedFaceId.toString());
|
||||
if (!face) {
|
||||
this.core.context.logger.logError('不支持的ID', id);
|
||||
return undefined;
|
||||
@@ -622,7 +628,7 @@ export class OneBotMsgApi {
|
||||
//throw Error('音乐消息签名地址未配置');
|
||||
}
|
||||
try {
|
||||
const musicJson = await RequestUtil.HttpGetJson<any>(signUrl, 'POST', postData);
|
||||
const musicJson = await RequestUtil.HttpGetJson<string>(signUrl, 'POST', postData);
|
||||
return this.ob11ToRawConverters.json({
|
||||
data: { data: musicJson },
|
||||
type: OB11MessageDataType.json
|
||||
@@ -630,6 +636,7 @@ export class OneBotMsgApi {
|
||||
} catch (e) {
|
||||
this.core.context.logger.logError('生成音乐消息失败', e);
|
||||
}
|
||||
return undefined;
|
||||
},
|
||||
|
||||
[OB11MessageDataType.node]: async () => undefined,
|
||||
@@ -671,6 +678,7 @@ export class OneBotMsgApi {
|
||||
type: OB11MessageDataType.json
|
||||
}, context);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -688,6 +696,7 @@ export class OneBotMsgApi {
|
||||
return new OB11FriendAddNoticeEvent(this.core, Number(await this.core.apis.UserApi.getUinByUidV2(msg.peerUid)));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
private async getMultiMessages(msg: RawMessage, parentMsgPeer: Peer) {
|
||||
@@ -696,11 +705,14 @@ export class OneBotMsgApi {
|
||||
//首次列表不存在则开始创建
|
||||
msg.parentMsgIdList.push(msg.msgId);
|
||||
//拉取下级消息
|
||||
return (await this.core.apis.MsgApi.getMultiMsg(
|
||||
parentMsgPeer,
|
||||
msg.parentMsgIdList[0],
|
||||
msg.msgId
|
||||
))?.msgList;
|
||||
if (msg.parentMsgIdList[0]) {
|
||||
return (await this.core.apis.MsgApi.getMultiMsg(
|
||||
parentMsgPeer,
|
||||
msg.parentMsgIdList[0],
|
||||
msg.msgId
|
||||
))?.msgList;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private async parseMultiMessageContent(
|
||||
@@ -831,7 +843,7 @@ export class OneBotMsgApi {
|
||||
msg: RawMessage,
|
||||
elementWrapper: MessageElement,
|
||||
context: RecvMessageContext
|
||||
) => PromiseLike<OB11MessageData | null>;
|
||||
) => Promise<OB11MessageData | null>;
|
||||
const parsedElement = await converters?.(
|
||||
element[key],
|
||||
msg,
|
||||
@@ -844,6 +856,7 @@ export class OneBotMsgApi {
|
||||
return parsedElement;
|
||||
}
|
||||
}
|
||||
return [];
|
||||
},
|
||||
));
|
||||
|
||||
@@ -897,7 +910,7 @@ export class OneBotMsgApi {
|
||||
return { sendElements, deleteAfterSentFiles };
|
||||
}
|
||||
|
||||
async sendMsgWithOb11UniqueId(peer: Peer, sendElements: SendMessageElement[], deleteAfterSentFiles: string[], waitComplete = true) {
|
||||
async sendMsgWithOb11UniqueId(peer: Peer, sendElements: SendMessageElement[], deleteAfterSentFiles: string[]) {
|
||||
if (!sendElements.length) {
|
||||
throw new Error('消息体无法解析, 请检查是否发送了不支持的消息类型');
|
||||
}
|
||||
@@ -928,7 +941,7 @@ export class OneBotMsgApi {
|
||||
|
||||
const timeout = 10000 + (totalSize / 1024 / 256 * 1000);
|
||||
|
||||
const returnMsg = await this.core.apis.MsgApi.sendMsg(peer, sendElements, waitComplete, timeout);
|
||||
const returnMsg = await this.core.apis.MsgApi.sendMsg(peer, sendElements, timeout);
|
||||
if (!returnMsg) throw new Error('发送消息失败');
|
||||
|
||||
returnMsg.id = MessageUnique.createUniqueMsgId({
|
||||
@@ -1025,7 +1038,7 @@ export class OneBotMsgApi {
|
||||
if (isAdminOrOwner && !operatorUid) {
|
||||
let dataNotify: GroupNotify | undefined;
|
||||
await this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onGroupNotifiesUpdated',
|
||||
(doubt, notifies) => {
|
||||
(_doubt, notifies) => {
|
||||
for (const notify of notifies) {
|
||||
if (notify.group.groupCode === groupUin && notify.user1.uid === memberUid) {
|
||||
dataNotify = notify;
|
||||
@@ -1124,7 +1137,7 @@ export class OneBotMsgApi {
|
||||
}
|
||||
return false;
|
||||
}, 1, 1000);
|
||||
} catch (error) {
|
||||
} catch {
|
||||
request_seq = '';
|
||||
}
|
||||
// 未拉取到seq
|
||||
@@ -1173,5 +1186,6 @@ export class OneBotMsgApi {
|
||||
} else if (SysMessage.contentHead.type == 528 && SysMessage.contentHead.subType == 39 && SysMessage.body?.msgContent) {
|
||||
return await this.obContext.apis.UserApi.parseLikeEvent(SysMessage.body?.msgContent);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,9 +46,7 @@ export class OneBotQuickActionApi {
|
||||
async handleMsg(msg: OB11Message, quickAction: QuickAction) {
|
||||
const reply = quickAction.reply;
|
||||
const peerContextMode = msg.message_type == 'private' ? ContextMode.Private : ContextMode.Group;
|
||||
|
||||
const peer: Peer = await createContext(this.core, {
|
||||
message_type: undefined,
|
||||
group_id: msg.group_id?.toString(),
|
||||
user_id: msg.user_id?.toString(),
|
||||
}, peerContextMode);
|
||||
@@ -79,7 +77,7 @@ export class OneBotQuickActionApi {
|
||||
sendElements,
|
||||
deleteAfterSentFiles,
|
||||
} = await this.obContext.apis.MsgApi.createSendElements(replyMessage, peer);
|
||||
this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles, false).then().catch(e => this.core.context.logger.logError(e));
|
||||
this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles).then().catch(e => this.core.context.logger.logError(e));
|
||||
}
|
||||
}
|
||||
async findNotify(flag: string) {
|
||||
@@ -91,7 +89,7 @@ export class OneBotQuickActionApi {
|
||||
}
|
||||
|
||||
async handleGroupRequest(request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest) {
|
||||
|
||||
|
||||
const invite_notify = this.obContext.apis.MsgApi.notifyGroupInvite.get(request.flag);
|
||||
const notify = invite_notify ?? await this.findNotify(request.flag);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { GrayTipRovokeElement, NapCatCore, RawMessage } from '@/core';
|
||||
import { NapCatCore } from '@/core';
|
||||
import { NapCatOneBot11Adapter } from '@/onebot';
|
||||
import { OB11ProfileLikeEvent } from '@/onebot/event/notice/OB11ProfileLikeEvent';
|
||||
import { decodeProfileLikeTip } from '@/core/helper/adaptDecoder';
|
||||
|
||||
Reference in New Issue
Block a user