Skip to content

Commit

Permalink
add ImageProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
zcpua committed Jul 16, 2023
1 parent 0f285e3 commit 44af65b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ SERVER_ID="Server id here"
CHANNEL_ID="Channel in which commands are sent"
SESSION_ID="session id here"
HUGGINGFACE_TOKEN="verify human https://huggingface.co/docs/hub/security-tokens"
IMAGE_PROXY="image proxy url here"
15 changes: 10 additions & 5 deletions src/discord.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,31 @@ export class MidjourneyMessage {
this.log("no attachment");
break;
}
const imageUrl = item.attachments[0].url;
//waiting
let uri = item.attachments[0].url;
if (this.config.ImageProxy !== "") {
uri = uri.replace(
"https://cdn.discordapp.com/",
this.config.ImageProxy
);
} //waiting
if (
item.attachments[0].filename.startsWith("grid") ||
item.components.length === 0
) {
this.log(`content`, item.content);
const progress = this.content2progress(item.content);
loading?.(imageUrl, progress);
loading?.(uri, progress);
break;
}
//finished
const content = item.content.split("**")[1];
const msg: MJMessage = {
content,
id: item.id,
uri: imageUrl,
uri: uri,
proxy_url: item.attachments[0].proxy_url,
flags: item.flags,
hash: this.UriToHash(imageUrl),
hash: this.UriToHash(uri),
progress: "done",
options: formatOptions(item.components),
};
Expand Down
24 changes: 20 additions & 4 deletions src/discord.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,18 @@ export class WsMessage {
this.emit("settings", message);
return;
case "describe":
// console.log("describe", "meseesage", message);
let uri = embeds?.[0]?.image?.url;
if (this.config.ImageProxy !== "") {
uri = uri.replace(
"https://cdn.discordapp.com/",
this.config.ImageProxy
);
}
const describe: MJDescribe = {
id: id,
flags: message.flags,
descriptions: embeds?.[0]?.description.split("\n\n"),
uri: embeds?.[0]?.image?.url,
uri: uri,
proxy_url: embeds?.[0]?.image?.proxy_url,
options: formatOptions(components),
};
Expand Down Expand Up @@ -378,13 +384,18 @@ export class WsMessage {

private done(message: any) {
const { content, id, attachments, components, flags } = message;
let uri = attachments[0].url;
if (this.config.ImageProxy !== "") {
uri = uri.replace("https://cdn.discordapp.com/", this.config.ImageProxy);
}

const MJmsg: MJMessage = {
id,
flags,
content,
hash: uriToHash(attachments[0].url),
progress: "done",
uri: attachments[0].url,
uri: uri,
proxy_url: attachments[0].proxy_url,
options: formatOptions(components),
};
Expand All @@ -405,8 +416,13 @@ export class WsMessage {
if (!attachments || attachments.length === 0) {
return;
}

let uri = attachments[0].url;
if (this.config.ImageProxy !== "") {
uri = uri.replace("https://cdn.discordapp.com/", this.config.ImageProxy);
}
const MJmsg: MJMessage = {
uri: attachments[0].url,
uri: uri,
proxy_url: attachments[0].proxy_url,
content: content,
flags: flags,
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface MJConfig {
fetch: FetchFn;
ApiInterval: number;
WebSocket: WebSocketCl;
ImageProxy: string;
}
export interface MJConfigParam {
SalaiToken: string; //DISCORD_TOKEN
Expand All @@ -35,6 +36,7 @@ export interface MJConfigParam {
HuggingFaceToken?: string; //HuggingFaceToken for verify human
SessionId?: string;
DiscordBaseUrl?: string;
ImageProxy?: string;
WsBaseUrl?: string;
fetch?: FetchFn; //Node.js<18 need node.fetch Or proxy
WebSocket?: WebSocketCl; //isomorphic-ws Or proxy
Expand All @@ -50,6 +52,7 @@ export const DefaultMJConfig: MJConfig = {
Limit: 50,
Ws: true,
MaxWait: 200,
ImageProxy: "",
DiscordBaseUrl: "https://discord.com",
WsBaseUrl: "wss://gateway.discord.gg?v=9&encoding=json&compress=gzip-stream",
fetch: fetch,
Expand Down

0 comments on commit 44af65b

Please sign in to comment.