增加处理消息段时的检查,过滤无效消息段。

This commit is contained in:
123233513 2024-09-19 21:31:33 +08:00
parent 0d65e1e314
commit 3ad64b7cbb

View File

@ -421,6 +421,10 @@ export class OneBotMsgApi {
// 从face_config.json中获取表情名称 // 从face_config.json中获取表情名称
const sysFaces = faceConfig.sysface; const sysFaces = faceConfig.sysface;
const face: any = sysFaces.find((systemFace) => systemFace.QSid === parsedFaceId.toString()); 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()); parsedFaceId = parseInt(parsedFaceId.toString());
let faceType = 1; let faceType = 1;
if (parsedFaceId >= 222) { 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) => { async (element) => {
for (const key in element) { for (const key in element) {
if (keyCanBeParsed(key, this.rawToOb11Converters) && element[key]) { if (keyCanBeParsed(key, this.rawToOb11Converters) && element[key]) {
return await this.rawToOb11Converters[key]?.( const parsedElement = await this.rawToOb11Converters[key]?.(
// eslint-disable-next-line // eslint-disable-next-line
// @ts-ignore // @ts-ignore
element[key], element[key],
msg, msg,
element, element,
); );
// 对于 face 类型的消息,检查是否存在
if (key === 'faceElement' && !parsedElement) {
return null; // 如果没有找到对应的表情,返回 null
}
return parsedElement;
} }
} }
}, },
))).filter(entry => { ));
// 过滤掉无效的消息段
const validSegments = msgSegments.filter(entry => {
if (entry.status === 'fulfilled') { if (entry.status === 'fulfilled') {
return !!entry.value; return !!entry.value;
} else { } else {
this.core.context.logger.logError('消息段解析失败', entry.reason); this.core.context.logger.logError('消息段解析失败', entry.reason);
return false; return false;
} }
}).map((entry) => (<PromiseFulfilledResult<OB11MessageData>>entry).value); }).map((entry) => (<PromiseFulfilledResult<OB11MessageData>>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') { if (messagePostFormat === 'string') {
resMsg.message = msgAsCQCode; resMsg.message = msgAsCQCode;
resMsg.raw_message = msgAsCQCode; resMsg.raw_message = msgAsCQCode;
} else { } else {
resMsg.message = msgSegments; resMsg.message = validSegments;
resMsg.raw_message = msgAsCQCode; resMsg.raw_message = msgAsCQCode;
} }
return resMsg; return resMsg;