diff --git a/example/imagine-err.ts b/example/imagine-err.ts index 52789f6..d45b115 100644 --- a/example/imagine-err.ts +++ b/example/imagine-err.ts @@ -13,12 +13,13 @@ async function main() { ChannelId: process.env.CHANNEL_ID, SalaiToken: process.env.SALAI_TOKEN, Debug: true, - Ws: true, }); - - await client.Connect(); + await client.init(); const msg = await client.Imagine( - "https://edge-runtime.vercel.app/features/available-apis", + `https://images.guapitu.com/chatgpt/5b9b907a/d3297338-ae3e-4276-9bd9-3b6ca27cedcf.png + https://images.guapitu.com/chatgpt/762a2db4/459d52f1-23fd-41c3-a912-317e65155fcc.png + https://images.guapitu.com/chatgpt/f86613ac/2e2497ae-9906-44d9-8396-e41abab2f47b.png + cat`, (uri: string, progress: string) => { console.log("loading", uri, "progress", progress); } diff --git a/example/vary.ts b/example/vary.ts new file mode 100644 index 0000000..93f5213 --- /dev/null +++ b/example/vary.ts @@ -0,0 +1,73 @@ +import "dotenv/config"; +import { Midjourney } from "../src"; +import { sleep } from "../src/utls"; +/** + * + * a simple example of how to use the vary + * ``` + * npx tsx example/vary.ts + * ``` + */ +async function main() { + const client = new Midjourney({ + ServerId: process.env.SERVER_ID, + ChannelId: process.env.CHANNEL_ID, + SalaiToken: process.env.SALAI_TOKEN, + Debug: true, + Ws: true, //enable ws is required for custom zoom + }); + await client.init(); + const prompt = + "Christmas dinner with spaghetti with family in a cozy house, we see interior details , simple blue&white illustration"; + const Imagine = await client.Imagine( + prompt, + (uri: string, progress: string) => { + console.log("loading", uri, "progress", progress); + } + ); + console.log(Imagine); + if (!Imagine) { + console.log("no message"); + return; + } + const Upscale = await client.Upscale({ + index: 2, + msgId: Imagine.id, + hash: Imagine.hash, + flags: Imagine.flags, + loading: (uri: string, progress: string) => { + console.log("loading", uri, "progress", progress); + }, + }); + if (!Upscale) { + console.log("no message"); + return; + } + console.log(Upscale); + + const vary = Upscale?.options?.find((o) => o.label === "Vary (Strong)"); + if (!vary) { + console.log("no zoomout"); + return; + } + await sleep(1400); + const varyCustom = await client.Custom({ + msgId: Upscale.id, + flags: Upscale.flags, + content: `${prompt} --zoom 2`, + customId: vary.custom, + loading: (uri: string, progress: string) => { + console.log("loading", uri, "progress", progress); + }, + }); + console.log("vary (Strong)", varyCustom); + client.Close(); +} +main() + .then(() => { + console.log("done"); + }) + .catch((err) => { + console.error(err); + process.exit(1); + }); diff --git a/src/discord.ws.ts b/src/discord.ws.ts index 8d432c3..e68b206 100644 --- a/src/discord.ws.ts +++ b/src/discord.ws.ts @@ -48,6 +48,7 @@ export class WsMessage { this.reconnect(); return; } + this.log("heartbeat", this.heartbeatInterval); this.heartbeatInterval++; this.ws.send( JSON.stringify({ @@ -62,9 +63,25 @@ export class WsMessage { this.closed = true; this.ws.close(); } + async checkWs() { + if (this.closed) return; + if (this.ws.readyState !== this.ws.OPEN) { + this.reconnect(); + await this.onceReady(); + } + } + async onceReady() { + return new Promise((resolve) => { + this.once("ready", (user) => { + //print user nickname + console.log(`🎊 ws ready!!! Hi: ${user.global_name}`); + resolve(this); + }); + }); + } //try reconnect - private reconnect() { + reconnect() { if (this.closed) return; this.ws = new this.config.WebSocket(this.config.WsBaseUrl); this.heartbeatInterval = 0; @@ -201,6 +218,15 @@ export class WsMessage { return; } } + if (embeds?.[0]) { + var { description, title } = embeds[0]; + if (title === "Duplicate images detected") { + const error = new Error(description); + this.EventError(id, error); + return; + } + } + if (content) { this.processingImage(message); } @@ -249,7 +275,8 @@ export class WsMessage { return; } const message = msg.d; - // this.log("message event", msg.t); + this.log("event", msg.t); + // console.log(data); switch (msg.t) { case "READY": this.emitSystem("ready", message.user); diff --git a/src/midjourney.ts b/src/midjourney.ts index 317f318..0c080ac 100644 --- a/src/midjourney.ts +++ b/src/midjourney.ts @@ -35,14 +35,9 @@ export class Midjourney extends MidjourneyMessage { await this.MJApi.allCommand(); } if (this.wsClient) return this; - return new Promise((resolve) => { - this.wsClient = new WsMessage(this.config, this.MJApi); - this.wsClient.once("ready", (user) => { - //print user nickname - console.log(`🎊 ws ready!!! Hi: ${user.global_name}`); - resolve(this); - }); - }); + this.wsClient = new WsMessage(this.config, this.MJApi); + await this.wsClient.onceReady(); + return this; } async init() { await this.Connect();