diff --git a/src/core/types/user.ts b/src/core/types/user.ts index 15ff201a..ed2cd129 100644 --- a/src/core/types/user.ts +++ b/src/core/types/user.ts @@ -239,7 +239,7 @@ export type UserV2 = UserDetailInfoListenerArg; export interface UserDetailInfoListenerArg { uid: string; uin: string; - simpleInfo: SimpleInfo; + simpleInfo?: SimpleInfo; commonExt?: CommonExt; photoWall?: PhotoWall; } diff --git a/src/onebot/action/go-cqhttp/GetStrangerInfo.ts b/src/onebot/action/go-cqhttp/GetStrangerInfo.ts index 9ffc719b..5c371b94 100644 --- a/src/onebot/action/go-cqhttp/GetStrangerInfo.ts +++ b/src/onebot/action/go-cqhttp/GetStrangerInfo.ts @@ -29,17 +29,17 @@ export default class GoCQHTTPGetStrangerInfo extends OneBotAction override actionName = ActionName.GetGroupMemberList; override payloadSchema = SchemaData; + /** + * 处理获取群成员列表请求 + */ async _handle(payload: Payload) { const groupIdStr = payload.group_id.toString(); const noCache = this.parseBoolean(payload.no_cache ?? false); + + // 获取群成员基本信息 const groupMembers = await this.getGroupMembers(groupIdStr, noCache); - const _groupMembers = await Promise.all( - Array.from(groupMembers.values()).map(async item => - OB11Construct.groupMember(groupIdStr, item, await this.core.apis.UserApi.getUserDetailInfoV2(item.uin)) - ) + const memberArray = Array.from(groupMembers.values()); + + // 批量并行获取用户详情 + const userDetailsPromises = memberArray.map(member => + this.core.apis.UserApi.getUserDetailInfoV2(member.uin) + .catch(_ => { + return { uin: member.uin, uid: member.uid }; + }) ); - return Array.from(new Map(_groupMembers.map(member => [member.user_id, member])).values()); + const userDetails = await Promise.all(userDetailsPromises); + + // 并行构建 OneBot 格式的群成员数据 + const groupMembersList = memberArray.map((member, index) => { + // 确保用户详情不会是undefined + const userDetail = userDetails[index] || { uin: member.uin, uid: member.uid }; + return OB11Construct.groupMember(groupIdStr, member, userDetail); + }); + + // 直接返回处理后的成员列表,不进行去重 + return groupMembersList; } private parseBoolean(value: boolean | string): boolean { @@ -37,13 +56,18 @@ export class GetGroupMemberList extends OneBotAction let groupMembers = memberCache.get(groupIdStr); if (noCache || !groupMembers) { - const data = this.core.apis.GroupApi.refreshGroupMemberCache(groupIdStr, true).then().catch(); - groupMembers = memberCache.get(groupIdStr) || (await data); - if (!groupMembers) { - throw new Error(`Failed to get group member list for group ${groupIdStr}`); + try { + const refreshPromise = this.core.apis.GroupApi.refreshGroupMemberCache(groupIdStr, true); + + groupMembers = memberCache.get(groupIdStr) || (await refreshPromise); + + if (!groupMembers) { + throw new Error(`无法获取群 ${groupIdStr} 的成员列表`); + } + } catch (error) { + throw new Error(`获取群 ${groupIdStr} 成员列表失败: ${error instanceof Error ? error.message : String(error)}`); } } - return groupMembers; } } \ No newline at end of file diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index 7c7ca63f..41e12d18 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -445,7 +445,7 @@ export class OneBotMsgApi { const uid = await this.core.apis.UserApi.getUidByUinV2(`${atQQ}`); if (!uid) throw new Error('Get Uid Error'); const info = await this.core.apis.UserApi.getUserDetailInfoV2(uid); - return at(atQQ, uid, NTMsgAtType.ATTYPEONE, info.simpleInfo.coreInfo.nick || ''); + return at(atQQ, uid, NTMsgAtType.ATTYPEONE, info.simpleInfo?.coreInfo.nick || ''); }, [OB11MessageDataType.reply]: async ({ data: { id } }) => { @@ -845,7 +845,7 @@ export class OneBotMsgApi { return; } } - resMsg.sender.nickname = (await this.core.apis.UserApi.getUserDetailInfoV2(msg.senderUid)).simpleInfo.coreInfo.nick || ''; + resMsg.sender.nickname = (await this.core.apis.UserApi.getUserDetailInfoV2(msg.senderUid)).simpleInfo?.coreInfo.nick || ''; } private async handleTempGroupMessage(resMsg: OB11Message, msg: RawMessage) { diff --git a/src/onebot/helper/data.ts b/src/onebot/helper/data.ts index 05dff46c..58c1d11e 100644 --- a/src/onebot/helper/data.ts +++ b/src/onebot/helper/data.ts @@ -59,8 +59,8 @@ export class OB11Construct { user_id: +member.uin, nickname: member.nick, card: member.cardName, - sex: this.sex(info?.simpleInfo.baseInfo.sex), - age: info?.simpleInfo.baseInfo.age ?? 0, + sex: this.sex(info?.simpleInfo?.baseInfo.sex), + age: info?.simpleInfo?.baseInfo.age ?? 0, area: info?.commonExt?.address, level: member.memberRealLevel?.toString() ?? '0', qq_level: info?.commonExt?.qqLevel && calcQQLevel(info.commonExt.qqLevel) || 0,