mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
refactor: move calls & clean code
This commit is contained in:
parent
4f98c0d045
commit
3f3d9cc6f1
@ -1,4 +1,3 @@
|
|||||||
import { handleQuickOperation } from '@/onebot/helper/quick';
|
|
||||||
import BaseAction from '../BaseAction';
|
import BaseAction from '../BaseAction';
|
||||||
import { ActionName } from '../types';
|
import { ActionName } from '../types';
|
||||||
import { QuickAction, QuickActionEvent } from '@/onebot/types';
|
import { QuickAction, QuickActionEvent } from '@/onebot/types';
|
||||||
@ -12,7 +11,9 @@ export class GoCQHTTPHandleQuickAction extends BaseAction<Payload, null> {
|
|||||||
actionName = ActionName.GoCQHTTP_HandleQuickAction;
|
actionName = ActionName.GoCQHTTP_HandleQuickAction;
|
||||||
|
|
||||||
async _handle(payload: Payload): Promise<null> {
|
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;
|
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 './friend';
|
||||||
export * from './group';
|
export * from './group';
|
||||||
export * from './user';
|
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,
|
||||||
|
}
|
||||||
|
@ -18,19 +18,19 @@ export class OneBotQuickActionApi {
|
|||||||
public core: NapCatCore
|
public core: NapCatCore
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
handleQuickOperation(context: QuickActionEvent, quickAction: QuickAction) {
|
async handleQuickOperation(eventContext: QuickActionEvent, quickAction: QuickAction) {
|
||||||
if (context.post_type === 'message') {
|
if (eventContext.post_type === 'message') {
|
||||||
this.handleMsg(context as OB11Message, quickAction)
|
await this.handleMsg(eventContext as OB11Message, quickAction)
|
||||||
.catch(this.core.context.logger.logError);
|
.catch(this.core.context.logger.logError);
|
||||||
}
|
}
|
||||||
if (context.post_type === 'request') {
|
if (eventContext.post_type === 'request') {
|
||||||
const friendRequest = context as OB11FriendRequestEvent;
|
const friendRequest = eventContext as OB11FriendRequestEvent;
|
||||||
const groupRequest = context as OB11GroupRequestEvent;
|
const groupRequest = eventContext as OB11GroupRequestEvent;
|
||||||
if ((friendRequest).request_type === 'friend') {
|
if ((friendRequest).request_type === 'friend') {
|
||||||
this.handleFriendRequest(friendRequest, quickAction)
|
await this.handleFriendRequest(friendRequest, quickAction)
|
||||||
.catch(this.core.context.logger.logError);
|
.catch(this.core.context.logger.logError);
|
||||||
} else if (groupRequest.request_type === 'group') {
|
} else if (groupRequest.request_type === 'group') {
|
||||||
this.handleGroupRequest(groupRequest, quickAction)
|
await this.handleGroupRequest(groupRequest, quickAction)
|
||||||
.catch(this.core.context.logger.logError);
|
.catch(this.core.context.logger.logError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
export * from './config';
|
export * from './config';
|
||||||
export * from './converter';
|
export * from './converter';
|
||||||
export * from './quick';
|
|
||||||
export * from './event';
|
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,
|
NodeIKernelGroupListener,
|
||||||
} from '@/core';
|
} from '@/core';
|
||||||
import { OB11Config, OB11ConfigLoader } from '@/onebot/helper/config';
|
import { OB11Config, OB11ConfigLoader } from '@/onebot/helper/config';
|
||||||
import { StableOneBotApiWrapper } from '@/onebot/types';
|
|
||||||
import {
|
import {
|
||||||
OB11ActiveHttpAdapter,
|
OB11ActiveHttpAdapter,
|
||||||
OB11ActiveWebSocketAdapter,
|
OB11ActiveWebSocketAdapter,
|
||||||
@ -23,7 +22,14 @@ import {
|
|||||||
OB11PassiveWebSocketAdapter,
|
OB11PassiveWebSocketAdapter,
|
||||||
} from '@/onebot/network';
|
} from '@/onebot/network';
|
||||||
import { NapCatPathWrapper } from '@/common/framework/napcat';
|
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 { ActionMap, createActionMap } from '@/onebot/action';
|
||||||
import { WebUiDataRuntime } from '@/webui/src/helper/Data';
|
import { WebUiDataRuntime } from '@/webui/src/helper/Data';
|
||||||
import { OB11InputStatusEvent } from '@/onebot/event/notice/OB11InputStatusEvent';
|
import { OB11InputStatusEvent } from '@/onebot/event/notice/OB11InputStatusEvent';
|
||||||
@ -60,6 +66,7 @@ export class NapCatOneBot11Adapter {
|
|||||||
UserApi: new OneBotUserApi(this, core),
|
UserApi: new OneBotUserApi(this, core),
|
||||||
FriendApi: new OneBotFriendApi(this, core),
|
FriendApi: new OneBotFriendApi(this, core),
|
||||||
MsgApi: new OneBotMsgApi(this, core),
|
MsgApi: new OneBotMsgApi(this, core),
|
||||||
|
QuickActionApi: new OneBotQuickActionApi(this, core),
|
||||||
};
|
};
|
||||||
this.actions = createActionMap(this, core);
|
this.actions = createActionMap(this, core);
|
||||||
this.networkManager = new OB11NetworkManager();
|
this.networkManager = new OB11NetworkManager();
|
||||||
@ -129,7 +136,7 @@ export class NapCatOneBot11Adapter {
|
|||||||
}
|
}
|
||||||
initRecentContactListener() {
|
initRecentContactListener() {
|
||||||
const recentContactListener = new NodeIKernelRecentContactListener();
|
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) => {
|
msgList.forEach((msg) => {
|
||||||
if (msg.chatType == ChatType.KCHATTYPEGROUP) {
|
if (msg.chatType == ChatType.KCHATTYPEGROUP) {
|
||||||
// log("recent contact", msgList, arg0, arg1);
|
// log("recent contact", msgList, arg0, arg1);
|
||||||
@ -231,9 +238,9 @@ export class NapCatOneBot11Adapter {
|
|||||||
|
|
||||||
private initMsgListener() {
|
private initMsgListener() {
|
||||||
const msgListener = new NodeIKernelMsgListener();
|
const msgListener = new NodeIKernelMsgListener();
|
||||||
msgListener.onRecvSysMsg = msg => {
|
/* msgListener.onRecvSysMsg = msg => {
|
||||||
//console.log('onRecvSysMsg', Buffer.from(msg).toString('hex'));
|
//console.log('onRecvSysMsg', Buffer.from(msg).toString('hex'));
|
||||||
};
|
}; */
|
||||||
msgListener.onInputStatusPush = async data => {
|
msgListener.onInputStatusPush = async data => {
|
||||||
const uin = await this.core.apis.UserApi.getUinByUidV2(data.fromUin);
|
const uin = await this.core.apis.UserApi.getUinByUidV2(data.fromUin);
|
||||||
this.context.logger.log(`[Notice] [输入状态] ${uin} ${data.statusText}`);
|
this.context.logger.log(`[Notice] [输入状态] ${uin} ${data.statusText}`);
|
||||||
|
@ -3,7 +3,6 @@ import { createHmac } from 'crypto';
|
|||||||
import { LogWrapper } from '@/common/utils/log';
|
import { LogWrapper } from '@/common/utils/log';
|
||||||
import { QuickAction, QuickActionEvent } from '../types';
|
import { QuickAction, QuickActionEvent } from '../types';
|
||||||
import { NapCatCore } from '@/core';
|
import { NapCatCore } from '@/core';
|
||||||
import { handleQuickOperation } from '../helper/quick';
|
|
||||||
import { NapCatOneBot11Adapter } from '..';
|
import { NapCatOneBot11Adapter } from '..';
|
||||||
|
|
||||||
export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
||||||
@ -48,7 +47,9 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
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) {
|
} catch (e: any) {
|
||||||
this.logger.logError('[OneBot] [Http Client] 新消息事件HTTP上报返回快速操作失败', e);
|
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 './entity';
|
||||||
export * from './message';
|
export * from './message';
|
||||||
export * from './quick';
|
export * from './quick';
|
||||||
export * from './adapter';
|
|
Loading…
x
Reference in New Issue
Block a user