mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-12-19 05:05:44 +08:00
Add emoji like event handling to core and onebot
Introduces a typed event emitter for app events in napcat-core, specifically for emoji like events in groups. OlPushService now emits 'event:emoji_like' when a group reaction is detected. napcat-onebot listens for this event and emits corresponding OneBot events. Refactors and adds missing type definitions and improves method formatting for consistency.
This commit is contained in:
parent
75e1e8dd79
commit
31bb1e5dee
@ -32,6 +32,7 @@ import { proxiedListenerOf } from 'napcat-common/src/proxy-handler';
|
||||
import { NTQQPacketApi } from './apis/packet';
|
||||
import { NativePacketHandler } from './packet/handler/client';
|
||||
import { container, ReceiverServiceRegistry } from './packet/handler/serviceRegister';
|
||||
import { appEvent } from './packet/handler/eventList';
|
||||
export * from './wrapper';
|
||||
export * from './types/index';
|
||||
export * from './services/index';
|
||||
@ -93,6 +94,7 @@ export function getMajorPath (QQVersion: string): string {
|
||||
export class NapCatCore {
|
||||
readonly context: InstanceContext;
|
||||
readonly eventWrapper: NTEventWrapper;
|
||||
event = appEvent;
|
||||
NapCatDataPath: string = '';
|
||||
NapCatTempPath: string = '';
|
||||
apis: StableNTApiWrapper;
|
||||
|
||||
6
packages/napcat-core/packet/handler/eventList.ts
Normal file
6
packages/napcat-core/packet/handler/eventList.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { TypedEventEmitter } from "./typeEvent";
|
||||
|
||||
export interface AppEvents {
|
||||
'event:emoji_like': { groupId: string; senderUin: string; emojiId: string, msgSeq: string, isAdd: boolean,count:number };
|
||||
}
|
||||
export const appEvent = new TypedEventEmitter<AppEvents>();
|
||||
22
packages/napcat-core/packet/handler/typeEvent.ts
Normal file
22
packages/napcat-core/packet/handler/typeEvent.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { EventEmitter } from 'node:events';
|
||||
|
||||
export class TypedEventEmitter<E extends Record<string, any>> {
|
||||
private emitter = new EventEmitter();
|
||||
|
||||
on<K extends keyof E>(event: K, listener: (payload: E[K]) => void) {
|
||||
this.emitter.on(event as string, listener);
|
||||
return () => this.off(event, listener);
|
||||
}
|
||||
|
||||
once<K extends keyof E>(event: K, listener: (payload: E[K]) => void) {
|
||||
this.emitter.once(event as string, listener);
|
||||
}
|
||||
|
||||
off<K extends keyof E>(event: K, listener: (payload: E[K]) => void) {
|
||||
this.emitter.off(event as string, listener);
|
||||
}
|
||||
|
||||
emit<K extends keyof E>(event: K, payload: E[K]) {
|
||||
this.emitter.emit(event as string, payload);
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,38 @@
|
||||
import { NapProtoMsg } from "napcat-protobuf";
|
||||
import { appEvent } from "../packet/handler/eventList";
|
||||
import { ReceiveService, ServiceBase } from "../packet/handler/serviceRegister";
|
||||
import { GroupReactNotify, PushMsg } from "../packet/transformer/proto";
|
||||
|
||||
// @ReceiveService('trpc.msg.olpush.OlPushService.MsgPush')
|
||||
// export class OlPushService extends ServiceBase {
|
||||
// async handler(seq: number, hex_data: string) {
|
||||
// console.log(`OlPushService handler called with seq: ${seq} and data: ${hex_data}`);
|
||||
// }
|
||||
// }
|
||||
@ReceiveService('trpc.msg.olpush.OlPushService.MsgPush')
|
||||
export class OlPushService extends ServiceBase {
|
||||
async handler(_seq: number, hex_data: string) {
|
||||
const data = new NapProtoMsg(PushMsg).decode(Buffer.from(hex_data, 'hex'));
|
||||
if (data.message.contentHead.type === 732 && data.message.contentHead.subType === 16) {
|
||||
const pbNotify = data.message.body?.msgContent?.slice(7);
|
||||
if (!pbNotify) {
|
||||
return;
|
||||
}
|
||||
// 开始解析Notify
|
||||
const notify = new NapProtoMsg(GroupReactNotify).decode(pbNotify);
|
||||
if ((notify.field13 ?? 0) === 35) {
|
||||
// Group React Notify
|
||||
const groupCode = notify.groupUin?.toString() ?? '';
|
||||
const operatorUid = notify.groupReactionData?.data?.data?.groupReactionDataContent?.operatorUid ?? '';
|
||||
const type = notify.groupReactionData?.data?.data?.groupReactionDataContent?.type ?? 0;
|
||||
const seq = notify.groupReactionData?.data?.data?.groupReactionTarget?.seq?.toString() ?? '';
|
||||
const code = notify.groupReactionData?.data?.data?.groupReactionDataContent?.code ?? '';
|
||||
const count = notify.groupReactionData?.data?.data?.groupReactionDataContent?.count ?? 0;
|
||||
const senderUin = await this.core.apis.UserApi.getUinByUidV2(operatorUid);
|
||||
appEvent.emit('event:emoji_like', {
|
||||
groupId: groupCode,
|
||||
senderUin: senderUin,
|
||||
emojiId: code,
|
||||
msgSeq: seq,
|
||||
isAdd: type === 1,
|
||||
count: count
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -172,6 +172,22 @@ export class OneBotGroupApi {
|
||||
});
|
||||
}
|
||||
|
||||
async registerParseGroupReactEventByCore() {
|
||||
this.core.event.on('event:emoji_like', async (data) => {
|
||||
console.log('Received emoji_like event from core:', data);
|
||||
const event = await this.createGroupEmojiLikeEvent(
|
||||
data.groupId,
|
||||
data.senderUin,
|
||||
data.msgSeq,
|
||||
data.emojiId,
|
||||
data.isAdd,
|
||||
data.count
|
||||
);
|
||||
if (event) {
|
||||
this.obContext.networkManager.emitEvent(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
async parsePaiYiPai(msg: RawMessage, jsonStr: string) {
|
||||
const json = JSON.parse(jsonStr);
|
||||
// 判断业务类型
|
||||
|
||||
@ -55,13 +55,20 @@ import { OB11HttpSSEServerAdapter } from './network/http-server-sse';
|
||||
import { OB11PluginMangerAdapter } from './network/plugin-manger';
|
||||
import { existsSync } from 'node:fs';
|
||||
|
||||
interface ApiListType {
|
||||
GroupApi: OneBotGroupApi;
|
||||
UserApi: OneBotUserApi;
|
||||
FriendApi: OneBotFriendApi;
|
||||
MsgApi: OneBotMsgApi;
|
||||
QuickActionApi: OneBotQuickActionApi;
|
||||
}
|
||||
// OneBot实现类
|
||||
export class NapCatOneBot11Adapter {
|
||||
readonly core: NapCatCore;
|
||||
readonly context: InstanceContext;
|
||||
|
||||
configLoader: OB11ConfigLoader;
|
||||
public readonly apis;
|
||||
public apis: ApiListType;
|
||||
networkManager: OB11NetworkManager;
|
||||
actions: ActionMap;
|
||||
private readonly bootTime = Date.now() / 1000;
|
||||
@ -218,7 +225,7 @@ export class NapCatOneBot11Adapter {
|
||||
// this.context.logger.log(`OneBot11 配置更改:${JSON.stringify(prev)} -> ${JSON.stringify(newConfig)}`);
|
||||
await this.reloadNetwork(prev, newConfig);
|
||||
});
|
||||
this.apis.GroupApi.registerParseGroupReactEvent().catch(e =>
|
||||
this.apis.GroupApi.registerParseGroupReactEventByCore().catch(e =>
|
||||
this.context.logger.logError('注册群消息反应表情失败', e)
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user