mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-03-01 16:20:25 +00:00
refactor: remove helper directory in onebot
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
import { ConfigBase } from '@/common/config-base';
|
||||
import ob11DefaultConfig from '@/onebot/external/onebot11.json';
|
||||
import { NapCatCore } from '@/core';
|
||||
|
||||
export type OB11Config = typeof ob11DefaultConfig;
|
||||
|
||||
export class OB11ConfigLoader extends ConfigBase<OB11Config> {
|
||||
constructor(core: NapCatCore, configPath: string) {
|
||||
super('onebot11', core, configPath);
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
import { OB11MessageData } from '../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> = {};
|
||||
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 h(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(h('text', { text: unescape(source.slice(0, capture.index)) }));
|
||||
}
|
||||
elements.push(h(type, data));
|
||||
source = source.slice(capture.index + capture[0].length);
|
||||
}
|
||||
if (source) elements.push(h('text', { text: unescape(source) }));
|
||||
return elements;
|
||||
}
|
||||
|
||||
|
||||
export function encodeCQCode(data: OB11MessageData) {
|
||||
const CQCodeEscapeText = (text: string) => {
|
||||
return text.replace(/&/g, '&')
|
||||
.replace(/\[/g, '[')
|
||||
.replace(/]/g, ']');
|
||||
|
||||
};
|
||||
|
||||
const CQCodeEscape = (text: string) => {
|
||||
return text.replace(/&/g, '&')
|
||||
.replace(/\[/g, '[')
|
||||
.replace(/]/g, ']')
|
||||
.replace(/,/g, ',');
|
||||
};
|
||||
|
||||
if (data.type === 'text') {
|
||||
return CQCodeEscapeText(data.data.text);
|
||||
}
|
||||
|
||||
let result = '[CQ:' + data.type;
|
||||
for (const name in data.data) {
|
||||
const value =
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
data.data[name];
|
||||
if (value === undefined) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const text = value.toString();
|
||||
result += `,${name}=${CQCodeEscape(text)}`;
|
||||
} catch (error) {
|
||||
// If it can't be converted, skip this name-value pair
|
||||
}
|
||||
}
|
||||
result += ']';
|
||||
return result;
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
import { calcQQLevel } from '@/common/helper';
|
||||
import { Friend, FriendV2, Group, GroupMember, SelfInfo, Sex, User } from '@/core';
|
||||
import { OB11Group, OB11GroupMember, OB11GroupMemberRole, OB11User, OB11UserSex } from '../types';
|
||||
|
||||
export class OB11Entities {
|
||||
static selfInfo(selfInfo: SelfInfo): OB11User {
|
||||
return {
|
||||
user_id: parseInt(selfInfo.uin),
|
||||
nickname: selfInfo.nick,
|
||||
};
|
||||
}
|
||||
|
||||
static friendsV2(friends: FriendV2[]): OB11User[] {
|
||||
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[] {
|
||||
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 {
|
||||
return {
|
||||
4: OB11GroupMemberRole.owner,
|
||||
3: OB11GroupMemberRole.admin,
|
||||
2: OB11GroupMemberRole.member,
|
||||
}[role];
|
||||
}
|
||||
|
||||
static sex(sex: Sex): OB11UserSex {
|
||||
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: parseInt(group_id),
|
||||
user_id: parseInt(member.uin),
|
||||
nickname: member.nick,
|
||||
card: member.cardName,
|
||||
sex: OB11Entities.sex(member.sex!),
|
||||
age: member.age ?? 0,
|
||||
area: '',
|
||||
level: '0',
|
||||
qq_level: member.qqLevel && calcQQLevel(member.qqLevel) || 0,
|
||||
join_time: 0, // 暂时没法获取
|
||||
last_sent_time: 0, // 暂时没法获取
|
||||
title_expire_time: 0,
|
||||
unfriendly: false,
|
||||
card_changeable: true,
|
||||
is_robot: member.isRobot,
|
||||
shut_up_timestamp: member.shutUpTime,
|
||||
role: OB11Entities.groupMemberRole(member.role),
|
||||
title: member.memberSpecialTitle || '',
|
||||
};
|
||||
}
|
||||
|
||||
static stranger(user: User): OB11User {
|
||||
return {
|
||||
...user,
|
||||
user_id: parseInt(user.uin),
|
||||
nickname: user.nick,
|
||||
sex: OB11Entities.sex(user.sex!),
|
||||
age: 0,
|
||||
qid: user.qid,
|
||||
login_days: 0,
|
||||
level: user.qqLevel && calcQQLevel(user.qqLevel) || 0,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
static group(group: Group): OB11Group {
|
||||
return {
|
||||
group_id: parseInt(group.groupCode),
|
||||
group_name: group.groupName,
|
||||
member_count: group.memberCount,
|
||||
max_member_count: group.maxMember,
|
||||
};
|
||||
}
|
||||
|
||||
static groups(groups: Group[]): OB11Group[] {
|
||||
return groups.map(OB11Entities.group);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from './config';
|
||||
export * from './entities';
|
||||
Reference in New Issue
Block a user