Skip to content

Commit

Permalink
Upscale no msg return
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
Eric committed May 9, 2023
1 parent 5ce7ccc commit 42a189c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 49 deletions.
2 changes: 1 addition & 1 deletion example/upscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function main() {
<string>process.env.SALAI_TOKEN,
true
);
const msg = await client.Imagine("a cool cat, blue ears, red hat");
const msg = await client.Imagine("a cool cat, blue ears, yellow hat");
console.log({ msg });
if (!msg) {
console.log("no message");
Expand Down
36 changes: 32 additions & 4 deletions src/midjourney.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export class MidjourneyMessage {
async FilterMessages(
prompt: string,
loading?: (uri: string) => void,
options?: string
options?: string,
index?: number
) {
const data = await this.safeRetrieveMessages(this.Limit);
for (let i = 0; i < data.length; i++) {
Expand All @@ -28,8 +29,16 @@ export class MidjourneyMessage {
item.author.id === "936929561302675456" &&
item.content.includes(`**${prompt}`)
) {
// this.log(JSON.stringify(item))
if (options && !item.content.includes(options)) {
this.log(JSON.stringify(item));
// Upscaled or Variation
if (
options &&
!(
item.content.includes(options) ||
(options === "Upscaled" &&
item.content.includes(` - Image #${index}`))
)
) {
this.log("no options");
continue;
}
Expand All @@ -39,7 +48,7 @@ export class MidjourneyMessage {
}
const imageUrl = item.attachments[0].url;
if (!imageUrl.endsWith(".png")) {
loading && loading(imageUrl);
loading?.(imageUrl);
break;
}
const content = item.content.split("**")[1];
Expand Down Expand Up @@ -82,6 +91,25 @@ export class MidjourneyMessage {
await sleep(1000 * 2);
}
}
async WaitUpscaledMessage(
content: string,
index: number,
loading?: (uri: string) => void
) {
for (let i = 0; i < this.maxWait; i++) {
const msg = await this.FilterMessages(
content,
loading,
"Upscaled",
index
);
if (msg !== null) {
return msg;
}
this.log(i, content, "wait no message found");
await sleep(1000 * 2);
}
}

// limit the number of concurrent interactions
protected async safeRetrieveMessages(limit = 50) {
Expand Down
2 changes: 1 addition & 1 deletion src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class Midjourney extends MidjourneyMessage {
throw new Error(`VariationApi failed with status ${httpStatus}`);
}
this.log(`await generate image`);
return await this.WaitOptionMessage(content, `Upscaled`, loading);
return await this.WaitUpscaledMessage(content, index, loading);
}

async UpscaleApi(index: number, messageId: string, messageHash: string) {
Expand Down
87 changes: 44 additions & 43 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
type TaskFunction<T> = () => Promise<T>;

interface QueueTask<T> {
task: TaskFunction<T>;
callback: (result: T) => void;
import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of how to use the imagine command
* ```
* npx tsx test/test.ts
* ```
*/
async function main() {
const client = new Midjourney(
<string>process.env.SERVER_ID,
<string>process.env.CHANNEL_ID,
<string>process.env.SALAI_TOKEN,
true
);
const msg = await client.RetrieveMessages(10);
console.log(JSON.stringify(msg));
}
import async from "async";

const queue = async.queue(async ({ task, callback }: QueueTask<any>) => {
const result = await task();
callback(result);
}, 10);

const task1: TaskFunction<number> = async () => {
// do some async work
return 42;
};

queue.push({
task: task1,
callback: (result: number) => {
console.log(result); // output: 42
},
});
queue.push({
task: task1,
callback: (result: number) => {
console.log(result); // output: 42
},
});

queue.push({
task: task1,
callback: (result: number) => {
console.log(result); // output: 42
},
});

queue.push({
task: task1,
callback: (result: number) => {
console.log(result); // output: 42
},
});
// main().catch((err) => {
// console.error(err);
// process.exit(1);
// });
function test2() {
const item = {
content:
"**a cool cat, blue ears, blue hat --seed 9113 --v 5** - Image #2 <@1017020769332637730>",
};
const options = "Upscaled";
const index = 2;
// if (options === "Upscaled" && !item.content.includes(` - Image #${index}`)) {
// console.log("123123");
// }
if (
item.content.includes(options) ||
(options === "Upscaled" && item.content.includes(` - Image #${index}`))
) {
console.log("has options");
} else {
console.log("no options");
return;
}
console.log("no options");
}
test2();

0 comments on commit 42a189c

Please sign in to comment.