Skip to content

Commit

Permalink
waitmessage add skipMessageId
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Jul 7, 2023
1 parent 5f65be0 commit a939132
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
12 changes: 8 additions & 4 deletions example/customzoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ async function main() {
console.log("Custom Zoom", zoomout2x);
client.Close();
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
main()
.then(() => {
console.log("done");
})
.catch((err) => {
console.error(err);
process.exit(1);
});
1 change: 1 addition & 0 deletions example/imagine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function main() {
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
Ws: false,
});

const msg = await client.Imagine(
Expand Down
12 changes: 11 additions & 1 deletion src/discord.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class WsMessage {
private event: Array<{ event: string; callback: (message: any) => void }> =
[];
private waitMjEvents: Map<string, WaitMjEvent> = new Map();
private skipMessageId: string[] = [];
private reconnectTime: boolean[] = [];
private heartbeatInterval = 0;
public UserId = "";
Expand Down Expand Up @@ -470,7 +471,12 @@ export class WsMessage {
this.waitMjEvents.set(nonce, { nonce });
this.event.push({ event: nonce, callback: once });
}

private removeSkipMessageId(messageId: string) {
const index = this.skipMessageId.findIndex((id) => id !== messageId);
if (index !== -1) {
this.skipMessageId.splice(index, 1);
}
}
private removeWaitMjEvent(nonce: string) {
this.waitMjEvents.delete(nonce);
}
Expand All @@ -489,13 +495,16 @@ export class WsMessage {
nonce,
prompt,
onmodal,
messageId,
loading,
}: {
nonce: string;
prompt?: string;
messageId?: string;
onmodal?: OnModal;
loading?: LoadingHandler;
}) {
if (messageId) this.skipMessageId.push(messageId);
return new Promise<MJMessage | null>((resolve, reject) => {
const handleImageMessage = ({ message, error }: MJEmit) => {
if (error) {
Expand All @@ -505,6 +514,7 @@ export class WsMessage {
}
if (message && message.progress === "done") {
this.removeWaitMjEvent(nonce);
messageId && this.removeSkipMessageId(messageId);
resolve(message);
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ export class Midjourney extends MidjourneyMessage {
return await wsClient.waitImageMessage({
nonce,
loading,
messageId: msgId,
prompt: content,
onmodal: async (nonde, id) => {
if (content === undefined || content === "") {
Expand Down

0 comments on commit a939132

Please sign in to comment.