mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
chore
This commit is contained in:
parent
2a9353ee70
commit
a9ca951854
@ -1,6 +1,7 @@
|
||||
import { handleQuickOperation } from '@/onebot/helper/quick';
|
||||
import BaseAction from '../BaseAction';
|
||||
import { ActionName } from '../types';
|
||||
import { QuickAction, QuickActionEvent, handleQuickOperation } from '@/onebot/server/postOB11Event';
|
||||
import { QuickAction, QuickActionEvent } from '@/onebot/types';
|
||||
|
||||
interface Payload{
|
||||
context: QuickActionEvent,
|
||||
@ -10,7 +11,7 @@ interface Payload{
|
||||
export class GoCQHTTPHandleQuickAction extends BaseAction<Payload, null>{
|
||||
actionName = ActionName.GoCQHTTP_HandleQuickAction;
|
||||
protected async _handle(payload: Payload): Promise<null> {
|
||||
handleQuickOperation(payload.context, payload.operation,this.CoreContext).then().catch(this.CoreContext.context.logger.logError);
|
||||
handleQuickOperation(this.CoreContext,payload.context, payload.operation).then().catch(this.CoreContext.context.logger.logError);
|
||||
return null;
|
||||
}
|
||||
}
|
77
src/onebot/helper/quick.ts
Normal file
77
src/onebot/helper/quick.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import { ChatType, Group, 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 { createSendElements, normalize, sendMsg } from "../action/msg/SendMsg";
|
||||
|
||||
async function handleMsg(coreContext: NapCatCore, msg: OB11Message, quickAction: QuickAction) {
|
||||
msg = msg as OB11Message;
|
||||
const reply = quickAction.reply;
|
||||
const peer: Peer = {
|
||||
chatType: ChatType.friend,
|
||||
peerUid: await coreContext.getApiContext().UserApi.getUidByUin(msg.user_id.toString()) as string
|
||||
};
|
||||
if (msg.message_type == 'private') {
|
||||
if (msg.sub_type === 'group') {
|
||||
peer.chatType = ChatType.temp;
|
||||
}
|
||||
} else {
|
||||
peer.chatType = ChatType.group;
|
||||
peer.peerUid = msg.group_id!.toString();
|
||||
}
|
||||
if (reply) {
|
||||
let group: Group | undefined;
|
||||
let replyMessage: OB11MessageData[] = [];
|
||||
|
||||
if (msg.message_type == 'group') {
|
||||
group = await 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 createSendElements(coreContext, replyMessage, peer);
|
||||
sendMsg(coreContext, peer, sendElements, deleteAfterSentFiles, false).then().catch(coreContext.context.logger.logError);
|
||||
}
|
||||
}
|
||||
async function handleGroupRequest(coreContext: NapCatCore, request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest) {
|
||||
if (!isNull(quickAction.approve)) {
|
||||
coreContext.getApiContext().GroupApi.handleGroupRequest(
|
||||
request.flag,
|
||||
quickAction.approve ? GroupRequestOperateTypes.approve : GroupRequestOperateTypes.reject,
|
||||
quickAction.reason,
|
||||
).then().catch(coreContext.context.logger.logError);
|
||||
}
|
||||
}
|
||||
async function handleFriendRequest(coreContext: NapCatCore, request: OB11FriendRequestEvent, quickAction: QuickActionFriendRequest) {
|
||||
if (!isNull(quickAction.approve)) {
|
||||
coreContext.getApiContext().FriendApi.handleFriendRequest(request.flag, !!quickAction.approve).then().catch(coreContext.context.logger.logError);
|
||||
}
|
||||
}
|
||||
export async function handleQuickOperation(coreContext: NapCatCore, context: QuickActionEvent, quickAction: QuickAction) {
|
||||
if (context.post_type === 'message') {
|
||||
handleMsg(coreContext, context as OB11Message, quickAction).then().catch(coreContext.context.logger.logError);
|
||||
}
|
||||
if (context.post_type === 'request') {
|
||||
const friendRequest = context as OB11FriendRequestEvent;
|
||||
const groupRequest = context as OB11GroupRequestEvent;
|
||||
if ((friendRequest).request_type === 'friend') {
|
||||
handleFriendRequest(coreContext, friendRequest, quickAction).then().catch(coreContext.context.logger.logError);
|
||||
}
|
||||
else if (groupRequest.request_type === 'group') {
|
||||
handleGroupRequest(coreContext, groupRequest, quickAction).then().catch(coreContext.context.logger.logError);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,15 +1,27 @@
|
||||
import { IOB11NetworkAdapter } from '@/onebot/network/index';
|
||||
import BaseAction from '@/onebot/action/BaseAction';
|
||||
import { OB11BaseEvent } from '@/onebot/event/OB11BaseEvent';
|
||||
|
||||
import { createHmac } from 'crypto';
|
||||
import { LogWrapper } from '@/common/utils/log';
|
||||
import { QuickAction, QuickActionEvent } from '../types';
|
||||
import { NapCatCore } from '@/core';
|
||||
import { NapCatOneBot11Adapter } from '../main';
|
||||
import { handleQuickOperation } from '../helper/quick';
|
||||
export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
||||
url: string;
|
||||
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||
heartbeatInterval: number;
|
||||
|
||||
constructor(url: string, heartbeatInterval: number) {
|
||||
secret: string | undefined;
|
||||
coreContext: NapCatCore;
|
||||
onebotContext: NapCatOneBot11Adapter;
|
||||
logger: LogWrapper;
|
||||
constructor(url: string, heartbeatInterval: number, secret: string | undefined, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) {
|
||||
this.heartbeatInterval = heartbeatInterval;
|
||||
this.url = url;
|
||||
this.secret = secret;
|
||||
this.coreContext = coreContext;
|
||||
this.onebotContext = onebotContext;
|
||||
this.logger = coreContext.context.logger;
|
||||
}
|
||||
registerHeartBeat() {
|
||||
//HttpPost心跳
|
||||
@ -20,20 +32,39 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
||||
}
|
||||
|
||||
onEvent<T extends OB11BaseEvent>(event: T) {
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
'x-self-id': this.coreContext.selfInfo.uin
|
||||
};
|
||||
const msgStr = JSON.stringify(event);
|
||||
if (this.secret && this.secret.length > 0) {
|
||||
|
||||
const hmac = createHmac('sha1', this.secret);
|
||||
hmac.update(msgStr);
|
||||
const sig = hmac.digest('hex');
|
||||
headers['x-signature'] = 'sha1=' + sig;
|
||||
}
|
||||
|
||||
fetch(this.url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(event)
|
||||
headers,
|
||||
body: msgStr
|
||||
}).then(async (res) => {
|
||||
let resJson: QuickAction;
|
||||
try {
|
||||
resJson = await res.json();
|
||||
//logDebug('新消息事件HTTP上报返回快速操作: ', JSON.stringify(resJson));
|
||||
} catch (e) {
|
||||
this.logger.logDebug('新消息事件HTTP上报没有返回快速操作,不需要处理');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
handleQuickOperation(this.coreContext, event as QuickActionEvent, resJson).then().catch(this.logger.logError);
|
||||
} catch (e: any) {
|
||||
this.logger.logError('新消息事件HTTP上报返回快速操作失败', e);
|
||||
}
|
||||
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
console.log('Event sent successfully:', data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Failed to send event:', error);
|
||||
});
|
||||
}
|
||||
|
||||
async open() {
|
||||
|
@ -1,2 +1,4 @@
|
||||
export * from './entity';
|
||||
export * from './message';
|
||||
export * from './message';
|
||||
export * from './quick';
|
||||
export * from './adapter';
|
37
src/onebot/types/quick.ts
Normal file
37
src/onebot/types/quick.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { OB11BaseMetaEvent } from "../event/meta/OB11BaseMetaEvent";
|
||||
import { OB11BaseNoticeEvent } from "../event/notice/OB11BaseNoticeEvent";
|
||||
import { OB11Message } from "./message";
|
||||
|
||||
export type QuickActionEvent = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
|
||||
export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
|
||||
|
||||
export interface QuickActionPrivateMessage {
|
||||
reply?: string;
|
||||
auto_escape?: boolean;
|
||||
}
|
||||
|
||||
export interface QuickActionGroupMessage extends QuickActionPrivateMessage {
|
||||
// 回复群消息
|
||||
at_sender?: boolean;
|
||||
delete?: boolean;
|
||||
kick?: boolean;
|
||||
ban?: boolean;
|
||||
ban_duration?: number;
|
||||
//
|
||||
}
|
||||
|
||||
export interface QuickActionFriendRequest {
|
||||
approve?: boolean;
|
||||
remark?: string;
|
||||
}
|
||||
|
||||
export interface QuickActionGroupRequest {
|
||||
approve?: boolean;
|
||||
reason?: string;
|
||||
}
|
||||
|
||||
export type QuickAction =
|
||||
QuickActionPrivateMessage
|
||||
& QuickActionGroupMessage
|
||||
& QuickActionFriendRequest
|
||||
& QuickActionGroupRequest
|
Loading…
x
Reference in New Issue
Block a user