Skip to content

Commit 19340e9

Browse files
committed
fix: rename discordToken to token and optimize the log
1 parent e32a2e6 commit 19340e9

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Midjourney } from 'midjourney-fetch'
1010
const midjourney = new Midjourney({
1111
channelId: 'your channelId',
1212
serverId: 'your serverId',
13-
discordToken: 'your discordToken',
13+
token: 'your token',
1414
})
1515

1616
const images = await midjourney.imagine('your prompt')

src/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type DeepPartial<T> = {
55
export interface MidjourneyProps {
66
channelId: string;
77
serverId: string;
8-
discordToken: string;
8+
token: string;
99
timeout?: number; // default timeout: 2 min
1010
}
1111

src/midjourney.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,17 @@ export class Midjourney {
1111

1212
protected readonly serverId: string;
1313

14-
protected readonly discordToken: string;
14+
protected readonly token: string;
1515

1616
timeout: number;
1717

1818
debugger: boolean;
1919

2020
constructor(props: MidjourneyProps) {
21-
const {
22-
channelId,
23-
serverId,
24-
discordToken,
25-
timeout = configs.timeout,
26-
} = props;
21+
const { channelId, serverId, token, timeout = configs.timeout } = props;
2722
this.channelId = channelId;
2823
this.serverId = serverId;
29-
this.discordToken = discordToken;
24+
this.token = token;
3025
this.timeout = timeout;
3126
this.debugger = false;
3227
}
@@ -86,39 +81,38 @@ export class Midjourney {
8681
body: JSON.stringify(payload),
8782
headers: {
8883
'Content-Type': 'application/json',
89-
Authorization: this.discordToken,
84+
Authorization: this.token,
9085
},
9186
});
9287
if (res.status >= 400) {
93-
let msg = '';
9488
try {
9589
const data = await res.json();
9690
if (this.debugger) {
97-
this.log('Request failed', JSON.stringify(data));
91+
this.log('Interactions failed', JSON.stringify(data));
92+
}
93+
if (data?.message) {
94+
throw new Error(data.message);
9895
}
99-
msg = data?.message;
10096
} catch (e) {
10197
// catch JSON error
10298
}
103-
if (msg) {
104-
throw new Error(msg);
105-
} else {
106-
throw new Error(`Interactions failed with ${res.status}`);
107-
}
99+
throw new Error(`Interactions failed with ${res.status}`);
108100
}
109101
}
110102

111-
async getMessages() {
103+
async getMessage(prompt: string) {
112104
const res = await fetch(
113105
`https://discord.com/api/v10/channels/${this.channelId}/messages?limit=50`,
114106
{
115107
headers: {
116-
Authorization: this.discordToken,
108+
Authorization: this.token,
117109
},
118110
}
119111
);
120112
const data: MessageItem[] = await res.json();
121-
return data;
113+
const message = findMessageByPrompt(data, prompt);
114+
this.log(JSON.stringify(message), '\n');
115+
return message;
122116
}
123117

124118
/**
@@ -133,9 +127,8 @@ export class Midjourney {
133127
try {
134128
count += 1;
135129
await new Promise((res) => setTimeout(res, configs.interval));
136-
const data = await this.getMessages();
137-
const message = findMessageByPrompt(data, prompt);
138-
this.log(count, JSON.stringify(message), '\n');
130+
this.log(count);
131+
const message = await this.getMessage(prompt);
139132
if (message && !isInProgress(message)) {
140133
[image] = message.attachments;
141134
break;

src/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { type MessageItem } from './interface';
22

3-
export const findMessageByPrompt = (messages: MessageItem[], prompt: string) =>
4-
messages.find(
3+
export const findMessageByPrompt = (
4+
messages: MessageItem[],
5+
prompt: string
6+
) => {
7+
// trim and merge spaces
8+
const filterPrompt = prompt.split(' ').filter(Boolean).join(' ');
9+
return messages.find(
510
(msg) =>
6-
msg.content.includes(prompt) && msg.author.id === '936929561302675456'
11+
msg.content.includes(filterPrompt) &&
12+
msg.author.id === '936929561302675456'
713
);
14+
};
815

916
export const isInProgress = (message: MessageItem) =>
1017
message.attachments.length === 0 ||

0 commit comments

Comments
 (0)