mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-06 21:10:23 +00:00
refactor: 项目结构
This commit is contained in:
84
src/onebot/helper/cqcode.ts
Normal file
84
src/onebot/helper/cqcode.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { OB11MessageData } from '@/onebot/types';
|
||||
|
||||
const pattern = /\[CQ:(\w+)((,\w+=[^,\]]*)*)]/;
|
||||
|
||||
function unescape(source: string) {
|
||||
return String(source)
|
||||
.replace(/[/g, '[')
|
||||
.replace(/]/g, ']')
|
||||
.replace(/,/g, ',')
|
||||
.replace(/&/g, '&');
|
||||
}
|
||||
|
||||
function from(source: string) {
|
||||
const capture = pattern.exec(source);
|
||||
if (!capture) return null;
|
||||
const [, type, attrs] = capture;
|
||||
const data: Record<string, any> = {};
|
||||
if (attrs) {
|
||||
attrs.slice(1).split(',').forEach((str) => {
|
||||
const index = str.indexOf('=');
|
||||
data[str.slice(0, index)] = unescape(str.slice(index + 1));
|
||||
});
|
||||
}
|
||||
return { type, data, capture };
|
||||
}
|
||||
|
||||
function convert(type: string, data: any) {
|
||||
return {
|
||||
type,
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
export function decodeCQCode(source: string): OB11MessageData[] {
|
||||
const elements: any[] = [];
|
||||
let result: ReturnType<typeof from>;
|
||||
while ((result = from(source))) {
|
||||
const { type, data, capture } = result;
|
||||
if (capture.index) {
|
||||
elements.push(convert('text', { text: unescape(source.slice(0, capture.index)) }));
|
||||
}
|
||||
elements.push(convert(type, data));
|
||||
source = source.slice(capture.index + capture[0].length);
|
||||
}
|
||||
if (source) elements.push(convert('text', { text: unescape(source) }));
|
||||
return elements;
|
||||
}
|
||||
|
||||
function CQCodeEscapeText(text: string) {
|
||||
return text.replace(/&/g, '&')
|
||||
.replace(/\[/g, '[')
|
||||
.replace(/]/g, ']');
|
||||
}
|
||||
|
||||
function CQCodeEscape(text: string) {
|
||||
return text.replace(/&/g, '&')
|
||||
.replace(/\[/g, '[')
|
||||
.replace(/]/g, ']')
|
||||
.replace(/,/g, ',');
|
||||
}
|
||||
|
||||
export function encodeCQCode(data: OB11MessageData) {
|
||||
if (data.type === 'text') {
|
||||
return CQCodeEscapeText(data.data.text);
|
||||
}
|
||||
|
||||
let result = '[CQ:' + data.type;
|
||||
for (const name in data.data) {
|
||||
const value = (data.data as Record<string, unknown>)[name];
|
||||
if (value === undefined) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const text = value?.toString();
|
||||
if (text) {
|
||||
result += `,${name}=${CQCodeEscape(text)}`;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error encoding CQCode for ${name}:`, error);
|
||||
}
|
||||
}
|
||||
result += ']';
|
||||
return result;
|
||||
}
|
||||
113
src/onebot/helper/data.ts
Normal file
113
src/onebot/helper/data.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { calcQQLevel, FileNapCatOneBotUUID } from '@/common/helper';
|
||||
import { FriendV2, Group, GroupFileInfoUpdateParamType, GroupMember, SelfInfo, Sex } from '@/core';
|
||||
import {
|
||||
OB11Group,
|
||||
OB11GroupFile,
|
||||
OB11GroupFileFolder,
|
||||
OB11GroupMember,
|
||||
OB11GroupMemberRole,
|
||||
OB11User,
|
||||
OB11UserSex,
|
||||
} from '../types';
|
||||
export class OB11Construct {
|
||||
static selfInfo(selfInfo: SelfInfo): OB11User {
|
||||
return {
|
||||
user_id: +selfInfo.uin,
|
||||
nickname: selfInfo.nick,
|
||||
};
|
||||
}
|
||||
|
||||
static friends(friends: FriendV2[]): OB11User[] {
|
||||
return friends.map(rawFriend => ({
|
||||
...rawFriend.baseInfo,
|
||||
...rawFriend.coreInfo,
|
||||
user_id: parseInt(rawFriend.coreInfo.uin),
|
||||
nickname: rawFriend.coreInfo.nick,
|
||||
remark: rawFriend.coreInfo.remark ?? rawFriend.coreInfo.nick,
|
||||
sex: this.sex(rawFriend.baseInfo.sex),
|
||||
level: 0,
|
||||
}));
|
||||
}
|
||||
|
||||
static groupMemberRole(role: number): OB11GroupMemberRole | undefined {
|
||||
return {
|
||||
4: OB11GroupMemberRole.owner,
|
||||
3: OB11GroupMemberRole.admin,
|
||||
2: OB11GroupMemberRole.member,
|
||||
}[role];
|
||||
}
|
||||
|
||||
static sex(sex?: Sex): OB11UserSex {
|
||||
if (!sex) return OB11UserSex.unknown;
|
||||
return {
|
||||
[Sex.male]: OB11UserSex.male,
|
||||
[Sex.female]: OB11UserSex.female,
|
||||
[Sex.unknown]: OB11UserSex.unknown,
|
||||
}[sex] || OB11UserSex.unknown;
|
||||
}
|
||||
|
||||
static groupMember(group_id: string, member: GroupMember): OB11GroupMember {
|
||||
return {
|
||||
group_id: +group_id,
|
||||
user_id: +member.uin,
|
||||
nickname: member.nick,
|
||||
card: member.cardName,
|
||||
sex: this.sex(member.sex),
|
||||
age: member.age ?? 0,
|
||||
area: '',
|
||||
level: member.memberRealLevel?.toString() ?? '0',
|
||||
qq_level: member.qqLevel && calcQQLevel(member.qqLevel) || 0,
|
||||
join_time: +member.joinTime,
|
||||
last_sent_time: +member.lastSpeakTime,
|
||||
title_expire_time: 0,
|
||||
unfriendly: false,
|
||||
card_changeable: true,
|
||||
is_robot: member.isRobot,
|
||||
shut_up_timestamp: member.shutUpTime,
|
||||
role: this.groupMemberRole(member.role),
|
||||
title: member.memberSpecialTitle ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
static group(group: Group): OB11Group {
|
||||
return {
|
||||
group_id: +group.groupCode,
|
||||
group_name: group.groupName,
|
||||
member_count: group.memberCount,
|
||||
max_member_count: group.maxMember,
|
||||
};
|
||||
}
|
||||
|
||||
static groups(groups: Group[]): OB11Group[] {
|
||||
return groups.map(this.group);
|
||||
}
|
||||
|
||||
static file(peerId: string, file: Exclude<GroupFileInfoUpdateParamType['item'][0]['fileInfo'], undefined>): OB11GroupFile {
|
||||
return {
|
||||
group_id: +peerId,
|
||||
file_id: FileNapCatOneBotUUID.encodeModelId({ chatType: 2, peerUid: peerId }, file.fileModelId, file.fileId, file.fileId ?? ''),
|
||||
file_name: file.fileName,
|
||||
busid: file.busId,
|
||||
size: +file.fileSize,
|
||||
upload_time: file.uploadTime,
|
||||
dead_time: file.deadTime,
|
||||
modify_time: file.modifyTime,
|
||||
download_times: file.downloadTimes,
|
||||
uploader: +file.uploaderUin,
|
||||
uploader_name: file.uploaderName,
|
||||
};
|
||||
}
|
||||
|
||||
static folder(peerId: string, folder: Exclude<GroupFileInfoUpdateParamType['item'][0]['folderInfo'], undefined>): OB11GroupFileFolder {
|
||||
return {
|
||||
group_id: +peerId,
|
||||
folder_id: folder.folderId,
|
||||
folder: folder.folderId,
|
||||
folder_name: folder.folderName,
|
||||
create_time: folder.createTime,
|
||||
creator: +folder.createUin,
|
||||
creator_name: folder.creatorName,
|
||||
total_file_count: folder.totalFileCount,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user