Files
SnowCatQQ/packages/snowcat-onebot/action/msg/GetMsg.ts
T
天天的鸟蛋蛋 b9655a5ab1 SnowCat 雪猫底层全源码:NapCat 4.18.28 底座 + SnowLuma 能力移植 + V8 通道适配 9.9.37
26 包 monorepo(TS 全源码):
- snowcat-core: NT 内核 API 封装 + listener(含 9.9.37 六回调适配)+ Packet 发包
- snowcat-onebot: OneBot v11 全动作 + MCP Streamable HTTP 适配器 + 稳定线
- snowcat-webui-frontend/backend: 雪猫控制台(React+HeroUI)
- snowcat-shell: 单文件 bundle 出口(vite inlineDynamicImports)
- snowcat-native: napi2native / MoeHoo / ffmpeg / pty 运行时
- 56 项 SnowLuma 独门动作移植(qzone 全家桶/群相册/装扮/闪照/收藏)
- 工具件: 解包器/启动器/便携制作器/doctor

GitHub 关联已全量指向 DandanLLab/SnowCatQQ
2026-11-12 06:27:36 +08:00

88 lines
3.8 KiB
TypeScript

import type { MessageFormat } from '@/snowcat-onebot/config/config';
import { OneBotAction } from '@/snowcat-onebot/action/OneBotAction';
import { ActionName } from '@/snowcat-onebot/action/router';
import { MessageUnique } from 'snowcat-common/src/message-unique';
import { PromiseTimer } from 'snowcat-common/src/helper';
import { Static, Type } from '@sinclair/typebox';
import { NetworkAdapterConfig } from '@/snowcat-onebot/config/config';
import { MsgActionsExamples } from './examples';
const PayloadSchema = Type.Object({
message_id: Type.Union([Type.Number(), Type.String()], { description: '消息ID' }),
});
type PayloadType = Static<typeof PayloadSchema>;
const ReturnSchema = Type.Object({
time: Type.Number({ description: '发送时间' }),
message_type: Type.String({ description: '消息类型' }),
message_id: Type.Number({ description: '消息ID' }),
real_id: Type.Number({ description: '真实ID' }),
message_seq: Type.Number({ description: '消息序号' }),
sender: Type.Any({ description: '发送者' }),
message: Type.Any({ description: '消息内容' }),
raw_message: Type.String({ description: '原始消息内容' }),
font: Type.Number({ description: '字体' }),
group_id: Type.Optional(Type.Union([Type.Number(), Type.String()], { description: '群号' })),
user_id: Type.Union([Type.Number(), Type.String()], { description: '发送者QQ号' }),
emoji_likes_list: Type.Optional(Type.Array(Type.Any(), { description: '表情回应列表' })),
}, { description: 'OneBot 11 消息' });
type ReturnType = Static<typeof ReturnSchema>;
class GetMsg extends OneBotAction<PayloadType, ReturnType> {
override actionName = ActionName.GetMsg;
override payloadSchema = PayloadSchema;
override returnSchema = ReturnSchema;
override actionSummary = '获取消息';
override actionDescription = '根据消息 ID 获取消息详细信息';
override actionTags = ['消息接口'];
override payloadExample = MsgActionsExamples.GetMsg.payload;
override returnExample = MsgActionsExamples.GetMsg.response;
async _handle (payload: PayloadType, _adapter: string, config: NetworkAdapterConfig) {
if (!payload.message_id) {
throw Error('参数message_id不能为空');
}
const MsgShortId = MessageUnique.getShortIdByMsgId(payload.message_id.toString());
const msgIdWithPeer = MessageUnique.getMsgIdAndPeerByShortId(MsgShortId ?? +payload.message_id);
if (!msgIdWithPeer) {
throw new Error('消息不存在');
}
const peer = { guildId: '', peerUid: msgIdWithPeer?.Peer.peerUid, chatType: msgIdWithPeer.Peer.chatType };
const getMsgTimeout = this.obContext.configLoader.configData.timeout.baseTimeout;
const msgRes = await PromiseTimer(
this.core.apis.MsgApi.getMsgsByMsgId(peer, [msgIdWithPeer?.MsgId || payload.message_id.toString()]),
getMsgTimeout
).catch((e: unknown) => {
if (e instanceof Error && e.message.startsWith('PromiseTimer:')) {
throw new Error(`消息不存在或已被撤回: ${e.message || String(e)}`);
}
throw e;
});
const msg = msgRes.msgList[0];
if (!msg) throw Error('消息不存在或已被撤回');
const retMsg = await this.obContext.apis.MsgApi.parseMessage(msg, config['messagePostFormat'] as MessageFormat | undefined ?? 'array');
if (!retMsg) throw Error('消息为空');
retMsg.emoji_likes_list = [];
msg.emojiLikesList?.forEach(emoji => {
retMsg.emoji_likes_list?.push({
emoji_id: emoji.emojiId,
emoji_type: emoji.emojiType,
likes_cnt: emoji.likesCnt,
});
});
try {
retMsg.message_id = MessageUnique.createUniqueMsgId(peer, msg.msgId)!;
retMsg.message_seq = retMsg.message_id;
retMsg.real_id = retMsg.message_id;
} catch {
// ignored
}
return retMsg;
}
}
export default GetMsg;