Skip to content

Commit

Permalink
add setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed Jun 19, 2023
1 parent a07aa30 commit d8f3b88
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ npx tsx example/imagine-ws.ts
- [x] `/describe`
- [x] [proxy](https://github.com/erictik/midjourney-discord/blob/main/examples/proxy.ts)
- [x] autoload command payload
- [x] /settings `reset`

---
<a href='https://ko-fi.com/erictik' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi1.png?v=3' border='0' alt='Buy Me a Coffee' /></a>
## Star History
Expand Down
31 changes: 31 additions & 0 deletions example/reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of using the settings api
* ```
* npx tsx example/settings.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,
});
await client.Connect();
await client.Reset();
client.Close();
}
main()
.then(() => {
console.log("finished");
process.exit(0);
})
.catch((err) => {
console.log("finished");
console.error(err);
process.exit(1);
});
50 changes: 50 additions & 0 deletions example/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import "dotenv/config";
import { Midjourney } from "../src";
/**
*
* a simple example of using the settings api
* ```
* npx tsx example/settings.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,
});
await client.Connect();
const msg = await client.Settings();
console.log(msg);
if (!msg) {
return;
}
// //niji5
// const niji5 = msg.options.filter((x) => {
// return x.label === "Niji version 5";
// })[0];
// console.log(niji5);
// const httpstatus = await client.MJApi.CustomApi({
// msgId: msg.id,
// customId: niji5.custom,
// flags: msg.flags,
// });
// console.log({ httpstatus });
// const setting = await client.Settings();
// console.log({ setting });
//reset settings

client.Close();
}
main()
.then(() => {
console.log("finished");
process.exit(0);
})
.catch((err) => {
console.log("finished");
console.error(err);
process.exit(1);
});
9 changes: 8 additions & 1 deletion src/interfaces/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ export interface MJInfo {

export interface MJOptions {
label: string;
type: string;
type: number;
style: number;
custom: string;
}
export interface MJSettings {
content: string;
id: string;
flags: number;
options: MJOptions[];
}
4 changes: 4 additions & 0 deletions src/midjourne.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ export class MidjourneyApi extends Command {
const payload = await this.infoPayload(nonce);
return this.safeIteractions(payload);
}
async SettingsApi(nonce?: string) {
const payload = await this.settingsPayload(nonce);
return this.safeIteractions(payload);
}
async FastApi(nonce?: string) {
const payload = await this.fastPayload(nonce);
return this.safeIteractions(payload);
Expand Down
30 changes: 30 additions & 0 deletions src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ export class Midjourney extends MidjourneyMessage {
}
}

async Settings() {
const nonce = nextNonce();
const httpStatus = await this.MJApi.SettingsApi(nonce);
if (httpStatus !== 204) {
throw new Error(`ImagineApi failed with status ${httpStatus}`);
}
if (this.wsClient) {
return this.wsClient.waitSettings();
}
return null;
}
async Reset() {
const settings = await this.Settings();
if (!settings) {
throw new Error(`Settings not found`);
}
const reset = settings.options.find((o) => o.label === "Reset Settings");
if (!reset) {
throw new Error(`Reset Settings not found`);
}
const httpstatus = await this.MJApi.CustomApi({
msgId: settings.id,
customId: reset.custom,
flags: settings.flags,
});
if (httpstatus !== 204) {
throw new Error(`Reset failed with status ${httpstatus}`);
}
}

async Info() {
const nonce = nextNonce();
const httpStatus = await this.MJApi.InfoApi(nonce);
Expand Down
2 changes: 2 additions & 0 deletions src/utls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export const formatOptions = (components: any) => {
const item = formatOptions(component.components);
data = data.concat(item);
}
if (!component.custom_id) continue;
data.push({
type: component.type,
style: component.style,
label: component.label || component.emoji?.name,
custom: component.custom_id,
});
Expand Down
50 changes: 37 additions & 13 deletions src/ws.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LoadingHandler,
WsEventMsg,
MJInfo,
MJSettings,
} from "./interfaces";

import { MidjourneyApi } from "./midjourne.api";
Expand Down Expand Up @@ -136,21 +137,24 @@ export class WsMessage {
this.messageUpdate(message);
}
private messageUpdate(message: any) {
// this.log("messageUpdate", message);
// this.log("messageUpdate", JSON.stringify(message));

const { content, embeds, interaction, nonce, id } = message;
//settings
if (interaction.name === "settings" && !nonce) {
this.emit("settings", message);
return;
}
//describe
if (interaction.name === "describe" && !nonce) {
this.emitDescribe(id, embeds[0].description);
}
//info
if (interaction.name === "info" && !nonce) {
this.emit("info", embeds[0].description);
return;
}
if (content === "") {
//describe
if (interaction.name === "describe" && !nonce) {
this.emitDescribe(id, embeds[0].description);
}
if (embeds && embeds.length > 0 && embeds[0].color === 0) {
this.log(embeds[0].title, embeds[0].description);
//maybe info
if (embeds[0].title.includes("info")) {
this.emit("info", embeds[0].description);
return;
}
}
return;
}
this.processingImage(message);
Expand Down Expand Up @@ -359,6 +363,13 @@ export class WsMessage {
};
this.event.push({ event: "info", callback: once });
}
onceSettings(callback: (message: any) => void) {
const once = (message: any) => {
this.remove("settings", once);
callback(message);
};
this.event.push({ event: "settings", callback: once });
}
onceDescribe(nonce: string, callback: (data: any) => void) {
const once = (message: any) => {
this.remove(nonce, once);
Expand Down Expand Up @@ -420,6 +431,19 @@ export class WsMessage {
});
});
}
async waitSettings() {
return new Promise<MJSettings | null>((resolve, reject) => {
this.onceSettings((message) => {
resolve({
id: message.id,
flags: message.flags,
content: message,
options: formatOptions(message.components),
});
});
});
}

msg2Info(msg: string) {
let jsonResult: MJInfo = {
subscription: "",
Expand Down

0 comments on commit d8f3b88

Please sign in to comment.