Skip to content

Commit

Permalink
add vary example
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Jul 12, 2023
1 parent 91224f0 commit 71481f6
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 14 deletions.
9 changes: 5 additions & 4 deletions example/imagine-err.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ async function main() {
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>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);
}
Expand Down
73 changes: 73 additions & 0 deletions example/vary.ts
Original file line number Diff line number Diff line change
@@ -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: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>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: <string>Imagine.id,
hash: <string>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: <string>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);
});
31 changes: 29 additions & 2 deletions src/discord.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class WsMessage {
this.reconnect();
return;
}
this.log("heartbeat", this.heartbeatInterval);
this.heartbeatInterval++;
this.ws.send(
JSON.stringify({
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 3 additions & 8 deletions src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@ export class Midjourney extends MidjourneyMessage {
await this.MJApi.allCommand();
}
if (this.wsClient) return this;
return new Promise<Midjourney>((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();
Expand Down

0 comments on commit 71481f6

Please sign in to comment.