mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
Merge pull request #301 from NapNeko/dev/RefactoredMsgParsers
refactor: make static functions dynamic
This commit is contained in:
commit
ec9e042b29
@ -101,7 +101,7 @@ class MessageUniqueWrapper {
|
|||||||
return ret.map((t) => t?.MsgId).filter((t) => t !== undefined);
|
return ret.map((t) => t?.MsgId).filter((t) => t !== undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
createMsg(peer: Peer, msgId: string): number | undefined {
|
createMsg(peer: Peer, msgId: string) {
|
||||||
const key = `${msgId}|${peer.chatType}|${peer.peerUid}`;
|
const key = `${msgId}|${peer.chatType}|${peer.peerUid}`;
|
||||||
const hash = crypto.createHash('md5').update(key).digest();
|
const hash = crypto.createHash('md5').update(key).digest();
|
||||||
//设置第一个bit为0 保证shortId为正数
|
//设置第一个bit为0 保证shortId为正数
|
||||||
|
@ -8,6 +8,11 @@ import {
|
|||||||
IMAGE_HTTP_HOST_NT,
|
IMAGE_HTTP_HOST_NT,
|
||||||
Peer,
|
Peer,
|
||||||
PicElement,
|
PicElement,
|
||||||
|
PicType,
|
||||||
|
SendFileElement,
|
||||||
|
SendPicElement,
|
||||||
|
SendPttElement,
|
||||||
|
SendVideoElement,
|
||||||
} from '@/core/entities';
|
} from '@/core/entities';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
@ -18,7 +23,12 @@ import imageSize from 'image-size';
|
|||||||
import { ISizeCalculationResult } from 'image-size/dist/types/interface';
|
import { ISizeCalculationResult } from 'image-size/dist/types/interface';
|
||||||
import { NodeIKernelSearchService } from '../services/NodeIKernelSearchService';
|
import { NodeIKernelSearchService } from '../services/NodeIKernelSearchService';
|
||||||
import { RkeyManager } from '../helper/rkey';
|
import { RkeyManager } from '../helper/rkey';
|
||||||
import { calculateFileMD5 } from '@/common/utils/file';
|
import { calculateFileMD5, isGIF } from '@/common/utils/file';
|
||||||
|
import pathLib from 'node:path';
|
||||||
|
import { defaultVideoThumbB64, getVideoInfo } from '@/common/utils/video';
|
||||||
|
import ffmpeg from 'fluent-ffmpeg';
|
||||||
|
import fsnormal from 'node:fs';
|
||||||
|
import { encodeSilk } from '@/common/utils/audio';
|
||||||
|
|
||||||
|
|
||||||
export class NTQQFileApi {
|
export class NTQQFileApi {
|
||||||
@ -84,6 +94,200 @@ export class NTQQFileApi {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async createValidSendFileElement(
|
||||||
|
filePath: string,
|
||||||
|
fileName: string = '',
|
||||||
|
folderId: string = ''
|
||||||
|
): Promise<SendFileElement> {
|
||||||
|
const { fileName: _fileName, path, fileSize } = await this.core.apis.FileApi.uploadFile(filePath, ElementType.FILE);
|
||||||
|
if (fileSize === 0) {
|
||||||
|
throw '文件异常,大小为0';
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
elementType: ElementType.FILE,
|
||||||
|
elementId: '',
|
||||||
|
fileElement: {
|
||||||
|
fileName: fileName || _fileName,
|
||||||
|
folderId: folderId,
|
||||||
|
'filePath': path!,
|
||||||
|
'fileSize': (fileSize).toString(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async createValidSendPicElement(
|
||||||
|
picPath: string,
|
||||||
|
summary: string = '',
|
||||||
|
subType: 0 | 1 = 0
|
||||||
|
): Promise<SendPicElement> {
|
||||||
|
const { md5, fileName, path, fileSize } = await this.core.apis.FileApi.uploadFile(picPath, ElementType.PIC, subType);
|
||||||
|
if (fileSize === 0) {
|
||||||
|
throw '文件异常,大小为0';
|
||||||
|
}
|
||||||
|
const imageSize = await this.core.apis.FileApi.getImageSize(picPath);
|
||||||
|
const picElement: any = {
|
||||||
|
md5HexStr: md5,
|
||||||
|
fileSize: fileSize.toString(),
|
||||||
|
picWidth: imageSize?.width,
|
||||||
|
picHeight: imageSize?.height,
|
||||||
|
fileName: fileName,
|
||||||
|
sourcePath: path,
|
||||||
|
original: true,
|
||||||
|
picType: isGIF(picPath) ? PicType.gif : PicType.jpg,
|
||||||
|
picSubType: subType,
|
||||||
|
fileUuid: '',
|
||||||
|
fileSubId: '',
|
||||||
|
thumbFileSize: 0,
|
||||||
|
summary,
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
elementType: ElementType.PIC,
|
||||||
|
elementId: '',
|
||||||
|
picElement,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async createValidSendVideoElement(
|
||||||
|
filePath: string,
|
||||||
|
fileName: string = '',
|
||||||
|
diyThumbPath: string = '',
|
||||||
|
): Promise<SendVideoElement> {
|
||||||
|
const logger = this.core.context.logger;
|
||||||
|
const { fileName: _fileName, path, fileSize, md5 } = await this.core.apis.FileApi.uploadFile(filePath, ElementType.VIDEO);
|
||||||
|
if (fileSize === 0) {
|
||||||
|
throw '文件异常,大小为0';
|
||||||
|
}
|
||||||
|
let thumb = path.replace(`${pathLib.sep}Ori${pathLib.sep}`, `${pathLib.sep}Thumb${pathLib.sep}`);
|
||||||
|
thumb = pathLib.dirname(thumb);
|
||||||
|
// log("thumb 目录", thumb)
|
||||||
|
let videoInfo = {
|
||||||
|
width: 1920, height: 1080,
|
||||||
|
time: 15,
|
||||||
|
format: 'mp4',
|
||||||
|
size: fileSize,
|
||||||
|
filePath,
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
videoInfo = await getVideoInfo(path, logger);
|
||||||
|
//logDebug('视频信息', videoInfo);
|
||||||
|
} catch (e) {
|
||||||
|
logger.logError('获取视频信息失败', e);
|
||||||
|
}
|
||||||
|
const createThumb = new Promise<string | undefined>((resolve, reject) => {
|
||||||
|
const thumbFileName = `${md5}_0.png`;
|
||||||
|
const thumbPath = pathLib.join(thumb, thumbFileName);
|
||||||
|
ffmpeg(filePath)
|
||||||
|
.on('error', (err) => {
|
||||||
|
logger.logDebug('获取视频封面失败,使用默认封面', err);
|
||||||
|
if (diyThumbPath) {
|
||||||
|
fsPromises.copyFile(diyThumbPath, thumbPath).then(() => {
|
||||||
|
resolve(thumbPath);
|
||||||
|
}).catch(reject);
|
||||||
|
} else {
|
||||||
|
fsnormal.writeFileSync(thumbPath, Buffer.from(defaultVideoThumbB64, 'base64'));
|
||||||
|
resolve(thumbPath);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.screenshots({
|
||||||
|
timestamps: [0],
|
||||||
|
filename: thumbFileName,
|
||||||
|
folder: thumb,
|
||||||
|
size: videoInfo.width + 'x' + videoInfo.height,
|
||||||
|
}).on('end', () => {
|
||||||
|
resolve(thumbPath);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const thumbPath = new Map();
|
||||||
|
const _thumbPath = await createThumb;
|
||||||
|
const thumbSize = _thumbPath ? (await fsPromises.stat(_thumbPath)).size : 0;
|
||||||
|
// log("生成缩略图", _thumbPath)
|
||||||
|
thumbPath.set(0, _thumbPath);
|
||||||
|
const thumbMd5 = _thumbPath ? await calculateFileMD5(_thumbPath) : "";
|
||||||
|
// "fileElement": {
|
||||||
|
// "fileMd5": "",
|
||||||
|
// "fileName": "1.mp4",
|
||||||
|
// "filePath": "C:\\Users\\nanae\\OneDrive\\Desktop\\1.mp4",
|
||||||
|
// "fileSize": "1847007",
|
||||||
|
// "picHeight": 1280,
|
||||||
|
// "picWidth": 720,
|
||||||
|
// "picThumbPath": {},
|
||||||
|
// "file10MMd5": "",
|
||||||
|
// "fileSha": "",
|
||||||
|
// "fileSha3": "",
|
||||||
|
// "fileUuid": "",
|
||||||
|
// "fileSubId": "",
|
||||||
|
// "thumbFileSize": 750
|
||||||
|
// }
|
||||||
|
return {
|
||||||
|
elementType: ElementType.VIDEO,
|
||||||
|
elementId: '',
|
||||||
|
videoElement: {
|
||||||
|
fileName: fileName || _fileName,
|
||||||
|
filePath: path,
|
||||||
|
videoMd5: md5,
|
||||||
|
thumbMd5,
|
||||||
|
fileTime: videoInfo.time,
|
||||||
|
thumbPath: thumbPath,
|
||||||
|
thumbSize,
|
||||||
|
thumbWidth: videoInfo.width,
|
||||||
|
thumbHeight: videoInfo.height,
|
||||||
|
fileSize: '' + fileSize,
|
||||||
|
// fileFormat: videotype
|
||||||
|
// fileUuid: "",
|
||||||
|
// transferStatus: 0,
|
||||||
|
// progress: 0,
|
||||||
|
// invalidState: 0,
|
||||||
|
// fileSubId: "",
|
||||||
|
// fileBizId: null,
|
||||||
|
// originVideoMd5: "",
|
||||||
|
// fileFormat: 2,
|
||||||
|
// import_rich_media_context: null,
|
||||||
|
// sourceVideoCodecFormat: 2
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async createValidSendPttElement(pttPath: string): Promise<SendPttElement> {
|
||||||
|
const {
|
||||||
|
converted,
|
||||||
|
path: silkPath,
|
||||||
|
duration,
|
||||||
|
} = await encodeSilk(pttPath, this.core.NapCatTempPath, this.core.context.logger);
|
||||||
|
// log("生成语音", silkPath, duration);
|
||||||
|
if (!silkPath) {
|
||||||
|
throw '语音转换失败, 请检查语音文件是否正常';
|
||||||
|
}
|
||||||
|
const { md5, fileName, path, fileSize } = await this.core.apis.FileApi.uploadFile(silkPath!, ElementType.PTT);
|
||||||
|
if (fileSize === 0) {
|
||||||
|
throw '文件异常,大小为0';
|
||||||
|
}
|
||||||
|
if (converted) {
|
||||||
|
fsPromises.unlink(silkPath);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
elementType: ElementType.PTT,
|
||||||
|
elementId: '',
|
||||||
|
pttElement: {
|
||||||
|
fileName: fileName,
|
||||||
|
filePath: path,
|
||||||
|
md5HexStr: md5,
|
||||||
|
fileSize: fileSize,
|
||||||
|
// duration: Math.max(1, Math.round(fileSize / 1024 / 3)), // 一秒钟大概是3kb大小, 小于1秒的按1秒算
|
||||||
|
duration: duration || 1,
|
||||||
|
formatType: 1,
|
||||||
|
voiceType: 1,
|
||||||
|
voiceChangeType: 0,
|
||||||
|
canConvert2Text: true,
|
||||||
|
waveAmplitudes: [
|
||||||
|
0, 18, 9, 23, 16, 17, 16, 15, 44, 17, 24, 20, 14, 15, 17,
|
||||||
|
],
|
||||||
|
fileSubId: '',
|
||||||
|
playState: 1,
|
||||||
|
autoConvertText: 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async downloadMediaByUuid() {
|
async downloadMediaByUuid() {
|
||||||
//napCatCore.session.getRichMediaService().downloadFileForFileUuid();
|
//napCatCore.session.getRichMediaService().downloadFileForFileUuid();
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
import BaseAction from '../BaseAction';
|
import BaseAction from '../BaseAction';
|
||||||
import { ActionName } from '../types';
|
import { ActionName } from '../types';
|
||||||
import { ChatType, SendFileElement } from '@/core/entities';
|
import { ChatType } from '@/core/entities';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { sendMsg } from '@/onebot/action/msg/SendMsg';
|
import { sendMsg } from '@/onebot/action/msg/SendMsg';
|
||||||
import { uri2local } from '@/common/utils/file';
|
import { uri2local } from '@/common/utils/file';
|
||||||
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||||
import { SendMsgElementConstructor } from '@/onebot/helper/genMessage';
|
|
||||||
|
|
||||||
const SchemaData = {
|
const SchemaData = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
@ -34,7 +33,7 @@ export default class GoCQHTTPUploadGroupFile extends BaseAction<Payload, null> {
|
|||||||
if (!downloadResult.success) {
|
if (!downloadResult.success) {
|
||||||
throw new Error(downloadResult.errMsg);
|
throw new Error(downloadResult.errMsg);
|
||||||
}
|
}
|
||||||
const sendFileEle: SendFileElement = await SendMsgElementConstructor.file(this.CoreContext, downloadResult.path, payload.name, payload.folder_id);
|
const sendFileEle = await this.CoreContext.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name, payload.folder_id);
|
||||||
await sendMsg(this.CoreContext, {
|
await sendMsg(this.CoreContext, {
|
||||||
chatType: ChatType.KCHATTYPEGROUP,
|
chatType: ChatType.KCHATTYPEGROUP,
|
||||||
peerUid: payload.group_id.toString(),
|
peerUid: payload.group_id.toString(),
|
||||||
|
@ -5,7 +5,6 @@ import fs from 'fs';
|
|||||||
import { sendMsg } from '@/onebot/action/msg/SendMsg';
|
import { sendMsg } from '@/onebot/action/msg/SendMsg';
|
||||||
import { uri2local } from '@/common/utils/file';
|
import { uri2local } from '@/common/utils/file';
|
||||||
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
|
||||||
import { SendMsgElementConstructor } from '@/onebot/helper/genMessage';
|
|
||||||
|
|
||||||
const SchemaData = {
|
const SchemaData = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
@ -47,7 +46,7 @@ export default class GoCQHTTPUploadPrivateFile extends BaseAction<Payload, null>
|
|||||||
if (!downloadResult.success) {
|
if (!downloadResult.success) {
|
||||||
throw new Error(downloadResult.errMsg);
|
throw new Error(downloadResult.errMsg);
|
||||||
}
|
}
|
||||||
const sendFileEle: SendFileElement = await SendMsgElementConstructor.file(this.CoreContext, downloadResult.path, payload.name);
|
const sendFileEle: SendFileElement = await this.CoreContext.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name);
|
||||||
await sendMsg(this.CoreContext, peer, [sendFileEle], [], true);
|
await sendMsg(this.CoreContext, peer, [sendFileEle], [], true);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
@ -71,7 +71,7 @@ import GetRecentContact from './user/GetRecentContact';
|
|||||||
import { GetProfileLike } from './extends/GetProfileLike';
|
import { GetProfileLike } from './extends/GetProfileLike';
|
||||||
import SetGroupPortrait from './go-cqhttp/SetGroupPortrait';
|
import SetGroupPortrait from './go-cqhttp/SetGroupPortrait';
|
||||||
import { FetchCustomFace } from './extends/FetchCustomFace';
|
import { FetchCustomFace } from './extends/FetchCustomFace';
|
||||||
import GoCQHTTPUploadPrivateFile from './go-cqhttp/UploadPrivareFile';
|
import GoCQHTTPUploadPrivateFile from './go-cqhttp/UploadPrivateFile';
|
||||||
import { FetchEmojiLike } from './extends/FetchEmojiLike';
|
import { FetchEmojiLike } from './extends/FetchEmojiLike';
|
||||||
import { NapCatCore } from '@/core';
|
import { NapCatCore } from '@/core';
|
||||||
|
|
||||||
|
@ -9,11 +9,9 @@ import { ActionName, BaseCheckResult } from '@/onebot/action/types';
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import fsPromise from 'node:fs/promises';
|
import fsPromise from 'node:fs/promises';
|
||||||
import { decodeCQCode } from '@/onebot/helper/cqcode';
|
import { decodeCQCode } from '@/onebot/helper/cqcode';
|
||||||
import createSendElements from './create-send-elements';
|
|
||||||
import { MessageUnique } from '@/common/utils/MessageUnique';
|
import { MessageUnique } from '@/common/utils/MessageUnique';
|
||||||
import { ChatType, ElementType, NapCatCore, Peer, SendMessageElement } from '@/core';
|
import { ChatType, ElementType, NapCatCore, Peer, RawMessage, SendMessageElement } from '@/core';
|
||||||
import BaseAction from '../../BaseAction';
|
import BaseAction from '../BaseAction';
|
||||||
import { handleForwardNode } from './handle-forward-node';
|
|
||||||
|
|
||||||
export interface ReturnDataType {
|
export interface ReturnDataType {
|
||||||
message_id: number;
|
message_id: number;
|
||||||
@ -34,8 +32,6 @@ export function normalize(message: OB11MessageMixType, autoEscape = false): OB11
|
|||||||
) : Array.isArray(message) ? message : [message];
|
) : Array.isArray(message) ? message : [message];
|
||||||
}
|
}
|
||||||
|
|
||||||
export { createSendElements };
|
|
||||||
|
|
||||||
export async function sendMsg(coreContext: NapCatCore, peer: Peer, sendElements: SendMessageElement[], deleteAfterSentFiles: string[], waitComplete = true) {
|
export async function sendMsg(coreContext: NapCatCore, peer: Peer, sendElements: SendMessageElement[], deleteAfterSentFiles: string[], waitComplete = true) {
|
||||||
const NTQQMsgApi = coreContext.apis.MsgApi;
|
const NTQQMsgApi = coreContext.apis.MsgApi;
|
||||||
const logger = coreContext.context.logger;
|
const logger = coreContext.context.logger;
|
||||||
@ -144,9 +140,6 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
|||||||
contextMode = ContextMode.Normal;
|
contextMode = ContextMode.Normal;
|
||||||
|
|
||||||
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
|
protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
|
||||||
const NTQQGroupApi = this.CoreContext.apis.GroupApi;
|
|
||||||
const NTQQFriendApi = this.CoreContext.apis.FriendApi;
|
|
||||||
const NTQQUserApi = this.CoreContext.apis.UserApi;
|
|
||||||
const messages = normalize(payload.message);
|
const messages = normalize(payload.message);
|
||||||
const nodeElementLength = getSpecialMsgNum(payload, OB11MessageDataType.node);
|
const nodeElementLength = getSpecialMsgNum(payload, OB11MessageDataType.node);
|
||||||
if (nodeElementLength > 0 && nodeElementLength != messages.length) {
|
if (nodeElementLength > 0 && nodeElementLength != messages.length) {
|
||||||
@ -156,9 +149,9 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (payload.user_id && payload.message_type !== 'group') {
|
if (payload.user_id && payload.message_type !== 'group') {
|
||||||
const uid = await NTQQUserApi.getUidByUinV2(payload.user_id.toString());
|
// const uid = await this.CoreContext.apis.UserApi.getUidByUinV2(payload.user_id.toString());
|
||||||
const isBuddy = await NTQQFriendApi.isBuddy(uid!);
|
// const isBuddy = await NTQQFriendApi.isBuddy(uid!);
|
||||||
//if (!isBuddy) { }
|
// if (!isBuddy) { }
|
||||||
}
|
}
|
||||||
return { valid: true };
|
return { valid: true };
|
||||||
}
|
}
|
||||||
@ -174,7 +167,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (getSpecialMsgNum(payload, OB11MessageDataType.node)) {
|
if (getSpecialMsgNum(payload, OB11MessageDataType.node)) {
|
||||||
const returnMsg = await handleForwardNode(this.CoreContext, this.OneBotContext, peer, messages as OB11MessageNode[]);
|
const returnMsg = await this.handleForwardedNodes(peer, messages as OB11MessageNode[]);
|
||||||
if (returnMsg) {
|
if (returnMsg) {
|
||||||
const msgShortId = MessageUnique.createMsg({
|
const msgShortId = MessageUnique.createMsg({
|
||||||
guildId: '',
|
guildId: '',
|
||||||
@ -194,11 +187,137 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
|
|||||||
}
|
}
|
||||||
// log("send msg:", peer, sendElements)
|
// log("send msg:", peer, sendElements)
|
||||||
|
|
||||||
const { sendElements, deleteAfterSentFiles } = await createSendElements(this.CoreContext, this.OneBotContext, messages, peer);
|
const { sendElements, deleteAfterSentFiles } = await this.OneBotContext.apiContext.MsgApi
|
||||||
//console.log(peer, JSON.stringify(sendElements,null,2));
|
.createSendElements(messages, peer);
|
||||||
const returnMsg = await sendMsg(this.CoreContext, peer, sendElements, deleteAfterSentFiles);
|
const returnMsg = await sendMsg(this.CoreContext, peer, sendElements, deleteAfterSentFiles);
|
||||||
return { message_id: returnMsg!.id! };
|
return { message_id: returnMsg!.id! };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async handleForwardedNodes(destPeer: Peer, messageNodes: OB11MessageNode[]): Promise<RawMessage | null> {
|
||||||
|
const NTQQMsgApi = this.CoreContext.apis.MsgApi;
|
||||||
|
const selfPeer = {
|
||||||
|
chatType: ChatType.KCHATTYPEC2C,
|
||||||
|
peerUid: this.CoreContext.selfInfo.uid,
|
||||||
|
};
|
||||||
|
let nodeMsgIds: string[] = [];
|
||||||
|
const logger = this.CoreContext.context.logger;
|
||||||
|
for (const messageNode of messageNodes) {
|
||||||
|
const nodeId = messageNode.data.id;
|
||||||
|
if (nodeId) {
|
||||||
|
//对Mgsid和OB11ID混用情况兜底
|
||||||
|
const nodeMsg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(nodeId)) || MessageUnique.getPeerByMsgId(nodeId);
|
||||||
|
if (!nodeMsg) {
|
||||||
|
logger.logError('转发消息失败,未找到消息', nodeId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
nodeMsgIds.push(nodeMsg.MsgId);
|
||||||
|
} else {
|
||||||
|
// 自定义的消息
|
||||||
|
try {
|
||||||
|
const OB11Data = normalize(messageNode.data.content);
|
||||||
|
//筛选node消息
|
||||||
|
const isNodeMsg = OB11Data.filter(e => e.type === OB11MessageDataType.node).length;//找到子转发消息
|
||||||
|
if (isNodeMsg !== 0) {
|
||||||
|
if (isNodeMsg !== OB11Data.length) {
|
||||||
|
logger.logError('子消息中包含非node消息 跳过不合法部分');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const nodeMsg = await this.handleForwardedNodes(selfPeer, OB11Data.filter(e => e.type === OB11MessageDataType.node));
|
||||||
|
if (nodeMsg) {
|
||||||
|
nodeMsgIds.push(nodeMsg.msgId);
|
||||||
|
MessageUnique.createMsg(selfPeer, nodeMsg.msgId);
|
||||||
|
}
|
||||||
|
//完成子卡片生成跳过后续
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const { sendElements } = await this.OneBotContext.apiContext.MsgApi
|
||||||
|
.createSendElements(OB11Data, destPeer);
|
||||||
|
//拆分消息
|
||||||
|
const MixElement = sendElements.filter(element => element.elementType !== ElementType.FILE && element.elementType !== ElementType.VIDEO);
|
||||||
|
const SingleElement = sendElements.filter(element => element.elementType === ElementType.FILE || element.elementType === ElementType.VIDEO).map(e => [e]);
|
||||||
|
const AllElement: SendMessageElement[][] = [MixElement, ...SingleElement].filter(e => e !== undefined && e.length !== 0);
|
||||||
|
const MsgNodeList: Promise<RawMessage | undefined>[] = [];
|
||||||
|
for (const sendElementsSplitElement of AllElement) {
|
||||||
|
MsgNodeList.push(sendMsg(this.CoreContext, selfPeer, sendElementsSplitElement, [], true).catch(_ => new Promise((resolve) => {
|
||||||
|
resolve(undefined);
|
||||||
|
})));
|
||||||
|
}
|
||||||
|
(await Promise.allSettled(MsgNodeList)).map((result) => {
|
||||||
|
if (result.status === 'fulfilled' && result.value) {
|
||||||
|
nodeMsgIds.push(result.value.msgId);
|
||||||
|
MessageUnique.createMsg(selfPeer, result.value.msgId);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
logger.logDebug('生成转发消息节点失败', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const nodeMsgArray: Array<RawMessage> = [];
|
||||||
|
let srcPeer: Peer | undefined = undefined;
|
||||||
|
let needSendSelf = false;
|
||||||
|
//检测是否处于同一个Peer 不在同一个peer则全部消息由自身发送
|
||||||
|
for (const msgId of nodeMsgIds) {
|
||||||
|
const nodeMsgPeer = MessageUnique.getPeerByMsgId(msgId);
|
||||||
|
if (!nodeMsgPeer) {
|
||||||
|
logger.logError('转发消息失败,未找到消息', msgId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const nodeMsg = (await NTQQMsgApi.getMsgsByMsgId(nodeMsgPeer.Peer, [msgId])).msgList[0];
|
||||||
|
srcPeer = srcPeer ?? { chatType: nodeMsg.chatType, peerUid: nodeMsg.peerUid };
|
||||||
|
if (srcPeer.peerUid !== nodeMsg.peerUid) {
|
||||||
|
needSendSelf = true;
|
||||||
|
}
|
||||||
|
nodeMsgArray.push(nodeMsg);
|
||||||
|
}
|
||||||
|
nodeMsgIds = nodeMsgArray.map(msg => msg.msgId);
|
||||||
|
let retMsgIds: string[] = [];
|
||||||
|
if (needSendSelf) {
|
||||||
|
for (const [, msg] of nodeMsgArray.entries()) {
|
||||||
|
if (msg.peerUid === this.CoreContext.selfInfo.uid){
|
||||||
|
retMsgIds.push(msg.msgId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const ClonedMsg = await this.cloneMsg(msg);
|
||||||
|
if (ClonedMsg) retMsgIds.push(ClonedMsg.msgId);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
retMsgIds = nodeMsgIds;
|
||||||
|
}
|
||||||
|
if (retMsgIds.length === 0) throw Error('转发消息失败,生成节点为空');
|
||||||
|
try {
|
||||||
|
logger.logDebug('开发转发', srcPeer, destPeer, retMsgIds);
|
||||||
|
return await NTQQMsgApi.multiForwardMsg(srcPeer!, destPeer, retMsgIds);
|
||||||
|
} catch (e) {
|
||||||
|
logger.logError('forward failed', e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async cloneMsg(msg: RawMessage): Promise<RawMessage | undefined> {
|
||||||
|
const selfPeer = {
|
||||||
|
chatType: ChatType.KCHATTYPEC2C,
|
||||||
|
peerUid: this.CoreContext.selfInfo.uid,
|
||||||
|
};
|
||||||
|
const logger = this.CoreContext.context.logger;
|
||||||
|
const NTQQMsgApi = this.CoreContext.apis.MsgApi;
|
||||||
|
//logDebug('克隆的目标消息', msg);
|
||||||
|
|
||||||
|
const sendElements: SendMessageElement[] = [];
|
||||||
|
|
||||||
|
for (const element of msg.elements) {
|
||||||
|
sendElements.push(element as SendMessageElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sendElements.length === 0) {
|
||||||
|
logger.logDebug('需要clone的消息无法解析,将会忽略掉', msg);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return await NTQQMsgApi.sendMsg(selfPeer, sendElements, true);
|
||||||
|
} catch (e) {
|
||||||
|
logger.logError(e, '克隆转发消息失败,将忽略本条消息', msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SendMsg;
|
export default SendMsg;
|
@ -1,36 +0,0 @@
|
|||||||
import { OB11MessageData } from '@/onebot/types';
|
|
||||||
|
|
||||||
function checkSendMessage(sendMsgList: OB11MessageData[]) {
|
|
||||||
function checkUri(uri: string): boolean {
|
|
||||||
const pattern = /^(file:\/\/|http:\/\/|https:\/\/|base64:\/\/)/;
|
|
||||||
return pattern.test(uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const msg of sendMsgList) {
|
|
||||||
if (msg['type'] && msg['data']) {
|
|
||||||
const type = msg['type'];
|
|
||||||
const data = msg['data'];
|
|
||||||
if (type === 'text' && !data['text']) {
|
|
||||||
return 400;
|
|
||||||
} else if (['image', 'voice', 'record'].includes(type)) {
|
|
||||||
if (!data['file']) {
|
|
||||||
return 400;
|
|
||||||
} else {
|
|
||||||
if (checkUri(data['file'])) {
|
|
||||||
return 200;
|
|
||||||
} else {
|
|
||||||
return 400;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (type === 'at' && !data['qq']) {
|
|
||||||
return 400;
|
|
||||||
} else if (type === 'reply' && !data['id']) {
|
|
||||||
return 400;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return 400;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 200;
|
|
||||||
}
|
|
@ -1,262 +0,0 @@
|
|||||||
import { OB11MessageData, OB11MessageDataType, OB11MessageFileBase } from '@/onebot/types';
|
|
||||||
import { uri2local } from '@/common/utils/file';
|
|
||||||
import { RequestUtil } from '@/common/utils/request';
|
|
||||||
import { MessageUnique } from '@/common/utils/MessageUnique';
|
|
||||||
import { AtType, ChatType, CustomMusicSignPostData, IdMusicSignPostData, NapCatCore, Peer, SendMessageElement } from '@/core';
|
|
||||||
import { SendMsgElementConstructor } from '@/onebot/helper/genMessage';
|
|
||||||
import { NapCatOneBot11Adapter } from '@/onebot';
|
|
||||||
|
|
||||||
export type MessageContext = {
|
|
||||||
deleteAfterSentFiles: string[],
|
|
||||||
peer: Peer
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleOb11FileLikeMessage(
|
|
||||||
coreContext: NapCatCore,
|
|
||||||
obContext: NapCatOneBot11Adapter,
|
|
||||||
{ data: inputdata }: OB11MessageFileBase,
|
|
||||||
{ deleteAfterSentFiles }: MessageContext,
|
|
||||||
) {
|
|
||||||
//inputdata?.url || inputdata.file
|
|
||||||
const isBlankUrl = !inputdata.url || inputdata.url === '';
|
|
||||||
const isBlankFile = !inputdata.file || inputdata.file === '';
|
|
||||||
if (isBlankUrl && isBlankFile) {
|
|
||||||
coreContext.context.logger.logError('文件消息缺少参数', inputdata);
|
|
||||||
throw Error('文件消息缺少参数');
|
|
||||||
}
|
|
||||||
const fileOrUrl = (isBlankUrl ? inputdata.file : inputdata.url) || "";
|
|
||||||
const {
|
|
||||||
path,
|
|
||||||
isLocal,
|
|
||||||
fileName,
|
|
||||||
errMsg,
|
|
||||||
success,
|
|
||||||
} = (await uri2local(coreContext.NapCatTempPath, fileOrUrl));
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
coreContext.context.logger.logError('文件下载失败', errMsg);
|
|
||||||
throw Error('文件下载失败' + errMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isLocal) { // 只删除http和base64转过来的文件
|
|
||||||
deleteAfterSentFiles.push(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
return { path, fileName: inputdata.name || fileName };
|
|
||||||
}
|
|
||||||
|
|
||||||
const _handlers: {
|
|
||||||
[Key in OB11MessageDataType]: (
|
|
||||||
CoreContext: NapCatCore,
|
|
||||||
obContext: NapCatOneBot11Adapter,
|
|
||||||
sendMsg: Extract<OB11MessageData, { type: Key }>,
|
|
||||||
// This picks the correct message type out
|
|
||||||
// How great the type system of TypeScript is!
|
|
||||||
context: MessageContext,
|
|
||||||
) => Promise<SendMessageElement | undefined>
|
|
||||||
} = {
|
|
||||||
[OB11MessageDataType.text]: async (coreContext, obContext: NapCatOneBot11Adapter, { data: { text } }) => SendMsgElementConstructor.text(coreContext, text),
|
|
||||||
|
|
||||||
[OB11MessageDataType.at]: async (coreContext, obContext: NapCatOneBot11Adapter, { data: { qq: atQQ } }, context) => {
|
|
||||||
if (!context.peer || context.peer.chatType == ChatType.KCHATTYPEC2C) return undefined;
|
|
||||||
if (atQQ === 'all') return SendMsgElementConstructor.at(coreContext, atQQ, atQQ, AtType.atAll, '全体成员');
|
|
||||||
const NTQQGroupApi = coreContext.apis.GroupApi;
|
|
||||||
const NTQQUserApi = coreContext.apis.UserApi;
|
|
||||||
const atMember = await NTQQGroupApi.getGroupMember(context.peer.peerUid, atQQ);
|
|
||||||
if (atMember) {
|
|
||||||
return SendMsgElementConstructor.at(coreContext, atQQ, atMember.uid, AtType.atUser, atMember.nick || atMember.cardName);
|
|
||||||
}
|
|
||||||
const uid = await NTQQUserApi.getUidByUinV2(`${atQQ}`);
|
|
||||||
if (!uid) throw new Error('Get Uid Error');
|
|
||||||
const info = await NTQQUserApi.getUserDetailInfo(uid);
|
|
||||||
return SendMsgElementConstructor.at(coreContext, atQQ, uid, AtType.atUser, info.nick || '');
|
|
||||||
},
|
|
||||||
[OB11MessageDataType.reply]: async (coreContext, obContext: NapCatOneBot11Adapter, { data: { id } }) => {
|
|
||||||
const replyMsgM = MessageUnique.getMsgIdAndPeerByShortId(parseInt(id));
|
|
||||||
if (!replyMsgM) {
|
|
||||||
coreContext.context.logger.logWarn('回复消息不存在', id);
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const NTQQMsgApi = coreContext.apis.MsgApi;
|
|
||||||
const replyMsg = (await NTQQMsgApi.getMsgsByMsgId(
|
|
||||||
replyMsgM.Peer, [replyMsgM.MsgId!])).msgList[0];
|
|
||||||
return replyMsg ?
|
|
||||||
SendMsgElementConstructor.reply(coreContext, replyMsg.msgSeq, replyMsg.msgId, replyMsg.senderUin!, replyMsg.senderUin!) :
|
|
||||||
undefined;
|
|
||||||
},
|
|
||||||
|
|
||||||
[OB11MessageDataType.face]: async (coreContext, obContext: NapCatOneBot11Adapter, { data: { id } }) => SendMsgElementConstructor.face(coreContext, parseInt(id)),
|
|
||||||
|
|
||||||
[OB11MessageDataType.mface]: async (coreContext, obContext: NapCatOneBot11Adapter, {
|
|
||||||
data: {
|
|
||||||
emoji_package_id, emoji_id, key, summary,
|
|
||||||
},
|
|
||||||
}) => SendMsgElementConstructor.mface(coreContext, emoji_package_id, emoji_id, key, summary),
|
|
||||||
|
|
||||||
// File service
|
|
||||||
[OB11MessageDataType.image]: async (coreContext, obContext: NapCatOneBot11Adapter, sendMsg, context) => {
|
|
||||||
const PicEle = await SendMsgElementConstructor.pic(
|
|
||||||
coreContext,
|
|
||||||
(await handleOb11FileLikeMessage(coreContext, obContext, sendMsg, context)).path,
|
|
||||||
sendMsg.data.summary || '',
|
|
||||||
sendMsg.data.subType || 0,
|
|
||||||
);
|
|
||||||
context.deleteAfterSentFiles.push(PicEle.picElement.sourcePath);
|
|
||||||
return PicEle;
|
|
||||||
}, // currently not supported
|
|
||||||
[OB11MessageDataType.file]: async (coreContext, obContext: NapCatOneBot11Adapter, sendMsg, context) => {
|
|
||||||
const { path, fileName } = await handleOb11FileLikeMessage(coreContext, obContext, sendMsg, context);
|
|
||||||
//logDebug('发送文件', path, fileName);
|
|
||||||
const FileEle = await SendMsgElementConstructor.file(coreContext, path, fileName);
|
|
||||||
// 清除Upload的应该
|
|
||||||
// context.deleteAfterSentFiles.push(fileName || FileEle.fileElement.filePath);
|
|
||||||
return FileEle;
|
|
||||||
},
|
|
||||||
|
|
||||||
[OB11MessageDataType.video]: async (coreContext, obContext, sendMsg, context) => {
|
|
||||||
const { path, fileName } = await handleOb11FileLikeMessage(coreContext, obContext, sendMsg, context);
|
|
||||||
|
|
||||||
//logDebug('发送视频', path, fileName);
|
|
||||||
let thumb = sendMsg.data.thumb;
|
|
||||||
if (thumb) {
|
|
||||||
const uri2LocalRes = await uri2local(coreContext.NapCatTempPath, thumb);
|
|
||||||
if (uri2LocalRes.success) thumb = uri2LocalRes.path;
|
|
||||||
}
|
|
||||||
const videoEle = await SendMsgElementConstructor.video(coreContext, path, fileName, thumb);
|
|
||||||
//未测试
|
|
||||||
context.deleteAfterSentFiles.push(videoEle.videoElement.filePath);
|
|
||||||
return videoEle;
|
|
||||||
},
|
|
||||||
|
|
||||||
[OB11MessageDataType.voice]: async (coreContext, obContext: NapCatOneBot11Adapter, sendMsg, context) => SendMsgElementConstructor.ptt(coreContext, (await handleOb11FileLikeMessage(coreContext, obContext, sendMsg, context)).path),
|
|
||||||
|
|
||||||
[OB11MessageDataType.json]: async (coreContext, obContext: NapCatOneBot11Adapter, { data: { data } }) => SendMsgElementConstructor.ark(coreContext, data),
|
|
||||||
|
|
||||||
[OB11MessageDataType.dice]: async (coreContext, obContext: NapCatOneBot11Adapter, { data: { result } }) => SendMsgElementConstructor.dice(coreContext, result),
|
|
||||||
|
|
||||||
[OB11MessageDataType.RPS]: async (coreContext, obContext: NapCatOneBot11Adapter, { data: { result } }) => SendMsgElementConstructor.rps(coreContext, result),
|
|
||||||
|
|
||||||
[OB11MessageDataType.markdown]: async (coreContext, obContext: NapCatOneBot11Adapter, { data: { content } }) => SendMsgElementConstructor.markdown(coreContext, content),
|
|
||||||
|
|
||||||
[OB11MessageDataType.music]: async (coreContext, obContext: NapCatOneBot11Adapter, { data }) => {
|
|
||||||
// 保留, 直到...找到更好的解决方案
|
|
||||||
if (data.type === 'custom') {
|
|
||||||
if (!data.url) {
|
|
||||||
coreContext.context.logger.logError('自定义音卡缺少参数url');
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (!data.audio) {
|
|
||||||
coreContext.context.logger.logError('自定义音卡缺少参数audio');
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (!data.title) {
|
|
||||||
coreContext.context.logger.logError('自定义音卡缺少参数title');
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!['qq', '163'].includes(data.type)) {
|
|
||||||
coreContext.context.logger.logError('音乐卡片type错误, 只支持qq、163、custom,当前type:', data.type);
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
if (!data.id) {
|
|
||||||
coreContext.context.logger.logError('音乐卡片缺少参数id');
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let postData: IdMusicSignPostData | CustomMusicSignPostData;
|
|
||||||
if (data.type === 'custom' && data.content) {
|
|
||||||
const { content, ...others } = data;
|
|
||||||
postData = { singer: content, ...others };
|
|
||||||
} else {
|
|
||||||
postData = data;
|
|
||||||
}
|
|
||||||
// Mlikiowa V2.2.8 Refactor Todo
|
|
||||||
const signUrl = obContext.configLoader.configData.musicSignUrl;
|
|
||||||
if (!signUrl) {
|
|
||||||
if (data.type === 'qq') {
|
|
||||||
//const musicJson = (await SignMusicWrapper(data.id.toString())).data.arkResult.slice(0, -1);
|
|
||||||
//return SendMsgElementConstructor.ark(musicJson);
|
|
||||||
}
|
|
||||||
throw Error('音乐消息签名地址未配置');
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const musicJson = await RequestUtil.HttpGetJson<any>(signUrl, 'POST', postData);
|
|
||||||
return SendMsgElementConstructor.ark(coreContext, musicJson);
|
|
||||||
} catch (e) {
|
|
||||||
coreContext.context.logger.logError('生成音乐消息失败', e);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
[OB11MessageDataType.node]: async (coreContext, obContext: NapCatOneBot11Adapter) => undefined,
|
|
||||||
|
|
||||||
[OB11MessageDataType.forward]: async (coreContext, obContext: NapCatOneBot11Adapter) => undefined,
|
|
||||||
|
|
||||||
[OB11MessageDataType.xml]: async (coreContext, obContext: NapCatOneBot11Adapter) => undefined,
|
|
||||||
|
|
||||||
[OB11MessageDataType.poke]: async (coreContext, obContext: NapCatOneBot11Adapter) => undefined,
|
|
||||||
|
|
||||||
[OB11MessageDataType.Location]: async (coreContext, obContext: NapCatOneBot11Adapter) => {
|
|
||||||
return SendMsgElementConstructor.location(coreContext);
|
|
||||||
},
|
|
||||||
[OB11MessageDataType.miniapp]: function (CoreContext: NapCatCore, obContext: NapCatOneBot11Adapter, sendMsg: never, context: MessageContext): Promise<SendMessageElement | undefined> {
|
|
||||||
throw new Error('Function not implemented.');
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlers = <{
|
|
||||||
[Key in OB11MessageDataType]: (
|
|
||||||
coreContext: NapCatCore,
|
|
||||||
obContext: NapCatOneBot11Adapter,
|
|
||||||
sendMsg: OB11MessageData,
|
|
||||||
context: MessageContext,
|
|
||||||
) => Promise<SendMessageElement | undefined>
|
|
||||||
}>_handlers;
|
|
||||||
|
|
||||||
export default async function createSendElements(
|
|
||||||
CoreContext: NapCatCore,
|
|
||||||
obContext: NapCatOneBot11Adapter,
|
|
||||||
messageData: OB11MessageData[],
|
|
||||||
peer: Peer,
|
|
||||||
ignoreTypes: OB11MessageDataType[] = [],
|
|
||||||
) {
|
|
||||||
const deleteAfterSentFiles: string[] = [];
|
|
||||||
const callResultList: Array<Promise<SendMessageElement | undefined>> = [];
|
|
||||||
for (const sendMsg of messageData) {
|
|
||||||
if (ignoreTypes.includes(sendMsg.type)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const callResult = handlers[sendMsg.type](
|
|
||||||
CoreContext,
|
|
||||||
obContext,
|
|
||||||
sendMsg,
|
|
||||||
{ peer, deleteAfterSentFiles },
|
|
||||||
)?.catch(undefined);
|
|
||||||
callResultList.push(callResult);
|
|
||||||
}
|
|
||||||
const ret = await Promise.all(callResultList);
|
|
||||||
const sendElements: SendMessageElement[] = ret.filter(ele => !!ele);
|
|
||||||
return { sendElements, deleteAfterSentFiles };
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createSendElementsParallel(
|
|
||||||
CoreContext: NapCatCore,
|
|
||||||
obContext: NapCatOneBot11Adapter,
|
|
||||||
messageData: OB11MessageData[],
|
|
||||||
peer: Peer,
|
|
||||||
ignoreTypes: OB11MessageDataType[] = [],
|
|
||||||
) {
|
|
||||||
const deleteAfterSentFiles: string[] = [];
|
|
||||||
const sendElements = <SendMessageElement[]>(
|
|
||||||
await Promise.all(
|
|
||||||
messageData.map(async sendMsg => ignoreTypes.includes(sendMsg.type) ?
|
|
||||||
undefined :
|
|
||||||
handlers[sendMsg.type](CoreContext, obContext, sendMsg, { peer, deleteAfterSentFiles })),
|
|
||||||
).then(
|
|
||||||
results => results.filter(
|
|
||||||
element => element !== undefined,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
return { sendElements, deleteAfterSentFiles };
|
|
||||||
}
|
|
@ -1,132 +0,0 @@
|
|||||||
import { ChatType, ElementType, NapCatCore, Peer, RawMessage, SendMessageElement } from '@/core';
|
|
||||||
import { MessageUnique } from '@/common/utils/MessageUnique';
|
|
||||||
import { OB11MessageDataType, OB11MessageNode } from '@/onebot/types';
|
|
||||||
import createSendElements from './create-send-elements';
|
|
||||||
import { normalize, sendMsg } from '../SendMsg/index';
|
|
||||||
import { NapCatOneBot11Adapter } from '@/onebot';
|
|
||||||
|
|
||||||
async function cloneMsg(coreContext: NapCatCore, msg: RawMessage): Promise<RawMessage | undefined> {
|
|
||||||
const selfPeer = {
|
|
||||||
chatType: ChatType.KCHATTYPEC2C,
|
|
||||||
peerUid: coreContext.selfInfo.uid,
|
|
||||||
};
|
|
||||||
const logger = coreContext.context.logger;
|
|
||||||
const NTQQMsgApi = coreContext.apis.MsgApi;
|
|
||||||
//logDebug('克隆的目标消息', msg);
|
|
||||||
|
|
||||||
const sendElements: SendMessageElement[] = [];
|
|
||||||
|
|
||||||
for (const element of msg.elements) {
|
|
||||||
sendElements.push(element as SendMessageElement);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sendElements.length === 0) {
|
|
||||||
logger.logDebug('需要clone的消息无法解析,将会忽略掉', msg);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const nodeMsg = await NTQQMsgApi.sendMsg(selfPeer, sendElements, true);
|
|
||||||
return nodeMsg;
|
|
||||||
} catch (e) {
|
|
||||||
logger.logError(e, '克隆转发消息失败,将忽略本条消息', msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function handleForwardNode(coreContext: NapCatCore, obContext: NapCatOneBot11Adapter, destPeer: Peer, messageNodes: OB11MessageNode[]): Promise<RawMessage | null> {
|
|
||||||
const NTQQMsgApi = coreContext.apis.MsgApi;
|
|
||||||
const selfPeer = {
|
|
||||||
chatType: ChatType.KCHATTYPEC2C,
|
|
||||||
peerUid: coreContext.selfInfo.uid,
|
|
||||||
};
|
|
||||||
let nodeMsgIds: string[] = [];
|
|
||||||
const logger = coreContext.context.logger;
|
|
||||||
for (const messageNode of messageNodes) {
|
|
||||||
const nodeId = messageNode.data.id;
|
|
||||||
if (nodeId) {
|
|
||||||
//对Mgsid和OB11ID混用情况兜底
|
|
||||||
const nodeMsg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(nodeId)) || MessageUnique.getPeerByMsgId(nodeId);
|
|
||||||
if (!nodeMsg) {
|
|
||||||
logger.logError('转发消息失败,未找到消息', nodeId);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
nodeMsgIds.push(nodeMsg.MsgId);
|
|
||||||
} else {
|
|
||||||
// 自定义的消息
|
|
||||||
try {
|
|
||||||
const OB11Data = normalize(messageNode.data.content);
|
|
||||||
//筛选node消息
|
|
||||||
const isNodeMsg = OB11Data.filter(e => e.type === OB11MessageDataType.node).length;//找到子转发消息
|
|
||||||
if (isNodeMsg !== 0) {
|
|
||||||
if (isNodeMsg !== OB11Data.length) {
|
|
||||||
logger.logError('子消息中包含非node消息 跳过不合法部分');
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const nodeMsg = await handleForwardNode(coreContext, obContext, selfPeer, OB11Data.filter(e => e.type === OB11MessageDataType.node));
|
|
||||||
if (nodeMsg) {
|
|
||||||
nodeMsgIds.push(nodeMsg.msgId);
|
|
||||||
MessageUnique.createMsg(selfPeer, nodeMsg.msgId);
|
|
||||||
}
|
|
||||||
//完成子卡片生成跳过后续
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const { sendElements } = await createSendElements(coreContext, obContext, OB11Data, destPeer);
|
|
||||||
//拆分消息
|
|
||||||
const MixElement = sendElements.filter(element => element.elementType !== ElementType.FILE && element.elementType !== ElementType.VIDEO);
|
|
||||||
const SingleElement = sendElements.filter(element => element.elementType === ElementType.FILE || element.elementType === ElementType.VIDEO).map(e => [e]);
|
|
||||||
const AllElement: SendMessageElement[][] = [MixElement, ...SingleElement].filter(e => e !== undefined && e.length !== 0);
|
|
||||||
const MsgNodeList: Promise<RawMessage | undefined>[] = [];
|
|
||||||
for (const sendElementsSplitElement of AllElement) {
|
|
||||||
MsgNodeList.push(sendMsg(coreContext, selfPeer, sendElementsSplitElement, [], true).catch(e => new Promise((resolve, reject) => {
|
|
||||||
resolve(undefined);
|
|
||||||
})));
|
|
||||||
}
|
|
||||||
(await Promise.allSettled(MsgNodeList)).map((result) => {
|
|
||||||
if (result.status === 'fulfilled' && result.value) {
|
|
||||||
nodeMsgIds.push(result.value.msgId);
|
|
||||||
MessageUnique.createMsg(selfPeer, result.value.msgId);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
logger.logDebug('生成转发消息节点失败', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const nodeMsgArray: Array<RawMessage> = [];
|
|
||||||
let srcPeer: Peer | undefined = undefined;
|
|
||||||
let needSendSelf = false;
|
|
||||||
//检测是否处于同一个Peer 不在同一个peer则全部消息由自身发送
|
|
||||||
for (const msgId of nodeMsgIds) {
|
|
||||||
const nodeMsgPeer = MessageUnique.getPeerByMsgId(msgId);
|
|
||||||
if (!nodeMsgPeer) {
|
|
||||||
logger.logError('转发消息失败,未找到消息', msgId);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const nodeMsg = (await NTQQMsgApi.getMsgsByMsgId(nodeMsgPeer.Peer, [msgId])).msgList[0];
|
|
||||||
srcPeer = srcPeer ?? { chatType: nodeMsg.chatType, peerUid: nodeMsg.peerUid };
|
|
||||||
if (srcPeer.peerUid !== nodeMsg.peerUid) {
|
|
||||||
needSendSelf = true;
|
|
||||||
}
|
|
||||||
nodeMsgArray.push(nodeMsg);
|
|
||||||
}
|
|
||||||
nodeMsgIds = nodeMsgArray.map(msg => msg.msgId);
|
|
||||||
let retMsgIds: string[] = [];
|
|
||||||
if (needSendSelf) {
|
|
||||||
for (const [, msg] of nodeMsgArray.entries()) {
|
|
||||||
if (msg.peerUid === coreContext.selfInfo.uid){
|
|
||||||
retMsgIds.push(msg.msgId);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const ClonedMsg = await cloneMsg(coreContext, msg);
|
|
||||||
if (ClonedMsg) retMsgIds.push(ClonedMsg.msgId);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
retMsgIds = nodeMsgIds;
|
|
||||||
}
|
|
||||||
if (retMsgIds.length === 0) throw Error('转发消息失败,生成节点为空');
|
|
||||||
try {
|
|
||||||
logger.logDebug('开发转发', srcPeer, destPeer, retMsgIds);
|
|
||||||
return await NTQQMsgApi.multiForwardMsg(srcPeer!, destPeer, retMsgIds);
|
|
||||||
} catch (e) {
|
|
||||||
logger.logError('forward failed', e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
@ -1,422 +0,0 @@
|
|||||||
import {
|
|
||||||
AtType,
|
|
||||||
ElementType,
|
|
||||||
FaceIndex,
|
|
||||||
FaceType,
|
|
||||||
NapCatCore,
|
|
||||||
PicType,
|
|
||||||
SendArkElement,
|
|
||||||
SendFaceElement,
|
|
||||||
SendFileElement,
|
|
||||||
SendMarkdownElement,
|
|
||||||
SendMarketFaceElement,
|
|
||||||
SendPicElement,
|
|
||||||
SendPttElement,
|
|
||||||
SendReplyElement,
|
|
||||||
sendShareLocationElement,
|
|
||||||
SendTextElement,
|
|
||||||
SendVideoElement,
|
|
||||||
viedo_type,
|
|
||||||
} from '@/core';
|
|
||||||
import * as fsnormal from 'node:fs';
|
|
||||||
import { promises as fs } from 'node:fs';
|
|
||||||
import ffmpeg from 'fluent-ffmpeg';
|
|
||||||
import { calculateFileMD5, isGIF } from '@/common/utils/file';
|
|
||||||
import { defaultVideoThumbB64, getVideoInfo } from '@/common/utils/video';
|
|
||||||
import { encodeSilk } from '@/common/utils/audio';
|
|
||||||
import faceConfig from '@/core/external/face_config.json';
|
|
||||||
import * as pathLib from 'node:path';
|
|
||||||
|
|
||||||
export class SendMsgElementConstructor {
|
|
||||||
static location(CoreContext: NapCatCore): sendShareLocationElement {
|
|
||||||
return {
|
|
||||||
elementType: ElementType.SHARELOCATION,
|
|
||||||
elementId: '',
|
|
||||||
shareLocationElement: {
|
|
||||||
text: '测试',
|
|
||||||
ext: '',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static text(CoreContext: NapCatCore, content: string): SendTextElement {
|
|
||||||
return {
|
|
||||||
elementType: ElementType.TEXT,
|
|
||||||
elementId: '',
|
|
||||||
textElement: {
|
|
||||||
content,
|
|
||||||
atType: AtType.notAt,
|
|
||||||
atUid: '',
|
|
||||||
atTinyId: '',
|
|
||||||
atNtUid: '',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static at(CoreContext: NapCatCore, atUid: string, atNtUid: string, atType: AtType, atName: string): SendTextElement {
|
|
||||||
return {
|
|
||||||
elementType: ElementType.TEXT,
|
|
||||||
elementId: '',
|
|
||||||
textElement: {
|
|
||||||
content: `@${atName}`,
|
|
||||||
atType,
|
|
||||||
atUid,
|
|
||||||
atTinyId: '',
|
|
||||||
atNtUid,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static reply(CoreContext: NapCatCore, msgSeq: string, msgId: string, senderUin: string, senderUinStr: string): SendReplyElement {
|
|
||||||
return {
|
|
||||||
elementType: ElementType.REPLY,
|
|
||||||
elementId: '',
|
|
||||||
replyElement: {
|
|
||||||
replayMsgSeq: msgSeq, // raw.msgSeq
|
|
||||||
replayMsgId: msgId, // raw.msgId
|
|
||||||
senderUin: senderUin,
|
|
||||||
senderUinStr: senderUinStr,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static async pic(coreContext: NapCatCore, picPath: string, summary: string = '', subType: 0 | 1 = 0): Promise<SendPicElement> {
|
|
||||||
const NTQQGroupApi = coreContext.apis.GroupApi;
|
|
||||||
const NTQQUserApi = coreContext.apis.UserApi;
|
|
||||||
const NTQQFileApi = coreContext.apis.FileApi;
|
|
||||||
const NTQQMsgApi = coreContext.apis.MsgApi;
|
|
||||||
const NTQQFriendApi = coreContext.apis.FriendApi;
|
|
||||||
const logger = coreContext.context.logger;
|
|
||||||
const { md5, fileName, path, fileSize } = await NTQQFileApi.uploadFile(picPath, ElementType.PIC, subType);
|
|
||||||
if (fileSize === 0) {
|
|
||||||
throw '文件异常,大小为0';
|
|
||||||
}
|
|
||||||
const imageSize = await NTQQFileApi.getImageSize(picPath);
|
|
||||||
const picElement: any = {
|
|
||||||
md5HexStr: md5,
|
|
||||||
fileSize: fileSize.toString(),
|
|
||||||
picWidth: imageSize?.width,
|
|
||||||
picHeight: imageSize?.height,
|
|
||||||
fileName: fileName,
|
|
||||||
sourcePath: path,
|
|
||||||
original: true,
|
|
||||||
picType: isGIF(picPath) ? PicType.gif : PicType.jpg,
|
|
||||||
picSubType: subType,
|
|
||||||
fileUuid: '',
|
|
||||||
fileSubId: '',
|
|
||||||
thumbFileSize: 0,
|
|
||||||
summary,
|
|
||||||
};
|
|
||||||
//logDebug('图片信息', picElement);
|
|
||||||
return {
|
|
||||||
elementType: ElementType.PIC,
|
|
||||||
elementId: '',
|
|
||||||
picElement,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static async file(coreContext: NapCatCore, filePath: string, fileName: string = '', folderId: string = ''): Promise<SendFileElement> {
|
|
||||||
const NTQQGroupApi = coreContext.apis.GroupApi;
|
|
||||||
const NTQQUserApi = coreContext.apis.UserApi;
|
|
||||||
const NTQQFileApi = coreContext.apis.FileApi;
|
|
||||||
const NTQQMsgApi = coreContext.apis.MsgApi;
|
|
||||||
const NTQQFriendApi = coreContext.apis.FriendApi;
|
|
||||||
const logger = coreContext.context.logger;
|
|
||||||
const { md5, fileName: _fileName, path, fileSize } = await NTQQFileApi.uploadFile(filePath, ElementType.FILE);
|
|
||||||
if (fileSize === 0) {
|
|
||||||
throw '文件异常,大小为0';
|
|
||||||
}
|
|
||||||
const element: SendFileElement = {
|
|
||||||
elementType: ElementType.FILE,
|
|
||||||
elementId: '',
|
|
||||||
fileElement: {
|
|
||||||
fileName: fileName || _fileName,
|
|
||||||
folderId: folderId,
|
|
||||||
'filePath': path!,
|
|
||||||
'fileSize': (fileSize).toString(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
static async video(coreContext: NapCatCore, filePath: string, fileName: string = '', diyThumbPath: string = '', videotype: viedo_type = viedo_type.VIDEO_FORMAT_MP4): Promise<SendVideoElement> {
|
|
||||||
const NTQQFileApi = coreContext.apis.FileApi;
|
|
||||||
const logger = coreContext.context.logger;
|
|
||||||
const { fileName: _fileName, path, fileSize, md5 } = await NTQQFileApi.uploadFile(filePath, ElementType.VIDEO);
|
|
||||||
if (fileSize === 0) {
|
|
||||||
throw '文件异常,大小为0';
|
|
||||||
}
|
|
||||||
let thumb = path.replace(`${pathLib.sep}Ori${pathLib.sep}`, `${pathLib.sep}Thumb${pathLib.sep}`);
|
|
||||||
thumb = pathLib.dirname(thumb);
|
|
||||||
// log("thumb 目录", thumb)
|
|
||||||
let videoInfo = {
|
|
||||||
width: 1920, height: 1080,
|
|
||||||
time: 15,
|
|
||||||
format: 'mp4',
|
|
||||||
size: fileSize,
|
|
||||||
filePath,
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
videoInfo = await getVideoInfo(path, logger);
|
|
||||||
//logDebug('视频信息', videoInfo);
|
|
||||||
} catch (e) {
|
|
||||||
logger.logError('获取视频信息失败', e);
|
|
||||||
}
|
|
||||||
const createThumb = new Promise<string | undefined>((resolve, reject) => {
|
|
||||||
const thumbFileName = `${md5}_0.png`;
|
|
||||||
const thumbPath = pathLib.join(thumb, thumbFileName);
|
|
||||||
ffmpeg(filePath)
|
|
||||||
.on('end', () => {
|
|
||||||
})
|
|
||||||
.on('error', (err) => {
|
|
||||||
logger.logDebug('获取视频封面失败,使用默认封面', err);
|
|
||||||
if (diyThumbPath) {
|
|
||||||
fs.copyFile(diyThumbPath, thumbPath).then(() => {
|
|
||||||
resolve(thumbPath);
|
|
||||||
}).catch(reject);
|
|
||||||
} else {
|
|
||||||
fsnormal.writeFileSync(thumbPath, Buffer.from(defaultVideoThumbB64, 'base64'));
|
|
||||||
resolve(thumbPath);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.screenshots({
|
|
||||||
timestamps: [0],
|
|
||||||
filename: thumbFileName,
|
|
||||||
folder: thumb,
|
|
||||||
size: videoInfo.width + 'x' + videoInfo.height,
|
|
||||||
}).on('end', () => {
|
|
||||||
resolve(thumbPath);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const thumbPath = new Map();
|
|
||||||
const _thumbPath = await createThumb;
|
|
||||||
const thumbSize = _thumbPath ? (await fs.stat(_thumbPath)).size : 0;
|
|
||||||
// log("生成缩略图", _thumbPath)
|
|
||||||
thumbPath.set(0, _thumbPath);
|
|
||||||
const thumbMd5 = _thumbPath ? await calculateFileMD5(_thumbPath) : "";
|
|
||||||
const element: SendVideoElement = {
|
|
||||||
elementType: ElementType.VIDEO,
|
|
||||||
elementId: '',
|
|
||||||
videoElement: {
|
|
||||||
fileName: fileName || _fileName,
|
|
||||||
filePath: path,
|
|
||||||
videoMd5: md5,
|
|
||||||
thumbMd5,
|
|
||||||
fileTime: videoInfo.time,
|
|
||||||
thumbPath: thumbPath,
|
|
||||||
thumbSize,
|
|
||||||
thumbWidth: videoInfo.width,
|
|
||||||
thumbHeight: videoInfo.height,
|
|
||||||
fileSize: '' + fileSize,
|
|
||||||
//fileFormat: videotype
|
|
||||||
// fileUuid: "",
|
|
||||||
// transferStatus: 0,
|
|
||||||
// progress: 0,
|
|
||||||
// invalidState: 0,
|
|
||||||
// fileSubId: "",
|
|
||||||
// fileBizId: null,
|
|
||||||
// originVideoMd5: "",
|
|
||||||
// fileFormat: 2,
|
|
||||||
// import_rich_media_context: null,
|
|
||||||
// sourceVideoCodecFormat: 2
|
|
||||||
},
|
|
||||||
};
|
|
||||||
// "fileElement": {
|
|
||||||
// "fileMd5": "",
|
|
||||||
// "fileName": "1.mp4",
|
|
||||||
// "filePath": "C:\\Users\\nanae\\OneDrive\\Desktop\\1.mp4",
|
|
||||||
// "fileSize": "1847007",
|
|
||||||
// "picHeight": 1280,
|
|
||||||
// "picWidth": 720,
|
|
||||||
// "picThumbPath": {},
|
|
||||||
// "file10MMd5": "",
|
|
||||||
// "fileSha": "",
|
|
||||||
// "fileSha3": "",
|
|
||||||
// "fileUuid": "",
|
|
||||||
// "fileSubId": "",
|
|
||||||
// "thumbFileSize": 750
|
|
||||||
// }
|
|
||||||
return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
static async ptt(coreContext: NapCatCore, pttPath: string): Promise<SendPttElement> {
|
|
||||||
const NTQQGroupApi = coreContext.apis.GroupApi;
|
|
||||||
const NTQQUserApi = coreContext.apis.UserApi;
|
|
||||||
const NTQQFileApi = coreContext.apis.FileApi;
|
|
||||||
const NTQQMsgApi = coreContext.apis.MsgApi;
|
|
||||||
const NTQQFriendApi = coreContext.apis.FriendApi;
|
|
||||||
const logger = coreContext.context.logger;
|
|
||||||
const {
|
|
||||||
converted,
|
|
||||||
path: silkPath,
|
|
||||||
duration,
|
|
||||||
} = await encodeSilk(pttPath, coreContext.NapCatTempPath, coreContext.context.logger);
|
|
||||||
// log("生成语音", silkPath, duration);
|
|
||||||
if (!silkPath) {
|
|
||||||
throw '语音转换失败, 请检查语音文件是否正常';
|
|
||||||
}
|
|
||||||
const { md5, fileName, path, fileSize } = await NTQQFileApi.uploadFile(silkPath!, ElementType.PTT);
|
|
||||||
if (fileSize === 0) {
|
|
||||||
throw '文件异常,大小为0';
|
|
||||||
}
|
|
||||||
if (converted) {
|
|
||||||
fs.unlink(silkPath).then();
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
elementType: ElementType.PTT,
|
|
||||||
elementId: '',
|
|
||||||
pttElement: {
|
|
||||||
fileName: fileName,
|
|
||||||
filePath: path,
|
|
||||||
md5HexStr: md5,
|
|
||||||
fileSize: fileSize,
|
|
||||||
// duration: Math.max(1, Math.round(fileSize / 1024 / 3)), // 一秒钟大概是3kb大小, 小于1秒的按1秒算
|
|
||||||
duration: duration || 1,
|
|
||||||
formatType: 1,
|
|
||||||
voiceType: 1,
|
|
||||||
voiceChangeType: 0,
|
|
||||||
canConvert2Text: true,
|
|
||||||
waveAmplitudes: [
|
|
||||||
0, 18, 9, 23, 16, 17, 16, 15, 44, 17, 24, 20, 14, 15, 17,
|
|
||||||
],
|
|
||||||
fileSubId: '',
|
|
||||||
playState: 1,
|
|
||||||
autoConvertText: 0,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// NodeIQQNTWrapperSession sendMsg [
|
|
||||||
// "0",
|
|
||||||
// {
|
|
||||||
// "peerUid": "u_e_RIxgTs2NaJ68h0PwOPSg",
|
|
||||||
// "chatType": 1,
|
|
||||||
// "guildId": ""
|
|
||||||
// },
|
|
||||||
// [
|
|
||||||
// {
|
|
||||||
// "elementId": "0",
|
|
||||||
// "elementType": 6,
|
|
||||||
// "faceElement": {
|
|
||||||
// "faceIndex": 0,
|
|
||||||
// "faceType": 5,
|
|
||||||
// "msgType": 0,
|
|
||||||
// "pokeType": 1,
|
|
||||||
// "pokeStrength": 0
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// ],
|
|
||||||
// {}
|
|
||||||
// ]
|
|
||||||
static face(CoreContext: NapCatCore, faceId: number): SendFaceElement {
|
|
||||||
// 从face_config.json中获取表情名称
|
|
||||||
const sysFaces = faceConfig.sysface;
|
|
||||||
const emojiFaces = faceConfig.emoji;
|
|
||||||
const face: any = sysFaces.find((face) => face.QSid === faceId.toString());
|
|
||||||
faceId = parseInt(faceId.toString());
|
|
||||||
// let faceType = parseInt(faceId.toString().substring(0, 1));
|
|
||||||
let faceType = 1;
|
|
||||||
if (faceId >= 222) {
|
|
||||||
faceType = 2;
|
|
||||||
}
|
|
||||||
if (face.AniStickerType) {
|
|
||||||
faceType = 3;
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
elementType: ElementType.FACE,
|
|
||||||
elementId: '',
|
|
||||||
faceElement: {
|
|
||||||
faceIndex: faceId,
|
|
||||||
faceType,
|
|
||||||
faceText: face.QDes,
|
|
||||||
stickerId: face.AniStickerId,
|
|
||||||
stickerType: face.AniStickerType,
|
|
||||||
packId: face.AniStickerPackId,
|
|
||||||
sourceType: 1,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static mface(CoreContext: NapCatCore, emojiPackageId: number, emojiId: string, key: string, faceName: string): SendMarketFaceElement {
|
|
||||||
return {
|
|
||||||
elementType: ElementType.MFACE,
|
|
||||||
marketFaceElement: {
|
|
||||||
emojiPackageId,
|
|
||||||
emojiId,
|
|
||||||
key,
|
|
||||||
faceName: faceName || '[商城表情]',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static dice(CoreContext: NapCatCore, resultId: number | null): SendFaceElement {
|
|
||||||
// 实际测试并不能控制结果
|
|
||||||
|
|
||||||
// 随机1到6
|
|
||||||
// if (isNull(resultId)) resultId = Math.floor(Math.random() * 6) + 1;
|
|
||||||
return {
|
|
||||||
elementType: ElementType.FACE,
|
|
||||||
elementId: '',
|
|
||||||
faceElement: {
|
|
||||||
faceIndex: FaceIndex.dice,
|
|
||||||
faceType: FaceType.dice,
|
|
||||||
'faceText': '[骰子]',
|
|
||||||
'packId': '1',
|
|
||||||
'stickerId': '33',
|
|
||||||
'sourceType': 1,
|
|
||||||
'stickerType': 2,
|
|
||||||
// resultId: resultId.toString(),
|
|
||||||
'surpriseId': '',
|
|
||||||
// "randomType": 1,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// 猜拳(石头剪刀布)表情
|
|
||||||
static rps(CoreContext: NapCatCore, resultId: number | null): SendFaceElement {
|
|
||||||
// 实际测试并不能控制结果
|
|
||||||
// if (isNull(resultId)) resultId = Math.floor(Math.random() * 3) + 1;
|
|
||||||
return {
|
|
||||||
elementType: ElementType.FACE,
|
|
||||||
elementId: '',
|
|
||||||
faceElement: {
|
|
||||||
'faceIndex': FaceIndex.RPS,
|
|
||||||
'faceText': '[包剪锤]',
|
|
||||||
'faceType': 3,
|
|
||||||
'packId': '1',
|
|
||||||
'stickerId': '34',
|
|
||||||
'sourceType': 1,
|
|
||||||
'stickerType': 2,
|
|
||||||
// 'resultId': resultId.toString(),
|
|
||||||
'surpriseId': '',
|
|
||||||
// "randomType": 1,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static ark(CoreContext: NapCatCore, data: any): SendArkElement {
|
|
||||||
if (typeof data !== 'string') {
|
|
||||||
data = JSON.stringify(data);
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
elementType: ElementType.ARK,
|
|
||||||
elementId: '',
|
|
||||||
arkElement: {
|
|
||||||
bytesData: data,
|
|
||||||
linkInfo: null,
|
|
||||||
subElementType: null,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static markdown(CoreContext: NapCatCore, content: string): SendMarkdownElement {
|
|
||||||
return {
|
|
||||||
elementType: ElementType.MARKDOWN,
|
|
||||||
elementId: '',
|
|
||||||
markdownElement: {
|
|
||||||
content,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,4 @@
|
|||||||
export * from './config';
|
export * from './config';
|
||||||
export * from './converter';
|
export * from './converter';
|
||||||
export * from './quick';
|
export * from './quick';
|
||||||
export * from './genMessage';
|
|
||||||
export * from './event';
|
export * from './event';
|
||||||
|
@ -13,7 +13,7 @@ import {
|
|||||||
QuickActionGroupRequest,
|
QuickActionGroupRequest,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
import { isNull } from '@/common/utils/helper';
|
import { isNull } from '@/common/utils/helper';
|
||||||
import { createSendElements, normalize, sendMsg } from '../action/msg/SendMsg';
|
import { normalize, sendMsg } from '../action/msg/SendMsg';
|
||||||
import { NapCatOneBot11Adapter } from '..';
|
import { NapCatOneBot11Adapter } from '..';
|
||||||
|
|
||||||
async function handleMsg(coreContext: NapCatCore, obContext: NapCatOneBot11Adapter, msg: OB11Message, quickAction: QuickAction) {
|
async function handleMsg(coreContext: NapCatCore, obContext: NapCatOneBot11Adapter, msg: OB11Message, quickAction: QuickAction) {
|
||||||
@ -53,7 +53,7 @@ async function handleMsg(coreContext: NapCatCore, obContext: NapCatOneBot11Adapt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape));
|
replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape));
|
||||||
const { sendElements, deleteAfterSentFiles } = await createSendElements(coreContext, obContext, replyMessage, peer);
|
const { sendElements, deleteAfterSentFiles } = await obContext.apiContext.MsgApi.createSendElements(replyMessage, peer);
|
||||||
sendMsg(coreContext, peer, sendElements, deleteAfterSentFiles, false).then().catch(coreContext.context.logger.logError);
|
sendMsg(coreContext, peer, sendElements, deleteAfterSentFiles, false).then().catch(coreContext.context.logger.logError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ export interface OB11MessageImage extends OB11MessageFileBase {
|
|||||||
type: OB11MessageDataType.image
|
type: OB11MessageDataType.image
|
||||||
data: OB11MessageFileBase['data'] & {
|
data: OB11MessageFileBase['data'] & {
|
||||||
summary?: string; // 图片摘要
|
summary?: string; // 图片摘要
|
||||||
subType?: PicSubType
|
sub_type?: PicSubType
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +113,7 @@ export interface OB11MessageVideo extends OB11MessageFileBase {
|
|||||||
export interface OB11MessageAt {
|
export interface OB11MessageAt {
|
||||||
type: OB11MessageDataType.at;
|
type: OB11MessageDataType.at;
|
||||||
data: {
|
data: {
|
||||||
qq: `${number}` | 'all'
|
qq: string, // `${number}` | 'all'
|
||||||
name?: string
|
name?: string
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -156,20 +156,20 @@ export interface OB11MessageCustomMusic {
|
|||||||
|
|
||||||
export interface OB11MessageJson {
|
export interface OB11MessageJson {
|
||||||
type: OB11MessageDataType.json;
|
type: OB11MessageDataType.json;
|
||||||
data: { config: { token: string }, data: string };
|
data: { config?: { token: string }, data: string };
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface OB11MessageDice {
|
export interface OB11MessageDice {
|
||||||
type: OB11MessageDataType.dice,
|
type: OB11MessageDataType.dice,
|
||||||
data: {
|
data: {
|
||||||
result: number
|
result: number /* intended */ | string /* in fact */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface OB11MessageRPS {
|
export interface OB11MessageRPS {
|
||||||
type: OB11MessageDataType.RPS,
|
type: OB11MessageDataType.RPS,
|
||||||
data: {
|
data: {
|
||||||
result: number
|
result: number | string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user