Skip to content

Commit

Permalink
Support custom SESSION_ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed May 14, 2023
1 parent f76574a commit dad12fc
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 65 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
SALAI_TOKEN= [Token of the Account from which you paid MidJourney]
SERVER_ID= [Server id here]
CHANNEL_ID= [Channel in which commands are sent]
SESSION_ID="8bb7f5b79c7a49f7d0824ab4b8773a81"
2 changes: 1 addition & 1 deletion .github/workflows/releases.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Node.js Package
env:
APPVERSION: v1.1.${{ github.run_number }}
APPVERSION: v2.0.${{ github.run_number }}
on:
workflow_dispatch:
push:
Expand Down
12 changes: 6 additions & 6 deletions example/imagine-uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { Midjourney } from "../src";
* ```
*/
async function main() {
const client = new Midjourney(
<string>process.env.SERVER_ID,
<string>process.env.CHANNEL_ID,
<string>process.env.SALAI_TOKEN,
true
);
const client = new Midjourney({
ServerId: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
});
const msg = await client.Imagine(
"https://media.discordapp.net/attachments/1094892992281718894/1106660210380132503/Soga_A_Greek_man_with_mustache_in_national_costume_riding_a_don_3255e7c1-38ee-4892-b7c7-9f0dc3f2786d.png?width=1040&height=1040 https://cdn.discordapp.com/attachments/1094892992281718894/1106798152188702720/Soga__489d80b2-db74-4a93-a998-881a9542abbe.png cool boy",
(uri: string, progress: string) => {
Expand Down
16 changes: 8 additions & 8 deletions example/imagine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { Midjourney } from "../src";
* ```
*/
async function main() {
const client = new Midjourney(
<string>process.env.SERVER_ID,
<string>process.env.CHANNEL_ID,
<string>process.env.SALAI_TOKEN,
true
);
client.Limit = 50;
client.maxWait = 200;
const client = new Midjourney({
ServerId: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
SessionId: process.env.SALAI_TOKEN || "8bb7f5b79c7a49f7d0824ab4b8773a81",
});

const msg = await client.Imagine(
"A little white elephant",
(uri: string, progress: string) => {
Expand Down
21 changes: 21 additions & 0 deletions example/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "dotenv/config";
import { Midjourney, MidjourneyMessage } from "../src";
/**
*
* a simple example of how to use the imagine command
* ```
* npx tsx example/message.ts
* ```
*/
async function main() {
const client = new MidjourneyMessage({
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
});
const msg = await client.RetrieveMessages();
console.log({ msg });
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
12 changes: 6 additions & 6 deletions example/upscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { Midjourney } from "../src";
* ```
*/
async function main() {
const client = new Midjourney(
<string>process.env.SERVER_ID,
<string>process.env.CHANNEL_ID,
<string>process.env.SALAI_TOKEN,
true
);
const client = new Midjourney({
ServerId: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
});
const msg = await client.Imagine("a cool cat, blue ears, yellow hat");
console.log({ msg });
if (!msg) {
Expand Down
12 changes: 6 additions & 6 deletions example/variation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { Midjourney } from "../src";
* ```
*/
async function main() {
const client = new Midjourney(
<string>process.env.SERVER_ID,
<string>process.env.CHANNEL_ID,
<string>process.env.SALAI_TOKEN,
true
);
const client = new Midjourney({
ServerId: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
Debug: true,
});
const msg = await client.Imagine("a dog, blue ears, and a red nose");
console.log({ msg });
if (!msg) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "midjourney",
"version": "1.1.0",
"version": "2.0.0",
"description": "Node.js client for the unofficial MidJourney API.",
"main": "libs/index.js",
"types": "libs/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./message";
export * from "./message.config";
35 changes: 35 additions & 0 deletions src/interfaces/message.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export interface MessageConfig {
ChannelId: string;
SalaiToken: string;
Debug: boolean;
Limit: number;
MaxWait: number;
}
export interface MessageConfigParam {
ChannelId: string;
SalaiToken: string;
Debug?: boolean;
Limit?: number;
MaxWait?: number;
}
export interface MidjourneyConfig extends MessageConfig {
ServerId: string;
SessionId: string;
}

export interface MidjourneyConfigParam extends MessageConfigParam {
ServerId: string;
SessionId?: string;
}
export const DefaultMessageConfig: MessageConfig = {
ChannelId: "",
SalaiToken: "",
Debug: false,
Limit: 50,
MaxWait: 100,
};
export const DefaultMidjourneyConfig: MidjourneyConfig = {
...DefaultMessageConfig,
ServerId: "",
SessionId: "8bb7f5b79c7a49f7d0824ab4b8773a81",
};
41 changes: 23 additions & 18 deletions src/midjourney.message.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { LoadingHandler, MJMessage } from "./interfaces";
import {
DefaultMessageConfig,
LoadingHandler,
MJMessage,
MessageConfig,
MessageConfigParam,
MidjourneyConfig,
} from "./interfaces";
import { CreateQueue } from "./queue";
import { sleep } from "./utls";

export class MidjourneyMessage {
private magApiQueue = CreateQueue(1);
constructor(
public ChannelId: string,
protected SalaiToken: string,
public debug = false,
public Limit = 50,
public maxWait = 100
) {
this.log("MidjourneyMessage constructor");
public config: MessageConfig;
constructor(defaults: MessageConfigParam) {
this.config = {
...DefaultMessageConfig,
...defaults,
};
}
protected log(...args: any[]) {
this.debug && console.log(...args, new Date().toISOString());
this.config.Debug && console.log(...args, new Date().toISOString());
}
async FilterMessages(
prompt: string,
Expand All @@ -28,7 +33,7 @@ export class MidjourneyMessage {
// remove multiple spaces
prompt = prompt.trim();

const data = await this.safeRetrieveMessages(this.Limit);
const data = await this.safeRetrieveMessages(this.config.Limit);
for (let i = 0; i < data.length; i++) {
const item = data[i];
if (
Expand Down Expand Up @@ -87,7 +92,7 @@ export class MidjourneyMessage {
return uri.split("_").pop()?.split(".")[0] ?? "";
}
async WaitMessage(prompt: string, loading?: LoadingHandler) {
for (let i = 0; i < this.maxWait; i++) {
for (let i = 0; i < this.config.MaxWait; i++) {
const msg = await this.FilterMessages(prompt, loading);
if (msg !== null) {
return msg;
Expand All @@ -102,7 +107,7 @@ export class MidjourneyMessage {
options: string,
loading?: LoadingHandler
) {
for (let i = 0; i < this.maxWait; i++) {
for (let i = 0; i < this.config.MaxWait; i++) {
const msg = await this.FilterMessages(content, loading, options);
if (msg !== null) {
return msg;
Expand All @@ -116,7 +121,7 @@ export class MidjourneyMessage {
index: number,
loading?: LoadingHandler
) {
for (let i = 0; i < this.maxWait; i++) {
for (let i = 0; i < this.config.MaxWait; i++) {
const msg = await this.FilterMessages(
content,
loading,
Expand All @@ -132,13 +137,13 @@ export class MidjourneyMessage {
}

// limit the number of concurrent interactions
protected async safeRetrieveMessages(limit = 50) {
protected async safeRetrieveMessages(limit = this.config.Limit) {
return this.magApiQueue.addTask(() => this.RetrieveMessages(limit));
}
async RetrieveMessages(limit = 50) {
const headers = { authorization: this.SalaiToken };
async RetrieveMessages(limit = this.config.Limit) {
const headers = { authorization: this.config.SalaiToken };
const response = await fetch(
`https://discord.com/api/v10/channels/${this.ChannelId}/messages?limit=${limit}`,
`https://discord.com/api/v10/channels/${this.config.ChannelId}/messages?limit=${limit}`,
{
headers: headers,
}
Expand Down
43 changes: 24 additions & 19 deletions src/midjourney.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { LoadingHandler } from "./interfaces";
import {
DefaultMidjourneyConfig,
LoadingHandler,
MidjourneyConfig,
MidjourneyConfigParam,
} from "./interfaces";
import { MidjourneyMessage } from "./midjourney.message";
import { CreateQueue } from "./queue";
import { random, sleep } from "./utls";
export class Midjourney extends MidjourneyMessage {
private ApiQueue = CreateQueue(1);
constructor(
public ServerId: string,
public ChannelId: string,
protected SalaiToken: string,
public debug = false
) {
super(ChannelId, SalaiToken, debug);
public config: MidjourneyConfig;
constructor(defaults: MidjourneyConfigParam) {
super(defaults);
this.config = {
...DefaultMidjourneyConfig,
...defaults,
};
}

async Imagine(prompt: string, loading?: LoadingHandler) {
Expand Down Expand Up @@ -47,7 +52,7 @@ export class Midjourney extends MidjourneyMessage {
try {
const headers = {
"Content-Type": "application/json",
Authorization: this.SalaiToken,
Authorization: this.config.SalaiToken,
};
const response = await fetch("https://discord.com/api/v9/interactions", {
method: "POST",
Expand All @@ -68,9 +73,9 @@ export class Midjourney extends MidjourneyMessage {
const payload = {
type: 2,
application_id: "936929561302675456",
guild_id: this.ServerId,
channel_id: this.ChannelId,
session_id: "2fb980f65e5c9a77c96ca01f2c242cf6",
guild_id: this.config.ServerId,
channel_id: this.config.ChannelId,
session_id: this.config.SessionId,
data: {
version: "1077969938624553050",
id: "938956540159881230",
Expand Down Expand Up @@ -130,8 +135,8 @@ export class Midjourney extends MidjourneyMessage {
async VariationApi(index: number, messageId: string, messageHash: string) {
const payload = {
type: 3,
guild_id: this.ServerId,
channel_id: this.ChannelId,
guild_id: this.config.ServerId,
channel_id: this.config.ChannelId,
message_flags: 0,
message_id: messageId,
application_id: "936929561302675456",
Expand Down Expand Up @@ -166,8 +171,8 @@ export class Midjourney extends MidjourneyMessage {
async UpscaleApi(index: number, messageId: string, messageHash: string) {
const payload = {
type: 3,
guild_id: this.ServerId,
channel_id: this.ChannelId,
guild_id: this.config.ServerId,
channel_id: this.config.ChannelId,
message_flags: 0,
message_id: messageId,
application_id: "936929561302675456",
Expand All @@ -182,12 +187,12 @@ export class Midjourney extends MidjourneyMessage {
async UpscaleByCustomID(messageId: string, customId: string) {
const payload = {
type: 3,
guild_id: this.ServerId,
channel_id: this.ChannelId,
guild_id: this.config.ServerId,
channel_id: this.config.ChannelId,
message_flags: 0,
message_id: messageId,
application_id: "936929561302675456",
session_id: "ec6524c8d2926e285a8232f7ed1ced98",
session_id: this.config.SessionId,
data: {
component_type: 2,
custom_id: customId,
Expand Down

0 comments on commit dad12fc

Please sign in to comment.