@@ -11,22 +11,17 @@ export class Midjourney {
11
11
12
12
protected readonly serverId : string ;
13
13
14
- protected readonly discordToken : string ;
14
+ protected readonly token : string ;
15
15
16
16
timeout : number ;
17
17
18
18
debugger : boolean ;
19
19
20
20
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 ;
27
22
this . channelId = channelId ;
28
23
this . serverId = serverId ;
29
- this . discordToken = discordToken ;
24
+ this . token = token ;
30
25
this . timeout = timeout ;
31
26
this . debugger = false ;
32
27
}
@@ -86,39 +81,38 @@ export class Midjourney {
86
81
body : JSON . stringify ( payload ) ,
87
82
headers : {
88
83
'Content-Type' : 'application/json' ,
89
- Authorization : this . discordToken ,
84
+ Authorization : this . token ,
90
85
} ,
91
86
} ) ;
92
87
if ( res . status >= 400 ) {
93
- let msg = '' ;
94
88
try {
95
89
const data = await res . json ( ) ;
96
90
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 ) ;
98
95
}
99
- msg = data ?. message ;
100
96
} catch ( e ) {
101
97
// catch JSON error
102
98
}
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 } ` ) ;
108
100
}
109
101
}
110
102
111
- async getMessages ( ) {
103
+ async getMessage ( prompt : string ) {
112
104
const res = await fetch (
113
105
`https://discord.com/api/v10/channels/${ this . channelId } /messages?limit=50` ,
114
106
{
115
107
headers : {
116
- Authorization : this . discordToken ,
108
+ Authorization : this . token ,
117
109
} ,
118
110
}
119
111
) ;
120
112
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 ;
122
116
}
123
117
124
118
/**
@@ -133,9 +127,8 @@ export class Midjourney {
133
127
try {
134
128
count += 1 ;
135
129
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 ) ;
139
132
if ( message && ! isInProgress ( message ) ) {
140
133
[ image ] = message . attachments ;
141
134
break ;
0 commit comments