Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Jul 3, 2023
1 parent 449d5c1 commit 72e477e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
15 changes: 8 additions & 7 deletions example/imagine-niji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ async function main() {
await client.Connect();
const info = await client.Info();
console.log(info);
// const msg = await client.Imagine(
// "A little white dog",
// (uri: string, progress: string) => {
// console.log("loading", uri, "progress", progress);
// }
// );
// console.log({ msg });
const msg = await client.Imagine(
"A little white dog",
(uri: string, progress: string) => {
console.log("loading", uri, "progress", progress);
}
);
console.log( msg );
client.Close();
}
main()
.then(() => {
Expand Down
28 changes: 16 additions & 12 deletions src/discord.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
OnModal,
MJShorten,
} from "./interfaces";

import { MidjourneyApi } from "./midjourne.api";
import {
content2progress,
Expand All @@ -30,12 +29,13 @@ export class WsMessage {
private waitMjEvents: Map<string, WaitMjEvent> = new Map();
private reconnectTime: boolean[] = [];
private heartbeatInterval = 0;

public UserId = "";
constructor(public config: MJConfig, public MJApi: MidjourneyApi) {
this.ws = new this.config.WebSocket(this.config.WsBaseUrl);
this.ws.addEventListener("open", this.open.bind(this));
this.onSystem("messageCreate", this.onMessageCreate.bind(this));
this.onSystem("messageUpdate", this.onMessageUpdate.bind(this));
this.onSystem("ready", this.onReady.bind(this));
this.onSystem("interactionSuccess", this.onInteractionSuccess.bind(this));
}

Expand Down Expand Up @@ -114,7 +114,7 @@ export class WsMessage {
private async messageCreate(message: any) {
const { embeds, id, nonce, components, attachments } = message;
if (nonce) {
this.log("waiting start image or info or error");
// this.log("waiting start image or info or error");
this.updateMjEventIdByNonce(id, nonce);

if (embeds?.[0]) {
Expand Down Expand Up @@ -158,7 +158,7 @@ export class WsMessage {
this.messageUpdate(message);
}
private messageUpdate(message: any) {
// this.log("messageUpdate",message);
// this.log("messageUpdate", message);
const {
content,
embeds,
Expand Down Expand Up @@ -214,25 +214,29 @@ export class WsMessage {
nonce: string;
id: string;
}) {
this.log("interactionSuccess", nonce, id);
// this.log("interactionSuccess", nonce, id);
const event = this.getEventByNonce(nonce);
if (!event) {
return;
}
event.onmodal && event.onmodal(nonce, id);
}

private async onReady(user: any) {
this.UserId = user.id;
}
private async onMessageCreate(message: any) {
const { channel_id, author } = message;
console.log("author", author.id);
if (!(author && author.id === this.config.BotId)) return;
const { channel_id, author, interaction } = message;
if (channel_id !== this.config.ChannelId) return;
if (author?.id !== this.config.BotId) return;
if (interaction && interaction.user.id !== this.UserId) return;
this.messageCreate(message);
}

private async onMessageUpdate(message: any) {
const { channel_id, author } = message;
if (!(author && author.id === this.config.BotId)) return;
const { channel_id, author, interaction } = message;
if (channel_id !== this.config.ChannelId) return;
if (author?.id !== this.config.BotId) return;
if (interaction && interaction.user.id !== this.UserId) return;
this.messageUpdate(message);
}

Expand All @@ -243,7 +247,7 @@ export class WsMessage {
return;
}
const message = msg.d;
this.log("has message", msg.t);
this.log("message event", msg.t);
switch (msg.t) {
case "READY":
this.emitSystem("ready", message.user);
Expand Down
1 change: 0 additions & 1 deletion src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ export class Midjourney extends MidjourneyMessage {
if (content === undefined || content === "") {
return "";
}
console.log("onmodalonmodal", nonde, id);
const newNonce = nextNonce();
switch (custom2Type(customId)) {
case "customZoom":
Expand Down

0 comments on commit 72e477e

Please sign in to comment.