Skip to content

Commit

Permalink
feat: support SetSuffix and add reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Yisheng Lu committed Jan 23, 2024
1 parent c2474a4 commit a1ff0d9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export class Command {
// command_ids: `${this.config.BotId}`,
});
if(!this.application_commands_res){
const url = `${this.config.DiscordBaseUrl}/api/v9/channels/${this.config.ChannelId}/application-command-index`;
let url = `${this.config.DiscordBaseUrl}/api/v9/channels/${this.config.ChannelId}/application-command-index`;
if(this.config.ServerId){
url=`${this.config.DiscordBaseUrl}/api/v9/guilds/${this.config.ServerId}/application-command-index`;
}
const response = await this.config.fetch(url, {
headers: { authorization: this.config.SalaiToken },
});
Expand Down Expand Up @@ -136,6 +139,23 @@ export class Command {
return this.data2Paylod(data, nonce);
}

async PreferSuffix(nonce?: string,value?:string) {
const data = await this.commandData("prefer", [
{
type: 1,
name: "suffix",
"options": [
{
"type": 3,
"name": "new_value",
"value": value
}
]
},
]);
return this.data2Paylod(data, nonce);
}

async shortenPayload(prompt: string, nonce?: string) {
const data = await this.commandData("shorten", [
{
Expand Down
14 changes: 12 additions & 2 deletions src/discord.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class WsMessage {
private skipMessageId: string[] = [];
private reconnectTime: boolean[] = [];
private heartbeatInterval = 0;
private hasReadyInit = false;
public UserId = "";
constructor(public config: MJConfig, public MJApi: MidjourneyApi) {
this.ws = new this.config.WebSocket(this.config.WsBaseUrl);
Expand Down Expand Up @@ -76,18 +77,22 @@ export class WsMessage {
}

async onceReady() {
const that=this;
return new Promise((resolve) => {
this.once("ready", (user) => {
//print user nickname
that.hasReadyInit=true;
console.log(`🎊 ws ready!!! Hi: ${user.global_name||user.email}`);
resolve(this);
});
});
}
//try reconnect
reconnect() {
return;
if (this.closed) return;
if(!this.hasReadyInit){
return;
}
console.log('-------- reconnect2',this.config?.ChannelId)
this.ws = new this.config.WebSocket(this.config.WsBaseUrl);
this.heartbeatInterval = 0;
this.ws.addEventListener("open", this.open.bind(this));
Expand Down Expand Up @@ -234,6 +239,11 @@ export class WsMessage {
this.emit("prefer-remix", content);
}
break;
case 'prefer suffix':
if (content != "") {
this.emit("prefer-suffix", content);
}
break;
case "shorten":
const shorten: MJShorten = {
description: embeds?.[0]?.description,
Expand Down
5 changes: 5 additions & 0 deletions src/midjourney.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class MidjourneyApi extends Command {
console.error("api.error.config", {
payload: JSON.stringify(payload),
config: this.config,

});
}
return response.status;
Expand Down Expand Up @@ -145,6 +146,10 @@ export class MidjourneyApi extends Command {
const payload = await this.PreferPayload(nonce);
return this.safeIteractions(payload);
}
async setPreferSuffix(nonce: string = nextNonce(),value:string){
const payload = await this.PreferSuffix(nonce,value);
return this.safeIteractions(payload);
}

async ShortenApi(prompt: string, nonce: string = nextNonce()) {
const payload = await this.shortenPayload(prompt, nonce);
Expand Down
9 changes: 9 additions & 0 deletions src/midjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ export class Midjourney extends MidjourneyMessage {
}
return wsClient.waitContent("prefer-remix");
}
async SetSuffix(value:string) {
const wsClient = await this.getWsClient();
const nonce = nextNonce();
const httpStatus = await this.MJApi.setPreferSuffix(nonce,value);
if (httpStatus !== 204) {
throw new Error(`------ RelaxApi failed with status ${httpStatus}`);
}
return wsClient.waitContent("prefer-suffix");
}
async Describe(imgUri: string) {
const wsClient = await this.getWsClient();
const nonce = nextNonce();
Expand Down

0 comments on commit a1ff0d9

Please sign in to comment.