From 91c689baf809d09ae7ed763cb7f8e30900823b34 Mon Sep 17 00:00:00 2001 From: idranme Date: Fri, 13 Sep 2024 14:56:30 +0800 Subject: [PATCH] fix --- src/common/utils/misc.ts | 8 ++++++-- src/onebot11/action/go-cqhttp/GetForwardMsg.ts | 17 ++++++++++------- .../action/go-cqhttp/GetGroupMsgHistory.ts | 3 ++- src/onebot11/action/msg/GetMsg.ts | 7 +++++-- src/onebot11/adapter.ts | 5 ++++- src/onebot11/entities.ts | 9 +++++---- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/common/utils/misc.ts b/src/common/utils/misc.ts index d3bb40a..7e0d63f 100644 --- a/src/common/utils/misc.ts +++ b/src/common/utils/misc.ts @@ -1,5 +1,5 @@ import { QQLevel } from '@/ntqqapi/types' -import { Dict } from 'cosmokit' +import { Dict, isNullable } from 'cosmokit' export function isNumeric(str: string) { return /^\d+$/.test(str) @@ -32,4 +32,8 @@ export function mergeNewProperties(newObj: Dict, oldObj: Dict) { } } }) -} \ No newline at end of file +} + +export function filterNullable(array: T[]) { + return array.filter(e => !isNullable(e)) as NonNullable[] +} diff --git a/src/onebot11/action/go-cqhttp/GetForwardMsg.ts b/src/onebot11/action/go-cqhttp/GetForwardMsg.ts index bc7511d..39eac8b 100644 --- a/src/onebot11/action/go-cqhttp/GetForwardMsg.ts +++ b/src/onebot11/action/go-cqhttp/GetForwardMsg.ts @@ -3,6 +3,7 @@ import { OB11ForwardMessage } from '../../types' import { OB11Entities } from '../../entities' import { ActionName } from '../types' import { MessageUnique } from '@/common/utils/messageUnique' +import { filterNullable } from '@/common/utils/misc' interface Payload { message_id: string // long msg id,gocq @@ -33,19 +34,21 @@ export class GetForwardMsg extends BaseAction { const messages = await Promise.all( msgList.map(async (msg) => { const resMsg = await OB11Entities.message(this.ctx, msg) + if (!resMsg) return resMsg.message_id = MessageUnique.createMsg({ chatType: msg.chatType, peerUid: msg.peerUid, }, msg.msgId) return resMsg - }), + }) ) - const forwardMessages = messages.map(v => { - const msg = v as Partial - msg.content = msg.message - delete msg.message - return msg as OB11ForwardMessage - }) + const forwardMessages = filterNullable(messages) + .map(v => { + const msg = v as Partial + msg.content = msg.message + delete msg.message + return msg as OB11ForwardMessage + }) return { messages: forwardMessages } } } diff --git a/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts index dd8fb82..74cf1f6 100644 --- a/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts +++ b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts @@ -5,6 +5,7 @@ import { ChatType } from '@/ntqqapi/types' import { OB11Entities } from '../../entities' import { RawMessage } from '@/ntqqapi/types' import { MessageUnique } from '@/common/utils/messageUnique' +import { filterNullable } from '@/common/utils/misc' interface Payload { group_id: number | string @@ -41,6 +42,6 @@ export class GetGroupMsgHistory extends BaseAction { }) ) const ob11MsgList = await Promise.all(msgList.map((msg) => OB11Entities.message(this.ctx, msg))) - return { messages: ob11MsgList } + return { messages: filterNullable(ob11MsgList) } } } diff --git a/src/onebot11/action/msg/GetMsg.ts b/src/onebot11/action/msg/GetMsg.ts index 1782256..02171c6 100644 --- a/src/onebot11/action/msg/GetMsg.ts +++ b/src/onebot11/action/msg/GetMsg.ts @@ -15,12 +15,12 @@ class GetMsg extends BaseAction { protected async _handle(payload: PayloadType) { if (!payload.message_id) { - throw '参数message_id不能为空' + throw new Error('参数message_id不能为空') } const msgShortId = MessageUnique.getShortIdByMsgId(payload.message_id.toString()) const msgIdWithPeer = await MessageUnique.getMsgIdAndPeerByShortId(msgShortId || +payload.message_id) if (!msgIdWithPeer) { - throw ('消息不存在') + throw new Error('消息不存在') } const peer = { guildId: '', @@ -29,6 +29,9 @@ class GetMsg extends BaseAction { } const msg = this.adapter.getMsgCache(msgIdWithPeer.MsgId) ?? (await this.ctx.ntMsgApi.getMsgsByMsgId(peer, [msgIdWithPeer.MsgId])).msgList[0] const retMsg = await OB11Entities.message(this.ctx, msg) + if (!retMsg) { + throw new Error('消息为空') + } retMsg.message_id = MessageUnique.createMsg(peer, msg.msgId)! retMsg.message_seq = retMsg.message_id retMsg.real_id = retMsg.message_id diff --git a/src/onebot11/adapter.ts b/src/onebot11/adapter.ts index 5b9fd32..d95ed0d 100644 --- a/src/onebot11/adapter.ts +++ b/src/onebot11/adapter.ts @@ -195,6 +195,9 @@ class OneBot11Adapter extends Service { OB11Entities.message(this.ctx, message) .then((msg) => { + if (!msg) { + return + } if (!this.config.debug && msg.message.length === 0) { return } @@ -440,4 +443,4 @@ namespace OneBot11Adapter { } } -export default OneBot11Adapter \ No newline at end of file +export default OneBot11Adapter diff --git a/src/onebot11/entities.ts b/src/onebot11/entities.ts index f0d2192..271b32c 100644 --- a/src/onebot11/entities.ts +++ b/src/onebot11/entities.ts @@ -49,7 +49,8 @@ import { pathToFileURL } from 'node:url' import OneBot11Adapter from './adapter' export namespace OB11Entities { - export async function message(ctx: Context, msg: RawMessage): Promise { + export async function message(ctx: Context, msg: RawMessage): Promise { + if (!msg.senderUin || msg.senderUin === '0') return //跳过空消息 const { debug, messagePostFormat, @@ -57,14 +58,14 @@ export namespace OB11Entities { const selfUin = selfInfo.uin const resMsg: OB11Message = { self_id: parseInt(selfUin), - user_id: parseInt(msg.senderUin!), + user_id: parseInt(msg.senderUin), time: parseInt(msg.msgTime) || Date.now(), message_id: msg.msgShortId!, real_id: msg.msgShortId!, message_seq: msg.msgShortId!, message_type: msg.chatType === ChatType.group ? 'group' : 'private', sender: { - user_id: parseInt(msg.senderUin!), + user_id: parseInt(msg.senderUin), nickname: msg.sendNickName, card: msg.sendMemberName ?? '', }, @@ -81,7 +82,7 @@ export namespace OB11Entities { if (msg.chatType === ChatType.group) { resMsg.sub_type = 'normal' resMsg.group_id = parseInt(msg.peerUin) - const member = await ctx.ntGroupApi.getGroupMember(msg.peerUin, msg.senderUin!) + const member = await ctx.ntGroupApi.getGroupMember(msg.peerUin, msg.senderUin) if (member) { resMsg.sender.role = groupMemberRole(member.role) resMsg.sender.nickname = member.nick