mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: emitMsg
This commit is contained in:
@@ -206,3 +206,17 @@ export function migrateOneBotConfigsV1(config: Partial<v1Config>): OneBotConfig
|
|||||||
}
|
}
|
||||||
return mergedConfig;
|
return mergedConfig;
|
||||||
}
|
}
|
||||||
|
export function getConfigBoolKey(
|
||||||
|
configs: Array<NetworkConfigAdapter>,
|
||||||
|
prediction: (config: NetworkConfigAdapter) => boolean
|
||||||
|
): { positive: Array<string>, negative: Array<string> } {
|
||||||
|
const result: { positive: string[], negative: string[] } = { positive: [], negative: [] };
|
||||||
|
configs.forEach(config => {
|
||||||
|
if (prediction(config)) {
|
||||||
|
result.positive.push(config.name);
|
||||||
|
} else {
|
||||||
|
result.negative.push(config.name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
@@ -543,24 +543,39 @@ export class NapCatOneBot11Adapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async emitMsg(message: RawMessage) {
|
private async emitMsg(message: RawMessage) {
|
||||||
const network = Object.values(this.configLoader.configData.network) as Array<
|
const network = Object.values(this.configLoader.configData.network) as Array<NetworkConfigAdapter>;
|
||||||
(typeof this.configLoader.configData.network)[keyof typeof this.configLoader.configData.network]
|
|
||||||
>;
|
|
||||||
this.context.logger.logDebug('收到新消息 RawMessage', message);
|
this.context.logger.logDebug('收到新消息 RawMessage', message);
|
||||||
this.apis.MsgApi.parseMessageV2(message)
|
try {
|
||||||
.then((ob11Msg) => {
|
const ob11Msg = await this.apis.MsgApi.parseMessageV2(message);
|
||||||
if (!ob11Msg) return;
|
if (!ob11Msg) return;
|
||||||
const isSelfMsg =
|
|
||||||
ob11Msg.stringMsg.user_id.toString() == this.core.selfInfo.uin ||
|
const isSelfMsg = this.isSelfMessage(ob11Msg);
|
||||||
ob11Msg.arrayMsg.user_id.toString() == this.core.selfInfo.uin;
|
|
||||||
this.context.logger.logDebug('转化为 OB11Message', ob11Msg);
|
this.context.logger.logDebug('转化为 OB11Message', ob11Msg);
|
||||||
|
|
||||||
|
const msgMap = this.createMsgMap(network, ob11Msg, isSelfMsg, message);
|
||||||
|
this.handleDebugNetwork(network, msgMap, message);
|
||||||
|
this.handleNotReportSelfNetwork(network, msgMap, isSelfMsg);
|
||||||
|
|
||||||
|
this.networkManager.emitEventByNames(msgMap);
|
||||||
|
} catch (e) {
|
||||||
|
this.context.logger.logError('constructMessage error: ', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.handleGroupEvent(message);
|
||||||
|
this.handlePrivateMsgEvent(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private isSelfMessage(ob11Msg: {
|
||||||
|
stringMsg: OB11Message;
|
||||||
|
arrayMsg: OB11Message;
|
||||||
|
}): boolean {
|
||||||
|
return ob11Msg.stringMsg.user_id.toString() == this.core.selfInfo.uin ||
|
||||||
|
ob11Msg.arrayMsg.user_id.toString() == this.core.selfInfo.uin;
|
||||||
|
}
|
||||||
|
|
||||||
|
private createMsgMap(network: Array<NetworkConfigAdapter>, ob11Msg: any, isSelfMsg: boolean, message: RawMessage): Map<string, OB11Message> {
|
||||||
const msgMap: Map<string, OB11Message> = new Map();
|
const msgMap: Map<string, OB11Message> = new Map();
|
||||||
const enable_client: string[] = [];
|
network.flat().filter(e => e.enable).forEach(e => {
|
||||||
network
|
|
||||||
.flat()
|
|
||||||
.filter((e) => e.enable)
|
|
||||||
.map((e) => {
|
|
||||||
enable_client.push(e.name);
|
|
||||||
if (e.messagePostFormat == 'string') {
|
if (e.messagePostFormat == 'string') {
|
||||||
msgMap.set(e.name, structuredClone(ob11Msg.stringMsg));
|
msgMap.set(e.name, structuredClone(ob11Msg.stringMsg));
|
||||||
} else {
|
} else {
|
||||||
@@ -571,48 +586,52 @@ export class NapCatOneBot11Adapter {
|
|||||||
ob11Msg.arrayMsg.target_id = parseInt(message.peerUin);
|
ob11Msg.arrayMsg.target_id = parseInt(message.peerUin);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return msgMap;
|
||||||
|
}
|
||||||
|
|
||||||
const debug_network = network.flat().filter((e) => e.enable && e.debug);
|
private handleDebugNetwork(network: Array<NetworkConfigAdapter>, msgMap: Map<string, OB11Message>, message: RawMessage) {
|
||||||
if (debug_network.length > 0) {
|
const debugNetwork = network.flat().filter(e => e.enable && e.debug);
|
||||||
for (const adapter of debug_network) {
|
if (debugNetwork.length > 0) {
|
||||||
if (adapter.name) {
|
debugNetwork.forEach(adapter => {
|
||||||
const msg = msgMap.get(adapter.name);
|
const msg = msgMap.get(adapter.name);
|
||||||
if (msg) {
|
if (msg) {
|
||||||
msg.raw = message;
|
msg.raw = message;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
} else if (msgMap.size === 0) {
|
||||||
} else if (ob11Msg.stringMsg.message.length === 0 || ob11Msg.arrayMsg.message.length == 0) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const notreportSelf_network = network.flat().filter((e) => e.enable && (('reportSelfMessage' in e && !e.reportSelfMessage) || !('reportSelfMessage' in e)));
|
}
|
||||||
|
|
||||||
|
private handleNotReportSelfNetwork(network: Array<NetworkConfigAdapter>, msgMap: Map<string, OB11Message>, isSelfMsg: boolean) {
|
||||||
if (isSelfMsg) {
|
if (isSelfMsg) {
|
||||||
for (const adapter of notreportSelf_network) {
|
const notReportSelfNetwork = network.flat().filter(e => e.enable && (('reportSelfMessage' in e && !e.reportSelfMessage) || !('reportSelfMessage' in e)));
|
||||||
|
notReportSelfNetwork.forEach(adapter => {
|
||||||
msgMap.delete(adapter.name);
|
msgMap.delete(adapter.name);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.networkManager.emitEventByNames(msgMap);
|
private async handleGroupEvent(message: RawMessage) {
|
||||||
})
|
try {
|
||||||
.catch((e) => this.context.logger.logError.bind(this.context.logger)('constructMessage error: ', e));
|
const groupEvent = await this.apis.GroupApi.parseGroupEvent(message);
|
||||||
|
|
||||||
this.apis.GroupApi.parseGroupEvent(message)
|
|
||||||
.then((groupEvent) => {
|
|
||||||
if (groupEvent) {
|
if (groupEvent) {
|
||||||
// log("post group event", groupEvent);
|
|
||||||
this.networkManager.emitEvent(groupEvent);
|
this.networkManager.emitEvent(groupEvent);
|
||||||
}
|
}
|
||||||
})
|
} catch (e) {
|
||||||
.catch((e) => this.context.logger.logError.bind(this.context.logger)('constructGroupEvent error: ', e));
|
this.context.logger.logError('constructGroupEvent error: ', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.apis.MsgApi.parsePrivateMsgEvent(message)
|
private async handlePrivateMsgEvent(message: RawMessage) {
|
||||||
.then((privateEvent) => {
|
try {
|
||||||
|
const privateEvent = await this.apis.MsgApi.parsePrivateMsgEvent(message);
|
||||||
if (privateEvent) {
|
if (privateEvent) {
|
||||||
// log("post private event", privateEvent);
|
|
||||||
this.networkManager.emitEvent(privateEvent);
|
this.networkManager.emitEvent(privateEvent);
|
||||||
}
|
}
|
||||||
})
|
} catch (e) {
|
||||||
.catch((e) => this.context.logger.logError.bind(this.context.logger)('constructPrivateEvent error: ', e));
|
this.context.logger.logError('constructPrivateEvent error: ', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private async emitRecallMsg(msgList: RawMessage[], cache: LRUCache<string, boolean>) {
|
private async emitRecallMsg(msgList: RawMessage[], cache: LRUCache<string, boolean>) {
|
||||||
for (const message of msgList) {
|
for (const message of msgList) {
|
||||||
|
Reference in New Issue
Block a user