From b1047309c917b62c6d3fefc2bdf0bd9760c272b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sat, 19 Apr 2025 11:36:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B6=88=E6=81=AFcontext=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/action/msg/SendMsg.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/onebot/action/msg/SendMsg.ts b/src/onebot/action/msg/SendMsg.ts index 26a93ad2..bb17807c 100644 --- a/src/onebot/action/msg/SendMsg.ts +++ b/src/onebot/action/msg/SendMsg.ts @@ -38,7 +38,7 @@ export function normalize(message: OB11MessageMixType, autoEscape = false): OB11 export async function createContext(core: NapCatCore, payload: OB11PostContext | undefined, contextMode: ContextMode = ContextMode.Normal): Promise { if (!payload) { - throw new Error('请指定 group_id 或 user_id'); + throw new Error('请传递请求内容'); } if ((contextMode === ContextMode.Group || contextMode === ContextMode.Normal) && payload.group_id) { return { @@ -48,7 +48,16 @@ export async function createContext(core: NapCatCore, payload: OB11PostContext | } if ((contextMode === ContextMode.Private || contextMode === ContextMode.Normal) && payload.user_id) { const Uid = await core.apis.UserApi.getUidByUinV2(payload.user_id.toString()); - if (!Uid) throw new Error('无法获取用户信息'); + if (!Uid) { + if (payload.group_id) { + return { + chatType: ChatType.KCHATTYPEGROUP, + peerUid: payload.group_id.toString(), + guildId: '' + } + } + throw new Error('无法获取用户信息'); + } const isBuddy = await core.apis.FriendApi.isBuddy(Uid); if (!isBuddy) { const ret = await core.apis.MsgApi.getTempChatInfo(ChatType.KCHATTYPETEMPC2CFROMGROUP, Uid); @@ -78,7 +87,13 @@ export async function createContext(core: NapCatCore, payload: OB11PostContext | guildId: '', }; } - throw new Error('请指定 group_id 或 user_id'); + if (contextMode === ContextMode.Private && payload.group_id) { + throw new Error('当前私聊发送,请指定 user_id 而不是 group_id'); + } + if (contextMode === ContextMode.Group && payload.user_id) { + throw new Error('当前群聊发送,请指定 group_id 而不是 user_id'); + } + throw new Error('请指定正确的 group_id 或 user_id'); } function getSpecialMsgNum(payload: OB11PostSendMsg, msgType: OB11MessageDataType): number {