refactor: rename function convertMessage2List to normalize

This commit is contained in:
Wesley F. Young 2024-05-15 16:33:15 +08:00
parent bed3e1289b
commit b5dbd9d59b
4 changed files with 13 additions and 22 deletions

@ -1 +1 @@
Subproject commit b1351835673f86d445d244fd515560bfcd0b7adb Subproject commit ed10e0fc9f4b98ad2b677c8862f5cc817a5560ba

View File

@ -1,4 +1,4 @@
import SendMsg, { convertMessage2List } from '../msg/SendMsg'; import SendMsg, { normalize } from '../msg/SendMsg';
import { OB11PostSendMsg } from '../../types'; import { OB11PostSendMsg } from '../../types';
import { ActionName } from '../types'; import { ActionName } from '../types';
@ -6,7 +6,7 @@ export class GoCQHTTPSendForwardMsg extends SendMsg {
actionName = ActionName.GoCQHTTP_SendForwardMsg; actionName = ActionName.GoCQHTTP_SendForwardMsg;
protected async check(payload: OB11PostSendMsg) { protected async check(payload: OB11PostSendMsg) {
if (payload.messages) payload.message = convertMessage2List(payload.messages); if (payload.messages) payload.message = normalize(payload.messages);
return super.check(payload); return super.check(payload);
} }
} }
@ -17,4 +17,4 @@ export class GoCQHTTPSendPrivateForwardMsg extends GoCQHTTPSendForwardMsg {
export class GoCQHTTPSendGroupForwardMsg extends GoCQHTTPSendForwardMsg { export class GoCQHTTPSendGroupForwardMsg extends GoCQHTTPSendForwardMsg {
actionName = ActionName.GoCQHTTP_SendGroupForwardMsg; actionName = ActionName.GoCQHTTP_SendGroupForwardMsg;
} }

View File

@ -72,22 +72,13 @@ export interface ReturnDataType {
message_id: number; message_id: number;
} }
export function convertMessage2List(message: OB11MessageMixType, autoEscape = false) { // Normalizes a mixed type (CQCode/a single segment/segment array) into a segment array.
if (typeof message === 'string') { export function normalize(message: OB11MessageMixType, autoEscape = false): OB11MessageData[] {
if (autoEscape === true) { return typeof message === 'string' ? (
message = [{ autoEscape ?
type: OB11MessageDataType.text, [{ type: OB11MessageDataType.text, data: { text: message } }] :
data: { decodeCQCode(message)
text: message ) : Array.isArray(message) ? message : [message];
}
}];
} else {
message = decodeCQCode(message.toString());
}
} else if (!Array.isArray(message)) {
message = [message];
}
return message;
} }
export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], deleteAfterSentFiles: string[], waitComplete = true) { export async function sendMsg(peer: Peer, sendElements: SendMessageElement[], deleteAfterSentFiles: string[], waitComplete = true) {

View File

@ -7,7 +7,7 @@ import { log, logDebug, logError } from '@/common/utils/log';
import { ob11Config } from '@/onebot11/config'; import { ob11Config } from '@/onebot11/config';
import crypto from 'crypto'; import crypto from 'crypto';
import { ChatType, Group, GroupRequestOperateTypes, Peer } from '@/core/entities'; import { ChatType, Group, GroupRequestOperateTypes, Peer } from '@/core/entities';
import { convertMessage2List, createSendElements, sendMsg } from '../action/msg/SendMsg'; import { normalize, createSendElements, sendMsg } from '../action/msg/SendMsg';
import { OB11FriendRequestEvent } from '../event/request/OB11FriendRequest'; import { OB11FriendRequestEvent } from '../event/request/OB11FriendRequest';
import { OB11GroupRequestEvent } from '../event/request/OB11GroupRequest'; import { OB11GroupRequestEvent } from '../event/request/OB11GroupRequest';
import { isNull } from '@/common/utils/helper'; import { isNull } from '@/common/utils/helper';
@ -138,7 +138,7 @@ export function postOB11Event(msg: PostEventType, reportSelf = false, postWs = t
} as OB11MessageAt); } as OB11MessageAt);
} }
} }
replyMessage = replyMessage.concat(convertMessage2List(reply, resJson.auto_escape)); replyMessage = replyMessage.concat(normalize(reply, resJson.auto_escape));
const { sendElements, deleteAfterSentFiles } = await createSendElements(replyMessage, group); const { sendElements, deleteAfterSentFiles } = await createSendElements(replyMessage, group);
sendMsg(peer, sendElements, deleteAfterSentFiles, false).then(); sendMsg(peer, sendElements, deleteAfterSentFiles, false).then();
} else if (resJson.delete) { } else if (resJson.delete) {