Skip to content

Commit

Permalink
check ws connect && add proxy_url
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Jul 7, 2023
1 parent c0b1cc9 commit 5f65be0
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 98 deletions.
10 changes: 8 additions & 2 deletions example/customzoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ async function main() {
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);
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");
Expand Down
4 changes: 2 additions & 2 deletions example/imagine-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ async function main() {
SalaiToken: <string>process.env.SALAI_TOKEN,
HuggingFaceToken: <string>process.env.HUGGINGFACE_TOKEN,
Debug: true,
Ws: true, // required `Only you can see this`
Ws: true, // required `Only you can see this`
});
await client.Connect(); // required
const Imagine = await client.Imagine(
"Red hamster smoking a cigaret, https://media.discordapp.net/attachments/1108515696385720410/1118385339732590682/DanielH_A_giant_hamster_monster._Friendly_in_a_business_suit_si_d4be1836-a4e1-41a8-b1d7-99eebc521220.png?width=1878&height=1878 ",
"Red hamster smoking a cigaret",
(uri: string, progress: string) => {
console.log("Imagine.loading", uri, "progress", progress);
}
Expand Down
3 changes: 2 additions & 1 deletion src/discord.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class MidjourneyMessage {
for (let i = 0; i < data.length; i++) {
const item = data[i];
if (
item.author.id === "936929561302675456" &&
item.author.id === this.config.BotId &&
item.content.includes(`${seed}`)
) {
const itemTimestamp = new Date(item.timestamp).getTime();
Expand Down Expand Up @@ -64,6 +64,7 @@ export class MidjourneyMessage {
content,
id: item.id,
uri: imageUrl,
proxy_url: item.attachments[0].proxy_url,
flags: item.flags,
hash: this.UriToHash(imageUrl),
progress: "done",
Expand Down
7 changes: 4 additions & 3 deletions src/discord.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export class WsMessage {
if (nonce) {
// this.log("waiting start image or info or error");
this.updateMjEventIdByNonce(id, nonce);

if (embeds?.[0]) {
const { color, description, title } = embeds[0];
this.log("embeds[0].color", color);
Expand Down Expand Up @@ -229,7 +228,7 @@ export class WsMessage {
if (channel_id !== this.config.ChannelId) return;
if (author?.id !== this.config.BotId) return;
if (interaction && interaction.user.id !== this.UserId) return;
this.log('[messageCreate]', JSON.stringify(message));
this.log("[messageCreate]", JSON.stringify(message));
this.messageCreate(message);
}

Expand All @@ -238,7 +237,7 @@ export class WsMessage {
if (channel_id !== this.config.ChannelId) return;
if (author?.id !== this.config.BotId) return;
if (interaction && interaction.user.id !== this.UserId) return;
this.log('[messageUpdate]', JSON.stringify(message));
this.log("[messageUpdate]", JSON.stringify(message));
this.messageUpdate(message);
}

Expand Down Expand Up @@ -315,6 +314,7 @@ export class WsMessage {
hash: uriToHash(attachments[0].url),
progress: "done",
uri: attachments[0].url,
proxy_url: attachments[0].proxy_url,
options: formatOptions(components),
};
this.filterMessages(MJmsg);
Expand All @@ -336,6 +336,7 @@ export class WsMessage {
}
const MJmsg: MJMessage = {
uri: attachments[0].url,
proxy_url: attachments[0].proxy_url,
content: content,
flags: flags,
progress: content2progress(content),
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface MJMessage {
uri: string;
proxy_url?: string;
content: string;
flags: number;
id?: string;
Expand Down
87 changes: 51 additions & 36 deletions src/midjourne.api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { CustomZoomModalSubmitID, DescribeModalSubmitID, DiscordImage, MJConfig, ModalSubmitID, RemixModalSubmitID, ShortenModalSubmitID, UploadParam, UploadSlot } from "./interfaces";
import {
CustomZoomModalSubmitID,
DescribeModalSubmitID,
DiscordImage,
MJConfig,
ModalSubmitID,
RemixModalSubmitID,
ShortenModalSubmitID,
UploadParam,
UploadSlot,
} from "./interfaces";
import { CreateQueue } from "./queue";
import { nextNonce, sleep } from "./utls";
import * as fs from "fs";
Expand Down Expand Up @@ -40,7 +50,10 @@ export class MidjourneyApi extends Command {
}
);
if (response.status >= 400) {
console.error("api.error.config", { payload:JSON.stringify(payload), config: this.config });
console.error("api.error.config", {
payload: JSON.stringify(payload),
config: this.config,
});
}
callback && callback(response.status);
//discord api rate limit
Expand All @@ -55,7 +68,7 @@ export class MidjourneyApi extends Command {
const payload = await this.imaginePayload(prompt, nonce);
return this.safeIteractions(payload);
}
async SwitchRemixApi( nonce: string = nextNonce()) {
async SwitchRemixApi(nonce: string = nextNonce()) {
const payload = await this.PreferPayload(nonce);
return this.safeIteractions(payload);
}
Expand Down Expand Up @@ -134,14 +147,16 @@ export class MidjourneyApi extends Command {
flags: number;
nonce?: string;
}) {
if (!msgId) throw new Error("msgId is empty");
if (flags === undefined) throw new Error("flags is undefined");
const payload = {
type: 3,
nonce,
guild_id: this.config.ServerId,
channel_id: this.config.ChannelId,
message_flags: flags,
message_id: msgId,
application_id: "936929561302675456",
application_id: this.config.BotId,
session_id: this.config.SessionId,
data: {
component_type: 2,
Expand All @@ -152,25 +167,25 @@ export class MidjourneyApi extends Command {
}
//FIXME: get SubmitCustomId from discord api
async ModalSubmitApi({
nonce ,
nonce,
msgId,
customId,
prompt,
submitCustomId
}:{
submitCustomId,
}: {
nonce: string;
msgId: string;
customId: string;
prompt: string;
submitCustomId:ModalSubmitID;
submitCustomId: ModalSubmitID;
}) {
var payload = {
type: 5,
application_id: "936929561302675456",
application_id: this.config.BotId,
channel_id: this.config.ChannelId,
guild_id: this.config.ServerId,
data: {
id:msgId,
id: msgId,
custom_id: customId,
components: [
{
Expand All @@ -179,24 +194,24 @@ export class MidjourneyApi extends Command {
{
type: 4,
custom_id: submitCustomId,
value:prompt,
value: prompt,
},
],
},
],
},
session_id: this.config.SessionId,
nonce,
}
console.log("submitCustomId",JSON.stringify(payload))
};
console.log("submitCustomId", JSON.stringify(payload));
return this.safeIteractions(payload);
}
async RemixApi({
nonce ,
nonce,
msgId,
customId,
prompt,
}:{
}: {
nonce: string;
msgId: string;
customId: string;
Expand All @@ -207,73 +222,73 @@ export class MidjourneyApi extends Command {
msgId,
customId,
prompt,
submitCustomId:RemixModalSubmitID
})
submitCustomId: RemixModalSubmitID,
});
}
async ShortenImagineApi({
nonce ,
nonce,
msgId,
customId,
prompt,
}:{
}: {
nonce: string;
msgId: string;
customId: string;
prompt: string;
}){
}) {
return this.ModalSubmitApi({
nonce,
msgId,
customId,
prompt,
submitCustomId:ShortenModalSubmitID
})
submitCustomId: ShortenModalSubmitID,
});
}



async DescribeImagineApi({
nonce ,
nonce,
msgId,
customId,
prompt,
}:{
}: {
nonce: string;
msgId: string;
customId: string;
prompt: string;
}){
}) {
return this.ModalSubmitApi({
nonce,
msgId,
customId,
prompt,
submitCustomId:DescribeModalSubmitID
})
submitCustomId: DescribeModalSubmitID,
});
}

async CustomZoomImagineApi({
nonce ,
nonce,
msgId,
customId,
prompt,
}:{
}: {
nonce: string;
msgId: string;
customId: string;
prompt: string;
}){
customId = customId.replace("MJ::CustomZoom","MJ::OutpaintCustomZoomModal")
}) {
customId = customId.replace(
"MJ::CustomZoom",
"MJ::OutpaintCustomZoomModal"
);
return this.ModalSubmitApi({
nonce,
msgId,
customId,
prompt,
submitCustomId:CustomZoomModalSubmitID
})
submitCustomId: CustomZoomModalSubmitID,
});
}


async InfoApi(nonce?: string) {
const payload = await this.infoPayload(nonce);
return this.safeIteractions(payload);
Expand Down
Loading

0 comments on commit 5f65be0

Please sign in to comment.