diff --git a/src/onebot/action/extends/GetFriendWithCategory.ts b/src/onebot/action/extends/GetFriendWithCategory.ts index 12cd92b0..c728e9b1 100644 --- a/src/onebot/action/extends/GetFriendWithCategory.ts +++ b/src/onebot/action/extends/GetFriendWithCategory.ts @@ -1,4 +1,4 @@ -import { OB11Constructor } from '@/onebot/helper/converter'; +import { OB11Entities } from '@/onebot/helper/entities'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; @@ -7,7 +7,7 @@ export class GetFriendWithCategory extends BaseAction { async _handle(payload: void) { return (await this.core.apis.FriendApi.getBuddyV2ExWithCate(true)).map(category => ({ ...category, - buddyList: OB11Constructor.friendsV2(category.buddyList), + buddyList: OB11Entities.friendsV2(category.buddyList), })); } } diff --git a/src/onebot/action/go-cqhttp/GetStrangerInfo.ts b/src/onebot/action/go-cqhttp/GetStrangerInfo.ts index 4f6a01a1..08b9dc3c 100644 --- a/src/onebot/action/go-cqhttp/GetStrangerInfo.ts +++ b/src/onebot/action/go-cqhttp/GetStrangerInfo.ts @@ -1,6 +1,6 @@ import BaseAction from '../BaseAction'; -import { OB11User, OB11UserSex } from '../../types'; -import { OB11Constructor } from '@/onebot/helper/converter'; +import { OB11User, OB11UserSex } from '@/onebot'; +import { OB11Entities } from '@/onebot/helper/entities'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { calcQQLevel } from '@/common/utils/helper'; @@ -41,6 +41,6 @@ export default class GoCQHTTPGetStrangerInfo extends BaseAction { const NTQQGroupApi = this.core.apis.GroupApi; const group = (await NTQQGroupApi.getGroups()).find(e => e.groupCode == payload.group_id.toString()); if (!group) throw `群${payload.group_id}不存在`; - return OB11Constructor.group(group); + return OB11Entities.group(group); } } diff --git a/src/onebot/action/group/GetGroupList.ts b/src/onebot/action/group/GetGroupList.ts index 72e63313..6e59f941 100644 --- a/src/onebot/action/group/GetGroupList.ts +++ b/src/onebot/action/group/GetGroupList.ts @@ -1,5 +1,5 @@ -import { OB11Group } from '../../types'; -import { OB11Constructor } from '@/onebot/helper/converter'; +import { OB11Group } from '@/onebot'; +import { OB11Entities } from '@/onebot/helper/entities'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; import { Group } from '@/core/entities'; @@ -21,7 +21,7 @@ class GetGroupList extends BaseAction { async _handle(payload: Payload) { const NTQQGroupApi = this.core.apis.GroupApi; const groupList: Group[] = await NTQQGroupApi.getGroups(typeof payload.no_cache === 'string' ? payload.no_cache === 'true' : !!payload.no_cache); - return OB11Constructor.groups(groupList); + return OB11Entities.groups(groupList); } } diff --git a/src/onebot/action/group/GetGroupMemberInfo.ts b/src/onebot/action/group/GetGroupMemberInfo.ts index 5f02c28f..ab65728d 100644 --- a/src/onebot/action/group/GetGroupMemberInfo.ts +++ b/src/onebot/action/group/GetGroupMemberInfo.ts @@ -1,5 +1,5 @@ -import { OB11GroupMember } from '../../types'; -import { OB11Constructor } from '@/onebot/helper/converter'; +import { OB11GroupMember } from '@/onebot'; +import { OB11Entities } from '@/onebot/helper/entities'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; @@ -39,7 +39,7 @@ class GetGroupMemberInfo extends BaseAction { this.core.context.logger.logDebug(`获取群成员详细信息失败, 只能返回基础信息 ${info.reason}`); } const date = Math.round(Date.now() / 1000); - const retMember = OB11Constructor.groupMember(payload.group_id.toString(), member.value as GroupMember); + const retMember = OB11Entities.groupMember(payload.group_id.toString(), member.value as GroupMember); const Member = await this.core.apis.GroupApi.getGroupMember(payload.group_id.toString(), retMember.user_id); retMember.last_sent_time = parseInt(Member?.lastSpeakTime || date.toString()); retMember.join_time = parseInt(Member?.joinTime || date.toString()); diff --git a/src/onebot/action/group/GetGroupMemberList.ts b/src/onebot/action/group/GetGroupMemberList.ts index 856e3ff8..8be35a90 100644 --- a/src/onebot/action/group/GetGroupMemberList.ts +++ b/src/onebot/action/group/GetGroupMemberList.ts @@ -1,5 +1,5 @@ -import { OB11GroupMember } from '../../types'; -import { OB11Constructor } from '@/onebot/helper/converter'; +import { OB11GroupMember } from '@/onebot'; +import { OB11Entities } from '@/onebot/helper/entities'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; @@ -26,7 +26,7 @@ class GetGroupMemberList extends BaseAction { const groupMembersArr = Array.from(groupMembers.values()); let _groupMembers = groupMembersArr.map(item => { - return OB11Constructor.groupMember(payload.group_id.toString(), item); + return OB11Entities.groupMember(payload.group_id.toString(), item); }); const MemberMap: Map = new Map(); diff --git a/src/onebot/action/system/GetLoginInfo.ts b/src/onebot/action/system/GetLoginInfo.ts index 6d0e9aac..05949dbc 100644 --- a/src/onebot/action/system/GetLoginInfo.ts +++ b/src/onebot/action/system/GetLoginInfo.ts @@ -1,5 +1,5 @@ -import { OB11User } from '../../types'; -import { OB11Constructor } from '@/onebot/helper/converter'; +import { OB11User } from '@/onebot'; +import { OB11Entities } from '@/onebot/helper/entities'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; @@ -7,7 +7,7 @@ class GetLoginInfo extends BaseAction { actionName = ActionName.GetLoginInfo; async _handle(payload: null) { - return OB11Constructor.selfInfo(this.core.selfInfo); + return OB11Entities.selfInfo(this.core.selfInfo); } } diff --git a/src/onebot/action/user/GetFriendList.ts b/src/onebot/action/user/GetFriendList.ts index bfa125b8..6dd517da 100644 --- a/src/onebot/action/user/GetFriendList.ts +++ b/src/onebot/action/user/GetFriendList.ts @@ -1,5 +1,5 @@ -import { OB11User } from '../../types'; -import { OB11Constructor } from '@/onebot/helper/converter'; +import { OB11User } from '@/onebot'; +import { OB11Entities } from '@/onebot/helper/entities'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; @@ -20,6 +20,6 @@ export default class GetFriendList extends BaseAction { async _handle(payload: Payload) { //全新逻辑 const NTQQFriendApi = this.core.apis.FriendApi; - return OB11Constructor.friendsV2(await NTQQFriendApi.getBuddyV2(typeof payload.no_cache === 'string' ? payload.no_cache === 'true' : !!payload.no_cache)); + return OB11Entities.friendsV2(await NTQQFriendApi.getBuddyV2(typeof payload.no_cache === 'string' ? payload.no_cache === 'true' : !!payload.no_cache)); } } diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index df0ac40d..180c73b8 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -24,7 +24,7 @@ import { OB11MessageFileBase, OB11MessageForward, } from '@/onebot'; -import { OB11Constructor } from '../helper'; +import { OB11Entities } from '../helper'; import { EventType } from '@/onebot/event/OB11BaseEvent'; import { encodeCQCode } from '@/onebot/helper/cqcode'; import { uri2local } from '@/common/utils/file'; @@ -689,7 +689,7 @@ export class OneBotMsgApi { let member = await NTQQGroupApi.getGroupMember(msg.peerUin, msg.senderUin); if (!member) member = await NTQQGroupApi.getGroupMember(msg.peerUin, msg.senderUin); if (member) { - resMsg.sender.role = OB11Constructor.groupMemberRole(member.role); + resMsg.sender.role = OB11Entities.groupMemberRole(member.role); resMsg.sender.nickname = member.nick; } } else if (msg.chatType == ChatType.KCHATTYPEC2C) { diff --git a/src/onebot/helper/converter.ts b/src/onebot/helper/entities.ts similarity index 60% rename from src/onebot/helper/converter.ts rename to src/onebot/helper/entities.ts index 2e73d67f..a8b5e1bc 100644 --- a/src/onebot/helper/converter.ts +++ b/src/onebot/helper/entities.ts @@ -1,9 +1,8 @@ import { calcQQLevel } from '@/common/utils/helper'; -import { SelfInfo, FriendV2, Friend, Sex, GroupMember, User, Group } from '@/core'; -import { OB11User, OB11GroupMemberRole, OB11UserSex, OB11GroupMember, OB11Group } from '../types'; +import { Friend, FriendV2, Group, GroupMember, SelfInfo, Sex, User } from '@/core'; +import { OB11Group, OB11GroupMember, OB11GroupMemberRole, OB11User, OB11UserSex } from '../types'; - -export class OB11Constructor { +export class OB11Entities { static selfInfo(selfInfo: SelfInfo): OB11User { return { user_id: parseInt(selfInfo.uin), @@ -12,35 +11,25 @@ export class OB11Constructor { } static friendsV2(friends: FriendV2[]): OB11User[] { - const data: OB11User[] = []; - friends.forEach(friend => { - const sexValue = this.sex(friend.baseInfo.sex!); - data.push({ - ...friend.baseInfo, - ...friend.coreInfo, - user_id: parseInt(friend.coreInfo.uin), - nickname: friend.coreInfo.nick, - remark: friend.coreInfo.nick, - sex: sexValue, - level: 0, - }); - }); - return data; + return friends.map(rawFriend => ({ + ...rawFriend.baseInfo, + ...rawFriend.coreInfo, + user_id: parseInt(rawFriend.coreInfo.uin), + nickname: rawFriend.coreInfo.nick, + remark: rawFriend.coreInfo.nick, + sex: this.sex(rawFriend.baseInfo.sex!), + level: 0, + })); } static friends(friends: Friend[]): OB11User[] { - const data: OB11User[] = []; - friends.forEach(friend => { - const sexValue = this.sex(friend.sex!); - data.push({ - user_id: parseInt(friend.uin), - nickname: friend.nick, - remark: friend.remark, - sex: sexValue, - level: 0, - }); - }); - return data; + return friends.map(rawFriend => ({ + user_id: parseInt(rawFriend.uin), + nickname: rawFriend.nick, + remark: rawFriend.remark, + sex: this.sex(rawFriend.sex!), + level: 0, + })); } static groupMemberRole(role: number): OB11GroupMemberRole | undefined { @@ -52,12 +41,11 @@ export class OB11Constructor { } static sex(sex: Sex): OB11UserSex { - const sexMap = { + return { [Sex.male]: OB11UserSex.male, [Sex.female]: OB11UserSex.female, [Sex.unknown]: OB11UserSex.unknown, - }; - return sexMap[sex] || OB11UserSex.unknown; + }[sex] || OB11UserSex.unknown; } static groupMember(group_id: string, member: GroupMember): OB11GroupMember { @@ -66,7 +54,7 @@ export class OB11Constructor { user_id: parseInt(member.uin), nickname: member.nick, card: member.cardName, - sex: OB11Constructor.sex(member.sex!), + sex: OB11Entities.sex(member.sex!), age: member.age ?? 0, area: '', level: '0', @@ -78,18 +66,17 @@ export class OB11Constructor { card_changeable: true, is_robot: member.isRobot, shut_up_timestamp: member.shutUpTime, - role: OB11Constructor.groupMemberRole(member.role), + role: OB11Entities.groupMemberRole(member.role), title: member.memberSpecialTitle || '', }; } static stranger(user: User): OB11User { - //logDebug('construct ob11 stranger', user); return { ...user, user_id: parseInt(user.uin), nickname: user.nick, - sex: OB11Constructor.sex(user.sex!), + sex: OB11Entities.sex(user.sex!), age: 0, qid: user.qid, login_days: 0, @@ -108,6 +95,6 @@ export class OB11Constructor { } static groups(groups: Group[]): OB11Group[] { - return groups.map(OB11Constructor.group); + return groups.map(OB11Entities.group); } } diff --git a/src/onebot/helper/index.ts b/src/onebot/helper/index.ts index bd9ff396..c32bcf4f 100644 --- a/src/onebot/helper/index.ts +++ b/src/onebot/helper/index.ts @@ -1,3 +1,3 @@ export * from './config'; -export * from './converter'; +export * from './entities'; export * from './event';