This commit is contained in:
手瓜一十雪 2024-08-09 17:49:51 +08:00
parent 2cabd7879c
commit 3623b991ff

View File

@ -1,37 +1,24 @@
import { OB11MessageData, OB11MessageDataType, OB11MessageFileBase } from '@/onebot11/types'; import { OB11MessageData, OB11MessageDataType, OB11MessageFileBase } from '@/onebot/types';
import {
AtType,
CustomMusicSignPostData,
Group,
IdMusicSignPostData,
NTQQFileApi,
NTQQMsgApi,
Peer,
SendArkElement,
SendMessageElement,
SendMsgElementConstructor,
SignMusicWrapper
} from '@/core';
import { getGroupMember } from '@/core/data';
import { logError, logWarn } from '@/common/utils/log';
import { uri2local } from '@/common/utils/file'; import { uri2local } from '@/common/utils/file';
import { ob11Config } from '@/onebot11/config';
import { RequestUtil } from '@/common/utils/request'; import { RequestUtil } from '@/common/utils/request';
import { MessageUnique } from '@/common/utils/MessageUnique'; import { MessageUnique } from '@/common/utils/MessageUnique';
console.log(process.pid) import { AtType, CustomMusicSignPostData, IdMusicSignPostData, NapCatCore, Peer, SendMessageElement } from '@/core';
import { SendMsgElementConstructor } from '@/onebot/helper/msg';
export type MessageContext = { export type MessageContext = {
deleteAfterSentFiles: string[], deleteAfterSentFiles: string[],
peer:Peer peer: Peer
} }
async function handleOb11FileLikeMessage( async function handleOb11FileLikeMessage(
coreContext: NapCatCore,
{ data: inputdata }: OB11MessageFileBase, { data: inputdata }: OB11MessageFileBase,
{ deleteAfterSentFiles }: MessageContext { deleteAfterSentFiles }: MessageContext
) { ) {
//有的奇怪的框架将url作为参数 而不是file 此时优先url 同时注意可能传入的是非file://开头的目录 By Mlikiowa //有的奇怪的框架将url作为参数 而不是file 此时优先url 同时注意可能传入的是非file://开头的目录 By Mlikiowa
const { path, isLocal, fileName, errMsg,success } = (await uri2local(inputdata?.url || inputdata.file)); const { path, isLocal, fileName, errMsg, success } = (await uri2local(coreContext.NapCatTempPath,inputdata?.url || inputdata.file));
if (!success) { if (!success) {
logError('文件下载失败', errMsg); coreContext.context.logger.logError('文件下载失败', errMsg);
throw Error('文件下载失败' + errMsg); throw Error('文件下载失败' + errMsg);
} }
@ -44,53 +31,56 @@ async function handleOb11FileLikeMessage(
const _handlers: { const _handlers: {
[Key in OB11MessageDataType]: ( [Key in OB11MessageDataType]: (
CoreContext: NapCatCore,
sendMsg: Extract<OB11MessageData, { type: Key }>, sendMsg: Extract<OB11MessageData, { type: Key }>,
// This picks the correct message type out // This picks the correct message type out
// How great the type system of TypeScript is! // How great the type system of TypeScript is!
context: MessageContext context: MessageContext
) => Promise<SendMessageElement | undefined> ) => Promise<SendMessageElement | undefined>
} = { } = {
[OB11MessageDataType.text]: async ({ data: { text } }) => SendMsgElementConstructor.text(text), [OB11MessageDataType.text]: async (coreContext, { data: { text } }) => SendMsgElementConstructor.text(text),
[OB11MessageDataType.at]: async ({ data: { qq: atQQ } }, context) => { [OB11MessageDataType.at]: async (coreContext, { data: { qq: atQQ } }, context) => {
if (!context.peer) return undefined; if (!context.peer) return undefined;
if (atQQ === 'all') return SendMsgElementConstructor.at(atQQ, atQQ, AtType.atAll, '全体成员'); if (atQQ === 'all') return SendMsgElementConstructor.at(coreContext, atQQ, atQQ, AtType.atAll, '全体成员');
// then the qq is a group member // then the qq is a group member
const atMember = await getGroupMember(context.peer.peerUid, atQQ); const atMember = await getGroupMember(context.peer.peerUid, atQQ);
return atMember ? return atMember ?
SendMsgElementConstructor.at(atQQ, atMember.uid, AtType.atUser, atMember.cardName || atMember.nick) : SendMsgElementConstructor.at(coreContext, atQQ, atMember.uid, AtType.atUser, atMember.cardName || atMember.nick) :
undefined; undefined;
}, },
[OB11MessageDataType.reply]: async ({ data: { id } }) => { [OB11MessageDataType.reply]: async (coreContext, { data: { id } }) => {
const replyMsgM = MessageUnique.getMsgIdAndPeerByShortId(parseInt(id)); const replyMsgM = MessageUnique.getMsgIdAndPeerByShortId(parseInt(id));
if (!replyMsgM) { if (!replyMsgM) {
logWarn('回复消息不存在', id); coreContext.context.logger.logWarn('回复消息不存在', id);
return undefined; return undefined;
} }
const NTQQMsgApi = coreContext.getApiContext().MsgApi;
const replyMsg = (await NTQQMsgApi.getMsgsByMsgId(replyMsgM?.Peer!, [replyMsgM?.MsgId!])).msgList[0]; const replyMsg = (await NTQQMsgApi.getMsgsByMsgId(replyMsgM?.Peer!, [replyMsgM?.MsgId!])).msgList[0];
return replyMsg ? return replyMsg ?
SendMsgElementConstructor.reply(replyMsg.msgSeq, replyMsg.msgId, replyMsg.senderUin!, replyMsg.senderUin!) : SendMsgElementConstructor.reply(coreContext, replyMsg.msgSeq, replyMsg.msgId, replyMsg.senderUin!, replyMsg.senderUin!) :
undefined; undefined;
}, },
[OB11MessageDataType.face]: async ({ data: { id } }) => SendMsgElementConstructor.face(parseInt(id)), [OB11MessageDataType.face]: async (coreContext, { data: { id } }) => SendMsgElementConstructor.face(parseInt(id)),
[OB11MessageDataType.mface]: async ({ [OB11MessageDataType.mface]: async (coreContext, {
data: { data: {
emoji_package_id, emoji_package_id,
emoji_id, emoji_id,
key, key,
summary summary
} }
}) => SendMsgElementConstructor.mface(emoji_package_id, emoji_id, key, summary), }) => SendMsgElementConstructor.mface(coreContext, emoji_package_id, emoji_id, key, summary),
// File service // File service
[OB11MessageDataType.image]: async (sendMsg, context) => { [OB11MessageDataType.image]: async (coreContext, sendMsg, context) => {
const PicEle = await SendMsgElementConstructor.pic( const PicEle = await SendMsgElementConstructor.pic(
(await handleOb11FileLikeMessage(sendMsg, context)).path, coreContext,
(await handleOb11FileLikeMessage(coreContext, sendMsg, context)).path,
sendMsg.data.summary || '', sendMsg.data.summary || '',
sendMsg.data.subType || 0 sendMsg.data.subType || 0
); );
@ -99,64 +89,63 @@ const _handlers: {
} }
, // currently not supported , // currently not supported
[OB11MessageDataType.file]: async (sendMsg, context) => { [OB11MessageDataType.file]: async (coreContext, sendMsg, context) => {
const { path, fileName } = await handleOb11FileLikeMessage(sendMsg, context); const { path, fileName } = await handleOb11FileLikeMessage(coreContext, sendMsg, context);
//logDebug('发送文件', path, fileName); //logDebug('发送文件', path, fileName);
const FileEle = await SendMsgElementConstructor.file(path, fileName); const FileEle = await SendMsgElementConstructor.file(coreContext, path, fileName);
// 清除Upload的应该 // 清除Upload的应该
// context.deleteAfterSentFiles.push(fileName || FileEle.fileElement.filePath); // context.deleteAfterSentFiles.push(fileName || FileEle.fileElement.filePath);
return FileEle; return FileEle;
}, },
[OB11MessageDataType.video]: async (sendMsg, context) => { [OB11MessageDataType.video]: async (coreContext, sendMsg, context) => {
const { path, fileName } = await handleOb11FileLikeMessage(sendMsg, context); const { path, fileName } = await handleOb11FileLikeMessage(coreContext, sendMsg, context);
//logDebug('发送视频', path, fileName); //logDebug('发送视频', path, fileName);
let thumb = sendMsg.data.thumb; let thumb = sendMsg.data.thumb;
if (thumb) { if (thumb) {
const uri2LocalRes = await uri2local(thumb); const uri2LocalRes = await uri2local(coreContext.NapCatTempPath, thumb);
if (uri2LocalRes.success) thumb = uri2LocalRes.path; if (uri2LocalRes.success) thumb = uri2LocalRes.path;
} }
const videoEle = await SendMsgElementConstructor.video(path, fileName, thumb); const videoEle = await SendMsgElementConstructor.video(coreContext, path, fileName, thumb);
//未测试 //未测试
context.deleteAfterSentFiles.push(videoEle.videoElement.filePath); context.deleteAfterSentFiles.push(videoEle.videoElement.filePath);
return videoEle; return videoEle;
}, },
[OB11MessageDataType.miniapp]: async ({ data: any }) => SendMsgElementConstructor.miniapp(),
[OB11MessageDataType.voice]: async (sendMsg, context) => [OB11MessageDataType.voice]: async (coreContext, sendMsg, context) =>
SendMsgElementConstructor.ptt((await handleOb11FileLikeMessage(sendMsg, context)).path), SendMsgElementConstructor.ptt(coreContext, (await handleOb11FileLikeMessage(coreContext, sendMsg, context)).path),
[OB11MessageDataType.json]: async ({ data: { data } }) => SendMsgElementConstructor.ark(data), [OB11MessageDataType.json]: async (coreContext, { data: { data } }) => SendMsgElementConstructor.ark(coreContext, data),
[OB11MessageDataType.dice]: async ({ data: { result } }) => SendMsgElementConstructor.dice(result), [OB11MessageDataType.dice]: async (coreContext, { data: { result } }) => SendMsgElementConstructor.dice(coreContext, result),
[OB11MessageDataType.RPS]: async ({ data: { result } }) => SendMsgElementConstructor.rps(result), [OB11MessageDataType.RPS]: async (coreContext, { data: { result } }) => SendMsgElementConstructor.rps(coreContext, result),
[OB11MessageDataType.markdown]: async ({ data: { content } }) => SendMsgElementConstructor.markdown(content), [OB11MessageDataType.markdown]: async (coreContext, { data: { content } }) => SendMsgElementConstructor.markdown(coreContext, content),
[OB11MessageDataType.music]: async ({ data }) => { [OB11MessageDataType.music]: async (coreContext, { data }) => {
// 保留, 直到...找到更好的解决方案 // 保留, 直到...找到更好的解决方案
if (data.type === 'custom') { if (data.type === 'custom') {
if (!data.url) { if (!data.url) {
logError('自定义音卡缺少参数url'); coreContext.context.logger.logError('自定义音卡缺少参数url');
return undefined; return undefined;
} }
if (!data.audio) { if (!data.audio) {
logError('自定义音卡缺少参数audio'); coreContext.context.logger.logError('自定义音卡缺少参数audio');
return undefined; return undefined;
} }
if (!data.title) { if (!data.title) {
logError('自定义音卡缺少参数title'); coreContext.context.logger.logError('自定义音卡缺少参数title');
return undefined; return undefined;
} }
} else { } else {
if (!['qq', '163'].includes(data.type)) { if (!['qq', '163'].includes(data.type)) {
logError('音乐卡片type错误, 只支持qq、163、custom当前type:', data.type); coreContext.context.logger.logError('音乐卡片type错误, 只支持qq、163、custom当前type:', data.type);
return undefined; return undefined;
} }
if (!data.id) { if (!data.id) {
logError('音乐卡片缺少参数id'); coreContext.context.logger.logError('音乐卡片缺少参数id');
return undefined; return undefined;
} }
} }
@ -168,44 +157,46 @@ const _handlers: {
} else { } else {
postData = data; postData = data;
} }
// Mlikiowa V2.0.0 Refactor Todo
const signUrl = ob11Config.musicSignUrl; const signUrl = "";
if (!signUrl) { if (!signUrl) {
if (data.type === 'qq') { if (data.type === 'qq') {
const musicJson = (await SignMusicWrapper(data.id.toString())).data.arkResult.slice(0, -1); //const musicJson = (await SignMusicWrapper(data.id.toString())).data.arkResult.slice(0, -1);
return SendMsgElementConstructor.ark(musicJson); //return SendMsgElementConstructor.ark(musicJson);
} }
throw Error('音乐消息签名地址未配置'); throw Error('音乐消息签名地址未配置');
} }
try { try {
const musicJson = await RequestUtil.HttpGetJson<any>(signUrl, 'POST', postData); const musicJson = await RequestUtil.HttpGetJson<any>(signUrl, 'POST', postData);
return SendMsgElementConstructor.ark(musicJson); return SendMsgElementConstructor.ark(coreContext, musicJson);
} catch (e) { } catch (e) {
logError('生成音乐消息失败', e); logError('生成音乐消息失败', e);
} }
}, },
[OB11MessageDataType.node]: async () => undefined, [OB11MessageDataType.node]: async (coreContext) => undefined,
[OB11MessageDataType.forward]: async () => undefined, [OB11MessageDataType.forward]: async (coreContext) => undefined,
[OB11MessageDataType.xml]: async () => undefined, [OB11MessageDataType.xml]: async (coreContext) => undefined,
[OB11MessageDataType.poke]: async () => undefined, [OB11MessageDataType.poke]: async (coreContext) => undefined,
[OB11MessageDataType.Location]: async () => { [OB11MessageDataType.Location]: async (coreContext) => {
return SendMsgElementConstructor.location(); return SendMsgElementConstructor.location(coreContext);
} }
}; };
const handlers = <{ const handlers = <{
[Key in OB11MessageDataType]: ( [Key in OB11MessageDataType]: (
coreContext: NapCatCore,
sendMsg: OB11MessageData, sendMsg: OB11MessageData,
context: MessageContext context: MessageContext
) => Promise<SendMessageElement | undefined> ) => Promise<SendMessageElement | undefined>
}>_handlers; }>_handlers;
export default async function createSendElements( export default async function createSendElements(
CoreContext: NapCatCore,
messageData: OB11MessageData[], messageData: OB11MessageData[],
peer: Peer, peer: Peer,
ignoreTypes: OB11MessageDataType[] = [] ignoreTypes: OB11MessageDataType[] = []
@ -217,6 +208,7 @@ export default async function createSendElements(
continue; continue;
} }
const callResult = handlers[sendMsg.type]( const callResult = handlers[sendMsg.type](
CoreContext,
sendMsg, sendMsg,
{ peer, deleteAfterSentFiles } { peer, deleteAfterSentFiles }
)?.catch(undefined); )?.catch(undefined);
@ -228,6 +220,7 @@ export default async function createSendElements(
} }
export async function createSendElementsParallel( export async function createSendElementsParallel(
CoreContext: NapCatCore,
messageData: OB11MessageData[], messageData: OB11MessageData[],
peer: Peer, peer: Peer,
ignoreTypes: OB11MessageDataType[] = [] ignoreTypes: OB11MessageDataType[] = []
@ -237,7 +230,7 @@ export async function createSendElementsParallel(
await Promise.all( await Promise.all(
messageData.map(async sendMsg => ignoreTypes.includes(sendMsg.type) ? messageData.map(async sendMsg => ignoreTypes.includes(sendMsg.type) ?
undefined : undefined :
handlers[sendMsg.type](sendMsg, { peer, deleteAfterSentFiles })) handlers[sendMsg.type](CoreContext, sendMsg, { peer, deleteAfterSentFiles }))
).then( ).then(
results => results.filter( results => results.filter(
element => element !== undefined element => element !== undefined