refactor: extract emoji like parsing logic

This commit is contained in:
Wesley F. Young 2024-08-28 10:50:39 +08:00
parent 3d39fd1580
commit 3a03e455c6
2 changed files with 20 additions and 7 deletions

View File

@ -112,8 +112,10 @@ export class OneBotGroupApi {
return undefined; return undefined;
} }
async parseGroupEmjioLikeEvent(GroupCode: string, grayTipElement: GrayTipElement) { async parseGroupEmojiLikeEventByGrayTip(
const NTQQMsgApi = this.core.apis.MsgApi; groupCode: string,
grayTipElement: GrayTipElement
) {
const emojiLikeData = new fastXmlParser.XMLParser({ const emojiLikeData = new fastXmlParser.XMLParser({
ignoreAttributes: false, ignoreAttributes: false,
attributeNamePrefix: '', attributeNamePrefix: '',
@ -122,16 +124,27 @@ export class OneBotGroupApi {
const senderUin = emojiLikeData.gtip.qq.jp; const senderUin = emojiLikeData.gtip.qq.jp;
const msgSeq = emojiLikeData.gtip.url.msgseq; const msgSeq = emojiLikeData.gtip.url.msgseq;
const emojiId = emojiLikeData.gtip.face.id; const emojiId = emojiLikeData.gtip.face.id;
return await this.createGroupEmojiLikeEvent(groupCode, senderUin, msgSeq, emojiId);
}
private async createGroupEmojiLikeEvent(
groupCode: string,
senderUin: string,
msgSeq: string,
emojiId: string,
) {
const peer = { const peer = {
chatType: ChatType.KCHATTYPEGROUP, chatType: ChatType.KCHATTYPEGROUP,
guildId: '', guildId: '',
peerUid: GroupCode, peerUid: groupCode,
}; };
const replyMsgList = (await NTQQMsgApi.getMsgExBySeq(peer, msgSeq)).msgList; const replyMsgList = (await this.core.apis.MsgApi.getMsgExBySeq(peer, msgSeq)).msgList;
if (replyMsgList.length < 1) { if (replyMsgList.length < 1) {
return; return;
} }
const replyMsg = replyMsgList.filter(e => e.msgSeq == msgSeq).sort((a, b) => parseInt(a.msgTime) - parseInt(b.msgTime))[0]; const replyMsg = replyMsgList
.filter(e => e.msgSeq == msgSeq)
.sort((a, b) => parseInt(a.msgTime) - parseInt(b.msgTime))[0];
//console.log("表情回应消息长度检测", msgSeq, replyMsg.elements); //console.log("表情回应消息长度检测", msgSeq, replyMsg.elements);
if (!replyMsg) { if (!replyMsg) {
this.core.context.logger.logError('解析表情回应消息失败: 未找到回应消息'); this.core.context.logger.logError('解析表情回应消息失败: 未找到回应消息');
@ -139,7 +152,7 @@ export class OneBotGroupApi {
} }
return new OB11GroupMsgEmojiLikeEvent( return new OB11GroupMsgEmojiLikeEvent(
this.core, this.core,
parseInt(GroupCode), parseInt(groupCode),
parseInt(senderUin), parseInt(senderUin),
MessageUnique.getShortIdByMsgId(replyMsg.msgId)!, MessageUnique.getShortIdByMsgId(replyMsg.msgId)!,
[{ [{

View File

@ -90,7 +90,7 @@ export async function NT2GroupEvent(core: NapCatCore, obContext: NapCatOneBot11A
} }
if (element.grayTipElement) { if (element.grayTipElement) {
if (element.grayTipElement.xmlElement?.templId === '10382') { if (element.grayTipElement.xmlElement?.templId === '10382') {
const emojiLikeEvent = await obContext.apis.GroupApi.parseGroupEmjioLikeEvent(msg.peerUid, element.grayTipElement); const emojiLikeEvent = await obContext.apis.GroupApi.parseGroupEmojiLikeEventByGrayTip(msg.peerUid, element.grayTipElement);
if (emojiLikeEvent) return emojiLikeEvent; if (emojiLikeEvent) return emojiLikeEvent;
} }
if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_XMLMSG) { if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_XMLMSG) {