diff --git a/README.md b/README.md index cf79077..3943d25 100644 --- a/README.md +++ b/README.md @@ -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` + --- Buy Me a Coffee ## Star History diff --git a/example/reset.ts b/example/reset.ts new file mode 100644 index 0000000..9f478d4 --- /dev/null +++ b/example/reset.ts @@ -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: process.env.SERVER_ID, + ChannelId: process.env.CHANNEL_ID, + SalaiToken: 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); + }); diff --git a/example/settings.ts b/example/settings.ts new file mode 100644 index 0000000..fd1907e --- /dev/null +++ b/example/settings.ts @@ -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: process.env.SERVER_ID, + ChannelId: process.env.CHANNEL_ID, + SalaiToken: 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); + }); diff --git a/src/interfaces/message.ts b/src/interfaces/message.ts index f165841..ce0bac4 100644 --- a/src/interfaces/message.ts +++ b/src/interfaces/message.ts @@ -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[]; +} diff --git a/src/midjourne.api.ts b/src/midjourne.api.ts index ce6e6cc..325c142 100644 --- a/src/midjourne.api.ts +++ b/src/midjourne.api.ts @@ -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); diff --git a/src/midjourney.ts b/src/midjourney.ts index e49bc71..7a244d3 100644 --- a/src/midjourney.ts +++ b/src/midjourney.ts @@ -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); diff --git a/src/utls/index.ts b/src/utls/index.ts index 040d43a..7c892e2 100644 --- a/src/utls/index.ts +++ b/src/utls/index.ts @@ -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, }); diff --git a/src/ws.message.ts b/src/ws.message.ts index 22d7db2..2b8f3e4 100644 --- a/src/ws.message.ts +++ b/src/ws.message.ts @@ -5,6 +5,7 @@ import { LoadingHandler, WsEventMsg, MJInfo, + MJSettings, } from "./interfaces"; import { MidjourneyApi } from "./midjourne.api"; @@ -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); @@ -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); @@ -420,6 +431,19 @@ export class WsMessage { }); }); } + async waitSettings() { + return new Promise((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: "",