From 3e78cc0c1eda878b6bcbf939625fb335f78510a5 Mon Sep 17 00:00:00 2001 From: xswl Date: Thu, 20 Jul 2023 17:49:48 +0800 Subject: [PATCH] optimize filterMessage --- example/imagine-ws.ts | 6 +++--- src/discord.ws.ts | 42 +++++++++++++++++++++++++++++++++++---- src/interfaces/config.ts | 2 +- src/interfaces/message.ts | 1 + 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/example/imagine-ws.ts b/example/imagine-ws.ts index 56e9c1c..0d4214d 100644 --- a/example/imagine-ws.ts +++ b/example/imagine-ws.ts @@ -18,7 +18,7 @@ async function main() { }); await client.Connect(); // required const Imagine = await client.Imagine( - "Red hamster smoking a cigaret", + "Red hamster smoking a cigaret --fast", (uri: string, progress: string) => { console.log("Imagine.loading", uri, "progress", progress); } @@ -66,8 +66,8 @@ async function main() { } main() .then(() => { - console.log("finished"); - process.exit(0); + // console.log("finished"); + // process.exit(0); }) .catch((err) => { console.log("finished"); diff --git a/src/discord.ws.ts b/src/discord.ws.ts index fda4ab1..058bdd7 100644 --- a/src/discord.ws.ts +++ b/src/discord.ws.ts @@ -38,6 +38,7 @@ export class WsMessage { this.ws.addEventListener("open", this.open.bind(this)); this.onSystem("messageCreate", this.onMessageCreate.bind(this)); this.onSystem("messageUpdate", this.onMessageUpdate.bind(this)); + this.onSystem("messageDelete", this.onMessageDelete.bind(this)); this.onSystem("ready", this.onReady.bind(this)); this.onSystem("interactionSuccess", this.onInteractionSuccess.bind(this)); } @@ -276,7 +277,7 @@ export class WsMessage { if (channel_id !== this.config.ChannelId) return; if (author?.id !== this.config.BotId) return; if (interaction && interaction.user.id !== this.UserId) return; - this.log("[messageCreate]", JSON.stringify(message)); + // this.log("[messageCreate]", JSON.stringify(message)); this.messageCreate(message); } @@ -285,9 +286,18 @@ export class WsMessage { if (channel_id !== this.config.ChannelId) return; if (author?.id !== this.config.BotId) return; if (interaction && interaction.user.id !== this.UserId) return; - this.log("[messageUpdate]", JSON.stringify(message)); + // this.log("[messageUpdate]", JSON.stringify(message)); this.messageUpdate(message); } + private async onMessageDelete(message: any) { + const { channel_id, id } = message; + if (channel_id !== this.config.ChannelId) return; + for (const [key, value] of this.waitMjEvents.entries()) { + if (value.id === id) { + this.waitMjEvents.set(key, { ...value, del: true }); + } + } + } // parse message from ws private parseMessage(data: string) { @@ -296,6 +306,9 @@ export class WsMessage { return; } const message = msg.d; + if (message.channel_id === this.config.ChannelId) { + this.log(data); + } this.log("event", msg.t); // console.log(data); switch (msg.t) { @@ -308,6 +321,8 @@ export class WsMessage { case "MESSAGE_UPDATE": this.emitSystem("messageUpdate", message); break; + case "MESSAGE_DELETE": + this.emitSystem("messageDelete", message); case "INTERACTION_SUCCESS": if (message.nonce) { this.emitSystem("interactionSuccess", message); @@ -434,7 +449,9 @@ export class WsMessage { this.emitImage(event.nonce, eventMsg); } - private filterMessages(MJmsg: MJMessage) { + private async filterMessages(MJmsg: MJMessage) { + // delay 300ms for discord message delete + await this.timeout(300); const event = this.getEventByContent(MJmsg.content); if (!event) { this.log("FilterMessages not found", MJmsg, this.waitMjEvents); @@ -447,6 +464,16 @@ export class WsMessage { } private getEventByContent(content: string) { const prompt = content2prompt(content); + //fist del message + for (const [key, value] of this.waitMjEvents.entries()) { + if ( + value.del === true && + prompt === content2prompt(value.prompt as string) + ) { + return value; + } + } + for (const [key, value] of this.waitMjEvents.entries()) { if (prompt === content2prompt(value.prompt as string)) { return value; @@ -499,7 +526,13 @@ export class WsMessage { this.event.push({ event, callback }); } onSystem( - event: "ready" | "messageCreate" | "messageUpdate" | "interactionSuccess", + event: + | "ready" + | "messageCreate" + | "messageUpdate" + | "messageDelete" + | "interactionCreate" + | "interactionSuccess", callback: (message: any) => void ) { this.on(event, callback); @@ -509,6 +542,7 @@ export class WsMessage { | "ready" | "messageCreate" | "messageUpdate" + | "messageDelete" | "interactionSuccess" | "interactionCreate", message: MJEmit diff --git a/src/interfaces/config.ts b/src/interfaces/config.ts index 36acdb8..0e30c8a 100644 --- a/src/interfaces/config.ts +++ b/src/interfaces/config.ts @@ -54,7 +54,7 @@ export const DefaultMJConfig: MJConfig = { MaxWait: 200, ImageProxy: "", DiscordBaseUrl: "https://discord.com", - WsBaseUrl: "wss://gateway.discord.gg?v=9&encoding=json&compress=gzip-stream", + WsBaseUrl: "wss://gateway.discord.gg/?encoding=json&v=9", fetch: fetch, WebSocket: WebSocket, }; diff --git a/src/interfaces/message.ts b/src/interfaces/message.ts index c18638f..2f448b8 100644 --- a/src/interfaces/message.ts +++ b/src/interfaces/message.ts @@ -16,6 +16,7 @@ export interface WaitMjEvent { nonce: string; prompt?: string; id?: string; + del?: boolean; // is delete message onmodal?: OnModal; } export interface MJEmit {