mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
Merge branch 'main' of https://github.com/NapNeko/NapCatQQ
This commit is contained in:
commit
be57c312c4
@ -158,11 +158,11 @@ export class LegacyNTEventWrapper {
|
||||
>(
|
||||
serviceAndMethod: `${Service}/${ServiceMethod}`,
|
||||
listenerAndMethod: `${Listener}/${ListenerMethod}`,
|
||||
waitTimes = 1,
|
||||
timeout: number = 3000,
|
||||
args: Parameters<EventType>,
|
||||
checkerEvent: (ret: Awaited<ReturnType<EventType>>) => boolean = () => true,
|
||||
checkerListener: (...args: Parameters<ListenerType>) => boolean = () => true,
|
||||
...args: Parameters<EventType>
|
||||
callbackTimesToWait = 1,
|
||||
timeout = 5000,
|
||||
) {
|
||||
return new Promise<[EventRet: Awaited<ReturnType<EventType>>, ...Parameters<ListenerType>]>(
|
||||
async (resolve, reject) => {
|
||||
@ -202,7 +202,7 @@ export class LegacyNTEventWrapper {
|
||||
func: (...args: any[]) => {
|
||||
complete++;
|
||||
retData = args as Parameters<ListenerType>;
|
||||
if (complete >= waitTimes) {
|
||||
if (complete >= callbackTimesToWait) {
|
||||
clearTimeout(timeoutRef);
|
||||
sendDataCallback();
|
||||
}
|
||||
@ -216,8 +216,8 @@ export class LegacyNTEventWrapper {
|
||||
}
|
||||
this.EventTask.get(ListenerMainName)?.get(ListenerSubName)?.set(id, eventCallback);
|
||||
this.createListenerFunction(ListenerMainName);
|
||||
const EventFunc = this.createEventFunction<EventType>(serviceAndMethod);
|
||||
retEvent = await EventFunc!(...(args as any[]));
|
||||
const eventFunction = this.createEventFunction<EventType>(serviceAndMethod);
|
||||
retEvent = await eventFunction!(...(args));
|
||||
if (!checkerEvent(retEvent)) {
|
||||
clearTimeout(timeoutRef);
|
||||
reject(
|
||||
@ -236,6 +236,7 @@ export class LegacyNTEventWrapper {
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
async callNormalEvent<
|
||||
Service extends keyof ServiceNamingMapping,
|
||||
ServiceMethod extends Exclude<keyof ServiceNamingMapping[Service], symbol>,
|
||||
@ -312,4 +313,5 @@ export class LegacyNTEventWrapper {
|
||||
},
|
||||
);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ import {
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import fsPromises from 'fs/promises';
|
||||
import { InstanceContext, NapCatCore, OnRichMediaDownloadCompleteParams } from '@/core';
|
||||
import { InstanceContext, NapCatCore } from '@/core';
|
||||
import * as fileType from 'file-type';
|
||||
import imageSize from 'image-size';
|
||||
import { ISizeCalculationResult } from 'image-size/dist/types/interface';
|
||||
@ -304,18 +304,10 @@ export class NTQQFileApi {
|
||||
return sourcePath;
|
||||
}
|
||||
}
|
||||
const [, fileTransNotifyInfo] = await this.core.eventWrapper.callNormalEvent(
|
||||
const [, fileTransNotifyInfo] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelMsgService/downloadRichMedia',
|
||||
'NodeIKernelMsgListener/onRichMediaDownloadComplete',
|
||||
1,
|
||||
timeout,
|
||||
(arg: OnRichMediaDownloadCompleteParams) => {
|
||||
if (arg.msgId === msgId) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
{
|
||||
[{
|
||||
fileModelId: '0',
|
||||
downloadSourceType: 0,
|
||||
triggerType: 1,
|
||||
@ -326,7 +318,11 @@ export class NTQQFileApi {
|
||||
thumbSize: 0,
|
||||
downloadType: 1,
|
||||
filePath: thumbPath,
|
||||
},
|
||||
}],
|
||||
() => true,
|
||||
(arg) => arg.msgId === msgId,
|
||||
1,
|
||||
timeout,
|
||||
);
|
||||
const msg = await this.core.apis.MsgApi.getMsgsByMsgId({
|
||||
guildId: '',
|
||||
@ -482,7 +478,7 @@ export class NTQQFileApi {
|
||||
const url: string = element.originImageUrl!; // 没有域名
|
||||
const md5HexStr = element.md5HexStr;
|
||||
const fileMd5 = element.md5HexStr;
|
||||
const fileUuid = element.fileUuid;
|
||||
// const fileUuid = element.fileUuid;
|
||||
|
||||
if (url) {
|
||||
const UrlParse = new URL(IMAGE_HTTP_HOST + url);//临时解析拼接
|
||||
@ -554,9 +550,9 @@ export class NTQQFileCacheApi {
|
||||
}
|
||||
|
||||
getFileCacheInfo(fileType: CacheFileType, pageSize: number = 1000, lastRecord?: CacheFileListItem) {
|
||||
const _lastRecord = lastRecord ? lastRecord : { fileType: fileType };
|
||||
//需要五个参数
|
||||
//return napCatCore.session.getStorageCleanService().getFileCacheInfo();
|
||||
// const _lastRecord = lastRecord ? lastRecord : { fileType: fileType };
|
||||
// 需要五个参数
|
||||
// return napCatCore.session.getStorageCleanService().getFileCacheInfo();
|
||||
}
|
||||
|
||||
async clearChatCache(chats: ChatCacheListItemBasic[] = [], fileKeys: string[] = []) {
|
||||
|
@ -5,7 +5,8 @@ import { LimitedHashTable } from '@/common/utils/message-unique';
|
||||
export class NTQQFriendApi {
|
||||
context: InstanceContext;
|
||||
core: NapCatCore;
|
||||
//friends: Map<string, Friend> = new Map<string, FriendV2>();
|
||||
|
||||
// friends: Map<string, Friend> = new Map<string, FriendV2>();
|
||||
|
||||
constructor(context: InstanceContext, core: NapCatCore) {
|
||||
this.context = context;
|
||||
@ -70,15 +71,17 @@ export class NTQQFriendApi {
|
||||
async isBuddy(uid: string) {
|
||||
return this.context.session.getBuddyService().isBuddy(uid);
|
||||
}
|
||||
|
||||
async clearBuddyReqUnreadCnt() {
|
||||
return this.context.session.getBuddyService().clearBuddyReqUnreadCnt();
|
||||
}
|
||||
|
||||
async getBuddyReq() {
|
||||
const [, ret] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelBuddyService/getBuddyReq',
|
||||
'NodeIKernelBuddyListener/onBuddyReqChange',
|
||||
1,
|
||||
5000);
|
||||
[],
|
||||
);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -4,13 +4,11 @@ import {
|
||||
Group,
|
||||
GroupMember,
|
||||
GroupMemberRole,
|
||||
GroupNotify,
|
||||
GroupRequestOperateTypes,
|
||||
InstanceContext,
|
||||
KickMemberV2Req,
|
||||
MemberExtSourceType,
|
||||
NapCatCore,
|
||||
NodeIKernelGroupListener,
|
||||
NodeIKernelGroupService,
|
||||
} from '@/core';
|
||||
import { isNumeric, runAllWithTimeout, sleep } from '@/common/utils/helper';
|
||||
@ -42,14 +40,10 @@ export class NTQQGroupApi {
|
||||
}
|
||||
|
||||
async getGroups(forced = false) {
|
||||
type ListenerType = NodeIKernelGroupListener['onGroupListUpdate'];
|
||||
const [,, groupList] = await this.core.eventWrapper.callNormalEvent(
|
||||
const [,, groupList] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelGroupService/getGroupList',
|
||||
'NodeIKernelGroupListener/onGroupListUpdate',
|
||||
1,
|
||||
5000,
|
||||
() => true,
|
||||
forced,
|
||||
[forced],
|
||||
);
|
||||
return groupList;
|
||||
}
|
||||
@ -254,15 +248,14 @@ export class NTQQGroupApi {
|
||||
}
|
||||
|
||||
async getSingleScreenNotifies(num: number) {
|
||||
const [,,, notifies] = await this.core.eventWrapper.callNormalEvent(
|
||||
const [,,, notifies] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelGroupService/getSingleScreenNotifies',
|
||||
'NodeIKernelGroupListener/onGroupSingleScreenNotifies',
|
||||
1,
|
||||
5000,
|
||||
() => true,
|
||||
false,
|
||||
'',
|
||||
num,
|
||||
[
|
||||
false,
|
||||
'',
|
||||
num
|
||||
],
|
||||
);
|
||||
return notifies;
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ export class NTQQMsgApi {
|
||||
async getMsgEmojiLikesList(peer: Peer, msgSeq: string, emojiId: string, emojiType: string, count: number = 20) {
|
||||
//console.log(peer, msgSeq, emojiId, emojiType, count);
|
||||
//注意此处emojiType 可选值一般为1-2 2好像是unicode表情dec值 大部分情况 Taged M likiowa
|
||||
return this.context.session.getMsgService().getMsgEmojiLikesList(peer, msgSeq, emojiId, emojiType, '', false, 20);
|
||||
return this.context.session.getMsgService().getMsgEmojiLikesList(peer, msgSeq, emojiId, emojiType, '', false, count);
|
||||
}
|
||||
|
||||
// napCatCore: NapCatCore | null = null;
|
||||
@ -50,7 +50,7 @@ export class NTQQMsgApi {
|
||||
}
|
||||
|
||||
async getLastestMsgByUids(peer: Peer, count: number = 20, isReverseOrder: boolean = false) {
|
||||
const ret = await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', '0', {
|
||||
return await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', '0', {
|
||||
chatInfo: peer,
|
||||
filterMsgType: [],
|
||||
filterSendersUid: [],
|
||||
@ -60,7 +60,6 @@ export class NTQQMsgApi {
|
||||
isIncludeCurrent: true,
|
||||
pageLimit: count,
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
async getMsgsByMsgId(peer: Peer | undefined, msgIds: string[] | undefined) {
|
||||
@ -79,7 +78,7 @@ export class NTQQMsgApi {
|
||||
}
|
||||
|
||||
async queryMsgsWithFilterExWithSeq(peer: Peer, msgSeq: string) {
|
||||
const ret = await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', msgSeq, {
|
||||
return await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', msgSeq, {
|
||||
chatInfo: peer,//此处为Peer 为关键查询参数 没有啥也没有 by mlik iowa
|
||||
filterMsgType: [],
|
||||
filterSendersUid: [],
|
||||
@ -89,7 +88,6 @@ export class NTQQMsgApi {
|
||||
isIncludeCurrent: true,
|
||||
pageLimit: 1,
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
async getMsgsBySeqAndCount(peer: Peer, seq: string, count: number, desc: boolean, z: boolean) {
|
||||
@ -99,7 +97,7 @@ export class NTQQMsgApi {
|
||||
const DateNow = Math.floor(Date.now() / 1000);
|
||||
const filterMsgFromTime = (DateNow - 300).toString();
|
||||
const filterMsgToTime = DateNow.toString();
|
||||
const ret = await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', msgSeq, {
|
||||
return await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', msgSeq, {
|
||||
chatInfo: peer,//此处为Peer 为关键查询参数 没有啥也没有 by mlik iowa
|
||||
filterMsgType: [],
|
||||
filterSendersUid: [],
|
||||
@ -109,24 +107,26 @@ export class NTQQMsgApi {
|
||||
isIncludeCurrent: true,
|
||||
pageLimit: 100,
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
async setMsgRead(peer: Peer) {
|
||||
return this.context.session.getMsgService().setMsgRead(peer);
|
||||
}
|
||||
|
||||
async getGroupFileList(GroupCode: string, params: GetFileListParam) {
|
||||
const [, groupFileListResult] = await this.core.eventWrapper.callNormalEvent(
|
||||
const [, groupFileListResult] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelRichMediaService/getGroupFileList',
|
||||
'NodeIKernelMsgListener/onGroupFileInfoUpdate',
|
||||
1,
|
||||
5000,
|
||||
[
|
||||
GroupCode,
|
||||
params
|
||||
],
|
||||
() => true,
|
||||
( /* groupFileListResult: GroupFileInfoUpdateParamType */) => {
|
||||
//Developer Mlikiowa Todo: 此处有问题 无法判断是否成功
|
||||
return true;
|
||||
},
|
||||
GroupCode,
|
||||
params,
|
||||
1,
|
||||
5000,
|
||||
);
|
||||
return groupFileListResult.item;
|
||||
}
|
||||
@ -175,12 +175,17 @@ export class NTQQMsgApi {
|
||||
}
|
||||
const msgId = await this.generateMsgUniqueId(peer.chatType, await this.getServerTime());
|
||||
peer.guildId = msgId;
|
||||
const [, msgList] = await this.core.eventWrapper.callNormalEvent(
|
||||
const [, msgList] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelMsgService/sendMsg',
|
||||
'NodeIKernelMsgListener/onMsgInfoListUpdate',
|
||||
1,
|
||||
timeout,
|
||||
(msgRecords: RawMessage[]) => {
|
||||
[
|
||||
'0',
|
||||
peer,
|
||||
msgElements,
|
||||
new Map()
|
||||
],
|
||||
() => true,
|
||||
msgRecords => {
|
||||
for (const msgRecord of msgRecords) {
|
||||
if (msgRecord.guildId === msgId && msgRecord.sendStatus === SendStatusType.KSEND_STATUS_SUCCESS) {
|
||||
return true;
|
||||
@ -188,10 +193,8 @@ export class NTQQMsgApi {
|
||||
}
|
||||
return false;
|
||||
},
|
||||
'0',
|
||||
peer,
|
||||
msgElements,
|
||||
new Map(),
|
||||
1,
|
||||
timeout,
|
||||
);
|
||||
return msgList.find(msgRecord => {
|
||||
if (msgRecord.guildId === msgId) {
|
||||
@ -216,12 +219,18 @@ export class NTQQMsgApi {
|
||||
const msgInfos = msgIds.map(id => {
|
||||
return { msgId: id, senderShowName: this.core.selfInfo.nick };
|
||||
});
|
||||
const [, msgList] = await this.core.eventWrapper.callNormalEvent(
|
||||
const [, msgList] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelMsgService/multiForwardMsgWithComment',
|
||||
'NodeIKernelMsgListener/onMsgInfoListUpdate',
|
||||
1,
|
||||
5000,
|
||||
(msgRecords: RawMessage[]) => {
|
||||
[
|
||||
msgInfos,
|
||||
srcPeer,
|
||||
destPeer,
|
||||
[],
|
||||
new Map(),
|
||||
],
|
||||
() => true,
|
||||
(msgRecords) => {
|
||||
for (const msgRecord of msgRecords) {
|
||||
if (msgRecord.peerUid == destPeer.peerUid && msgRecord.senderUid == this.core.selfInfo.uid) {
|
||||
return true;
|
||||
@ -229,11 +238,6 @@ export class NTQQMsgApi {
|
||||
}
|
||||
return false;
|
||||
},
|
||||
msgInfos,
|
||||
srcPeer,
|
||||
destPeer,
|
||||
[],
|
||||
new Map(),
|
||||
);
|
||||
for (const msg of msgList) {
|
||||
const arkElement = msg.elements.find(ele => ele.arkElement);
|
||||
|
@ -1,7 +1,6 @@
|
||||
import type { ModifyProfileParams, User, UserDetailInfoByUin, UserDetailInfoByUinV2 } from '@/core/entities';
|
||||
import { NodeIKernelProfileListener } from '@/core/listeners';
|
||||
import type { ModifyProfileParams, User, UserDetailInfoByUinV2 } from '@/core/entities';
|
||||
import { RequestUtil } from '@/common/utils/request';
|
||||
import { NodeIKernelProfileService, ProfileBizType, UserDetailSource } from '@/core/services';
|
||||
import { ProfileBizType, UserDetailSource } from '@/core/services';
|
||||
import { InstanceContext, NapCatCore } from '..';
|
||||
import { solveAsyncProblem } from '@/common/utils/helper';
|
||||
|
||||
@ -65,15 +64,18 @@ export class NTQQUserApi {
|
||||
}
|
||||
|
||||
async fetchUserDetailInfos(uids: string[]) {
|
||||
//26702 以上使用新接口 .Dev Mlikiowa
|
||||
type EventService = NodeIKernelProfileService['fetchUserDetailInfo'];
|
||||
type EventListener = NodeIKernelProfileListener['onUserDetailInfoChanged'];
|
||||
// TODO: 26702 以上使用新接口 .Dev MliKiowa
|
||||
const retData: User[] = [];
|
||||
const [_retData, _retListener] = await this.core.eventWrapper.callNormalEvent(
|
||||
const [_retData, _retListener] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelProfileService/fetchUserDetailInfo',
|
||||
'NodeIKernelProfileListener/onUserDetailInfoChanged',
|
||||
uids.length,
|
||||
5000,
|
||||
[
|
||||
'BuddyProfileStore',
|
||||
uids,
|
||||
UserDetailSource.KSERVER,
|
||||
[ProfileBizType.KALL]
|
||||
],
|
||||
() => true,
|
||||
(profile) => {
|
||||
if (uids.includes(profile.uid)) {
|
||||
const RetUser: User = {
|
||||
@ -90,26 +92,24 @@ export class NTQQUserApi {
|
||||
}
|
||||
return false;
|
||||
},
|
||||
'BuddyProfileStore',
|
||||
uids,
|
||||
UserDetailSource.KSERVER,
|
||||
[ProfileBizType.KALL],
|
||||
uids.length,
|
||||
);
|
||||
|
||||
return retData;
|
||||
}
|
||||
|
||||
async fetchUserDetailInfo(uid: string, mode: UserDetailSource = UserDetailSource.KDB) {
|
||||
const [_retData, profile] = await this.core.eventWrapper.callNormalEvent(
|
||||
const [_retData, profile] = await this.core.eventWrapper.callNormalEventV2(
|
||||
'NodeIKernelProfileService/fetchUserDetailInfo',
|
||||
'NodeIKernelProfileListener/onUserDetailInfoChanged',
|
||||
1,
|
||||
5000,
|
||||
[
|
||||
'BuddyProfileStore',
|
||||
[uid],
|
||||
mode,
|
||||
[ProfileBizType.KALL]
|
||||
],
|
||||
() => true,
|
||||
(profile) => profile.uid === uid,
|
||||
'BuddyProfileStore',
|
||||
[uid],
|
||||
mode,
|
||||
[ProfileBizType.KALL],
|
||||
);
|
||||
const RetUser: User = {
|
||||
...profile.simpleInfo.coreInfo,
|
||||
@ -142,8 +142,7 @@ export class NTQQUserApi {
|
||||
const ClientKeyData = await this.forceFetchClientKey();
|
||||
const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + this.core.selfInfo.uin +
|
||||
'&clientkey=' + ClientKeyData.clientKey + '&u1=https%3A%2F%2F' + domain + '%2F' + this.core.selfInfo.uin + '%2Finfocenter&keyindex=19%27';
|
||||
const cookies: { [key: string]: string; } = await RequestUtil.HttpsGetCookies(requestUrl);
|
||||
return cookies;
|
||||
return await RequestUtil.HttpsGetCookies(requestUrl);
|
||||
}
|
||||
|
||||
async getPSkey(domainList: string[]) {
|
||||
@ -166,8 +165,7 @@ export class NTQQUserApi {
|
||||
async getQzoneCookies() {
|
||||
const ClientKeyData = await this.forceFetchClientKey();
|
||||
const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + this.core.selfInfo.uin + '&clientkey=' + ClientKeyData.clientKey + '&u1=https%3A%2F%2Fuser.qzone.qq.com%2F' + this.core.selfInfo.uin + '%2Finfocenter&keyindex=19%27';
|
||||
const cookies: { [key: string]: string; } = await RequestUtil.HttpsGetCookies(requestUrl);
|
||||
return cookies;
|
||||
return await RequestUtil.HttpsGetCookies(requestUrl);
|
||||
}
|
||||
|
||||
//需要异常处理
|
||||
@ -178,7 +176,7 @@ export class NTQQUserApi {
|
||||
throw new Error('getClientKey Error');
|
||||
}
|
||||
const clientKey = ClientKeyData.clientKey;
|
||||
const keyIndex = ClientKeyData.keyIndex;
|
||||
// const keyIndex = ClientKeyData.keyIndex;
|
||||
const requestUrl = 'https://ssl.ptlogin2.qq.com/jump?ptlang=1033&clientuin=' + this.core.selfInfo.uin + '&clientkey=' + clientKey + '&u1=https%3A%2F%2Fh5.qzone.qq.com%2Fqqnt%2Fqzoneinpcqq%2Ffriend%3Frefresh%3D0%26clientuin%3D0%26darkMode%3D0&keyindex=19%27';
|
||||
const cookies: { [key: string]: string; } = await RequestUtil.HttpsGetCookies(requestUrl);
|
||||
const skey = cookies['skey'];
|
||||
|
@ -1,4 +1,3 @@
|
||||
import { handleQuickOperation } from '@/onebot/helper/quick';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
import { QuickAction, QuickActionEvent } from '@/onebot/types';
|
||||
@ -12,7 +11,9 @@ export class GoCQHTTPHandleQuickAction extends BaseAction<Payload, null> {
|
||||
actionName = ActionName.GoCQHTTP_HandleQuickAction;
|
||||
|
||||
async _handle(payload: Payload): Promise<null> {
|
||||
handleQuickOperation(this.core, this.obContext, payload.context, payload.operation).then().catch(this.core.context.logger.logError);
|
||||
this.obContext.apis.QuickActionApi
|
||||
.handleQuickOperation(payload.context, payload.operation)
|
||||
.catch(this.core.context.logger.logError);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,19 @@
|
||||
import { OneBotFriendApi } from '@/onebot/api/friend';
|
||||
import { OneBotUserApi } from '@/onebot/api/user';
|
||||
import { OneBotGroupApi } from '@/onebot/api/group';
|
||||
import { OneBotMsgApi } from '@/onebot/api/msg';
|
||||
import { OneBotQuickActionApi } from '@/onebot/api/quick-action';
|
||||
|
||||
export * from './friend';
|
||||
export * from './group';
|
||||
export * from './user';
|
||||
export * from './msg';
|
||||
export * from './msg';
|
||||
export * from './quick-action';
|
||||
|
||||
export interface StableOneBotApiWrapper {
|
||||
FriendApi: OneBotFriendApi;
|
||||
UserApi: OneBotUserApi;
|
||||
GroupApi: OneBotGroupApi;
|
||||
MsgApi: OneBotMsgApi;
|
||||
QuickActionApi: OneBotQuickActionApi,
|
||||
}
|
||||
|
95
src/onebot/api/quick-action.ts
Normal file
95
src/onebot/api/quick-action.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import {
|
||||
NapCatOneBot11Adapter,
|
||||
OB11Message, OB11MessageAt,
|
||||
OB11MessageData,
|
||||
OB11MessageReply,
|
||||
QuickAction,
|
||||
QuickActionEvent, QuickActionFriendRequest, QuickActionGroupMessage, QuickActionGroupRequest,
|
||||
} from '@/onebot';
|
||||
import { ChatType, GroupRequestOperateTypes, NapCatCore, Peer } from '@/core';
|
||||
import { OB11FriendRequestEvent } from '@/onebot/event/request/OB11FriendRequest';
|
||||
import { OB11GroupRequestEvent } from '@/onebot/event/request/OB11GroupRequest';
|
||||
import { normalize } from '@/onebot/action/msg/SendMsg';
|
||||
import { isNull } from '@/common/utils/helper';
|
||||
|
||||
export class OneBotQuickActionApi {
|
||||
constructor(
|
||||
public obContext: NapCatOneBot11Adapter,
|
||||
public core: NapCatCore
|
||||
) {}
|
||||
|
||||
async handleQuickOperation(eventContext: QuickActionEvent, quickAction: QuickAction) {
|
||||
if (eventContext.post_type === 'message') {
|
||||
await this.handleMsg(eventContext as OB11Message, quickAction)
|
||||
.catch(this.core.context.logger.logError);
|
||||
}
|
||||
if (eventContext.post_type === 'request') {
|
||||
const friendRequest = eventContext as OB11FriendRequestEvent;
|
||||
const groupRequest = eventContext as OB11GroupRequestEvent;
|
||||
if ((friendRequest).request_type === 'friend') {
|
||||
await this.handleFriendRequest(friendRequest, quickAction)
|
||||
.catch(this.core.context.logger.logError);
|
||||
} else if (groupRequest.request_type === 'group') {
|
||||
await this.handleGroupRequest(groupRequest, quickAction)
|
||||
.catch(this.core.context.logger.logError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async handleMsg(msg: OB11Message, quickAction: QuickAction) {
|
||||
const reply = quickAction.reply;
|
||||
const peer: Peer = {
|
||||
chatType: ChatType.KCHATTYPEC2C,
|
||||
peerUid: await this.core.apis.UserApi.getUidByUinV2(msg.user_id.toString()) as string,
|
||||
};
|
||||
if (msg.message_type == 'private') {
|
||||
if (msg.sub_type === 'group') {
|
||||
peer.chatType = ChatType.KCHATTYPETEMPC2CFROMGROUP;
|
||||
}
|
||||
} else {
|
||||
peer.chatType = ChatType.KCHATTYPETEMPC2CFROMGROUP;
|
||||
peer.peerUid = msg.group_id!.toString();
|
||||
}
|
||||
if (reply) {
|
||||
// let group: Group | undefined;
|
||||
let replyMessage: OB11MessageData[] = [];
|
||||
|
||||
if (msg.message_type == 'group') {
|
||||
// group = await core.apis.GroupApi.getGroup(msg.group_id!.toString());
|
||||
replyMessage.push({
|
||||
type: 'reply',
|
||||
data: {
|
||||
id: msg.message_id.toString(),
|
||||
},
|
||||
} as OB11MessageReply);
|
||||
if ((quickAction as QuickActionGroupMessage).at_sender) {
|
||||
replyMessage.push({
|
||||
type: 'at',
|
||||
data: {
|
||||
qq: msg.user_id.toString(),
|
||||
},
|
||||
} as OB11MessageAt);
|
||||
}
|
||||
}
|
||||
replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape));
|
||||
const { sendElements, deleteAfterSentFiles } = await this.obContext.apis.MsgApi.createSendElements(replyMessage, peer);
|
||||
this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles, false).then().catch(this.core.context.logger.logError);
|
||||
}
|
||||
}
|
||||
|
||||
async handleGroupRequest(request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest) {
|
||||
if (!isNull(quickAction.approve)) {
|
||||
this.core.apis.GroupApi.handleGroupRequest(
|
||||
request.flag,
|
||||
quickAction.approve ? GroupRequestOperateTypes.approve : GroupRequestOperateTypes.reject,
|
||||
quickAction.reason,
|
||||
).catch(this.core.context.logger.logError);
|
||||
}
|
||||
}
|
||||
|
||||
async handleFriendRequest(request: OB11FriendRequestEvent, quickAction: QuickActionFriendRequest) {
|
||||
if (!isNull(quickAction.approve)) {
|
||||
this.core.apis.FriendApi.handleFriendRequest(request.flag, !!quickAction.approve).then().catch(this.core.context.logger.logError);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
export * from './config';
|
||||
export * from './converter';
|
||||
export * from './quick';
|
||||
export * from './event';
|
||||
|
@ -1,90 +0,0 @@
|
||||
import { ChatType, GroupRequestOperateTypes, NapCatCore, Peer } from '@/core';
|
||||
import { OB11FriendRequestEvent } from '../event/request/OB11FriendRequest';
|
||||
import { OB11GroupRequestEvent } from '../event/request/OB11GroupRequest';
|
||||
import {
|
||||
OB11Message,
|
||||
OB11MessageAt,
|
||||
OB11MessageData,
|
||||
OB11MessageReply,
|
||||
QuickAction,
|
||||
QuickActionEvent,
|
||||
QuickActionFriendRequest,
|
||||
QuickActionGroupMessage,
|
||||
QuickActionGroupRequest,
|
||||
} from '../types';
|
||||
import { isNull } from '@/common/utils/helper';
|
||||
import { normalize } from '../action/msg/SendMsg';
|
||||
import { NapCatOneBot11Adapter } from '..';
|
||||
|
||||
async function handleMsg(core: NapCatCore, obContext: NapCatOneBot11Adapter, msg: OB11Message, quickAction: QuickAction) {
|
||||
msg = msg as OB11Message;
|
||||
const reply = quickAction.reply;
|
||||
const peer: Peer = {
|
||||
chatType: ChatType.KCHATTYPEC2C,
|
||||
peerUid: await core.apis.UserApi.getUidByUinV2(msg.user_id.toString()) as string,
|
||||
};
|
||||
if (msg.message_type == 'private') {
|
||||
if (msg.sub_type === 'group') {
|
||||
peer.chatType = ChatType.KCHATTYPETEMPC2CFROMGROUP;
|
||||
}
|
||||
} else {
|
||||
peer.chatType = ChatType.KCHATTYPETEMPC2CFROMGROUP;
|
||||
peer.peerUid = msg.group_id!.toString();
|
||||
}
|
||||
if (reply) {
|
||||
// let group: Group | undefined;
|
||||
let replyMessage: OB11MessageData[] = [];
|
||||
|
||||
if (msg.message_type == 'group') {
|
||||
// group = await core.apis.GroupApi.getGroup(msg.group_id!.toString());
|
||||
replyMessage.push({
|
||||
type: 'reply',
|
||||
data: {
|
||||
id: msg.message_id.toString(),
|
||||
},
|
||||
} as OB11MessageReply);
|
||||
if ((quickAction as QuickActionGroupMessage).at_sender) {
|
||||
replyMessage.push({
|
||||
type: 'at',
|
||||
data: {
|
||||
qq: msg.user_id.toString(),
|
||||
},
|
||||
} as OB11MessageAt);
|
||||
}
|
||||
}
|
||||
replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape));
|
||||
const { sendElements, deleteAfterSentFiles } = await obContext.apis.MsgApi.createSendElements(replyMessage, peer);
|
||||
obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles, false).then().catch(core.context.logger.logError);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleGroupRequest(core: NapCatCore, request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest) {
|
||||
if (!isNull(quickAction.approve)) {
|
||||
core.apis.GroupApi.handleGroupRequest(
|
||||
request.flag,
|
||||
quickAction.approve ? GroupRequestOperateTypes.approve : GroupRequestOperateTypes.reject,
|
||||
quickAction.reason,
|
||||
).then().catch(core.context.logger.logError);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleFriendRequest(core: NapCatCore, request: OB11FriendRequestEvent, quickAction: QuickActionFriendRequest) {
|
||||
if (!isNull(quickAction.approve)) {
|
||||
core.apis.FriendApi.handleFriendRequest(request.flag, !!quickAction.approve).then().catch(core.context.logger.logError);
|
||||
}
|
||||
}
|
||||
|
||||
export async function handleQuickOperation(core: NapCatCore, obContext: NapCatOneBot11Adapter, context: QuickActionEvent, quickAction: QuickAction) {
|
||||
if (context.post_type === 'message') {
|
||||
handleMsg(core, obContext, context as OB11Message, quickAction).then().catch(core.context.logger.logError);
|
||||
}
|
||||
if (context.post_type === 'request') {
|
||||
const friendRequest = context as OB11FriendRequestEvent;
|
||||
const groupRequest = context as OB11GroupRequestEvent;
|
||||
if ((friendRequest).request_type === 'friend') {
|
||||
handleFriendRequest(core, friendRequest, quickAction).then().catch(core.context.logger.logError);
|
||||
} else if (groupRequest.request_type === 'group') {
|
||||
handleGroupRequest(core, groupRequest, quickAction).then().catch(core.context.logger.logError);
|
||||
}
|
||||
}
|
||||
}
|
@ -14,7 +14,6 @@ import {
|
||||
NodeIKernelGroupListener,
|
||||
} from '@/core';
|
||||
import { OB11Config, OB11ConfigLoader } from '@/onebot/helper/config';
|
||||
import { StableOneBotApiWrapper } from '@/onebot/types';
|
||||
import {
|
||||
OB11ActiveHttpAdapter,
|
||||
OB11ActiveWebSocketAdapter,
|
||||
@ -23,7 +22,14 @@ import {
|
||||
OB11PassiveWebSocketAdapter,
|
||||
} from '@/onebot/network';
|
||||
import { NapCatPathWrapper } from '@/common/framework/napcat';
|
||||
import { OneBotFriendApi, OneBotGroupApi, OneBotMsgApi, OneBotUserApi } from '@/onebot/api';
|
||||
import {
|
||||
OneBotFriendApi,
|
||||
OneBotGroupApi,
|
||||
OneBotMsgApi,
|
||||
OneBotQuickActionApi,
|
||||
OneBotUserApi,
|
||||
StableOneBotApiWrapper,
|
||||
} from '@/onebot/api';
|
||||
import { ActionMap, createActionMap } from '@/onebot/action';
|
||||
import { WebUiDataRuntime } from '@/webui/src/helper/Data';
|
||||
import { OB11InputStatusEvent } from '@/onebot/event/notice/OB11InputStatusEvent';
|
||||
@ -60,6 +66,7 @@ export class NapCatOneBot11Adapter {
|
||||
UserApi: new OneBotUserApi(this, core),
|
||||
FriendApi: new OneBotFriendApi(this, core),
|
||||
MsgApi: new OneBotMsgApi(this, core),
|
||||
QuickActionApi: new OneBotQuickActionApi(this, core),
|
||||
};
|
||||
this.actions = createActionMap(this, core);
|
||||
this.networkManager = new OB11NetworkManager();
|
||||
@ -129,7 +136,7 @@ export class NapCatOneBot11Adapter {
|
||||
}
|
||||
initRecentContactListener() {
|
||||
const recentContactListener = new NodeIKernelRecentContactListener();
|
||||
recentContactListener.onRecentContactNotification = function (msgList: any[], arg0: { msgListUnreadCnt: string }, arg1: number) {
|
||||
recentContactListener.onRecentContactNotification = function (msgList: any[], /* arg0: { msgListUnreadCnt: string }, arg1: number */) {
|
||||
msgList.forEach((msg) => {
|
||||
if (msg.chatType == ChatType.KCHATTYPEGROUP) {
|
||||
// log("recent contact", msgList, arg0, arg1);
|
||||
@ -231,9 +238,9 @@ export class NapCatOneBot11Adapter {
|
||||
|
||||
private initMsgListener() {
|
||||
const msgListener = new NodeIKernelMsgListener();
|
||||
msgListener.onRecvSysMsg = msg => {
|
||||
/* msgListener.onRecvSysMsg = msg => {
|
||||
//console.log('onRecvSysMsg', Buffer.from(msg).toString('hex'));
|
||||
};
|
||||
}; */
|
||||
msgListener.onInputStatusPush = async data => {
|
||||
const uin = await this.core.apis.UserApi.getUinByUidV2(data.fromUin);
|
||||
this.context.logger.log(`[Notice] [输入状态] ${uin} ${data.statusText}`);
|
||||
|
@ -3,7 +3,6 @@ import { createHmac } from 'crypto';
|
||||
import { LogWrapper } from '@/common/utils/log';
|
||||
import { QuickAction, QuickActionEvent } from '../types';
|
||||
import { NapCatCore } from '@/core';
|
||||
import { handleQuickOperation } from '../helper/quick';
|
||||
import { NapCatOneBot11Adapter } from '..';
|
||||
|
||||
export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
||||
@ -48,7 +47,9 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
handleQuickOperation(this.core, this.obContext, event as QuickActionEvent, resJson).then().catch(this.logger.logError);
|
||||
this.obContext.apis.QuickActionApi
|
||||
.handleQuickOperation(event as QuickActionEvent, resJson)
|
||||
.catch(this.logger.logError);
|
||||
} catch (e: any) {
|
||||
this.logger.logError('[OneBot] [Http Client] 新消息事件HTTP上报返回快速操作失败', e);
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
import { OneBotFriendApi, OneBotGroupApi, OneBotMsgApi, OneBotUserApi } from '../api';
|
||||
|
||||
export interface StableOneBotApiWrapper {
|
||||
FriendApi: OneBotFriendApi;
|
||||
UserApi: OneBotUserApi;
|
||||
GroupApi: OneBotGroupApi;
|
||||
MsgApi: OneBotMsgApi;
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
export * from './entity';
|
||||
export * from './message';
|
||||
export * from './quick';
|
||||
export * from './adapter';
|
Loading…
x
Reference in New Issue
Block a user