Skip to content

Commit

Permalink
optimize filterMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Jul 20, 2023
1 parent 15b01bb commit 3e78cc0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
6 changes: 3 additions & 3 deletions example/imagine-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");
Expand Down
42 changes: 38 additions & 4 deletions src/discord.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -509,6 +542,7 @@ export class WsMessage {
| "ready"
| "messageCreate"
| "messageUpdate"
| "messageDelete"
| "interactionSuccess"
| "interactionCreate",
message: MJEmit
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
1 change: 1 addition & 0 deletions src/interfaces/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface WaitMjEvent {
nonce: string;
prompt?: string;
id?: string;
del?: boolean; // is delete message
onmodal?: OnModal;
}
export interface MJEmit {
Expand Down

0 comments on commit 3e78cc0

Please sign in to comment.