From 3ad64b7cbb483975b267f090983018b7b6537e55 Mon Sep 17 00:00:00 2001 From: 123233513 <123233513@qq.com> Date: Thu, 19 Sep 2024 21:31:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=AE=B5=E6=97=B6=E7=9A=84=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=EF=BC=8C=E8=BF=87=E6=BB=A4=E6=97=A0=E6=95=88=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=AE=B5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/api/msg.ts | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index d37b4654..a9b24cb6 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -421,6 +421,10 @@ export class OneBotMsgApi { // 从face_config.json中获取表情名称 const sysFaces = faceConfig.sysface; const face: any = sysFaces.find((systemFace) => systemFace.QSid === parsedFaceId.toString()); + if (!face) { + this.core.context.logger.logError('不支持的ID', id); + return undefined; + } parsedFaceId = parseInt(parsedFaceId.toString()); let faceType = 1; if (parsedFaceId >= 222) { @@ -701,36 +705,46 @@ export class OneBotMsgApi { } } - const msgSegments = (await Promise.allSettled(msg.elements.map( + // 处理消息段 + const msgSegments = await Promise.allSettled(msg.elements.map( async (element) => { for (const key in element) { if (keyCanBeParsed(key, this.rawToOb11Converters) && element[key]) { - return await this.rawToOb11Converters[key]?.( + const parsedElement = await this.rawToOb11Converters[key]?.( // eslint-disable-next-line // @ts-ignore element[key], msg, element, ); + // 对于 face 类型的消息,检查是否存在 + if (key === 'faceElement' && !parsedElement) { + return null; // 如果没有找到对应的表情,返回 null + } + + return parsedElement; } } }, - ))).filter(entry => { + )); + + // 过滤掉无效的消息段 + const validSegments = msgSegments.filter(entry => { if (entry.status === 'fulfilled') { return !!entry.value; } else { this.core.context.logger.logError('消息段解析失败', entry.reason); return false; } - }).map((entry) => (>entry).value); + }).map((entry) => (>entry).value).filter(value => value != null); - const msgAsCQCode = msgSegments.map(msg => encodeCQCode(msg)).join('').trim(); + const msgAsCQCode = validSegments.map(msg => encodeCQCode(msg)).join('').trim(); if (messagePostFormat === 'string') { resMsg.message = msgAsCQCode; resMsg.raw_message = msgAsCQCode; } else { - resMsg.message = msgSegments; + resMsg.message = validSegments; resMsg.raw_message = msgAsCQCode; } return resMsg; From 847d88ea7756338c261e256c8a85819ea2f8fa57 Mon Sep 17 00:00:00 2001 From: 123233513 <37925595+123233513@users.noreply.github.com> Date: Thu, 19 Sep 2024 22:14:18 +0800 Subject: [PATCH 2/2] Update msg.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 处理消息段时的检查,过滤无效消息段。 --- src/onebot/api/msg.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index 07e6e28d..a9b24cb6 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -705,36 +705,46 @@ export class OneBotMsgApi { } } - const msgSegments = (await Promise.allSettled(msg.elements.map( + // 处理消息段 + const msgSegments = await Promise.allSettled(msg.elements.map( async (element) => { for (const key in element) { if (keyCanBeParsed(key, this.rawToOb11Converters) && element[key]) { - return await this.rawToOb11Converters[key]?.( + const parsedElement = await this.rawToOb11Converters[key]?.( // eslint-disable-next-line // @ts-ignore element[key], msg, element, ); + // 对于 face 类型的消息,检查是否存在 + if (key === 'faceElement' && !parsedElement) { + return null; // 如果没有找到对应的表情,返回 null + } + + return parsedElement; } } }, - ))).filter(entry => { + )); + + // 过滤掉无效的消息段 + const validSegments = msgSegments.filter(entry => { if (entry.status === 'fulfilled') { return !!entry.value; } else { this.core.context.logger.logError('消息段解析失败', entry.reason); return false; } - }).map((entry) => (>entry).value); + }).map((entry) => (>entry).value).filter(value => value != null); - const msgAsCQCode = msgSegments.map(msg => encodeCQCode(msg)).join('').trim(); + const msgAsCQCode = validSegments.map(msg => encodeCQCode(msg)).join('').trim(); if (messagePostFormat === 'string') { resMsg.message = msgAsCQCode; resMsg.raw_message = msgAsCQCode; } else { - resMsg.message = msgSegments; + resMsg.message = validSegments; resMsg.raw_message = msgAsCQCode; } return resMsg;