1
1
'use strict' ;
2
2
3
+ const { makeURLSearchParams } = require ( '@discordjs/rest' ) ;
3
4
const { isJSONEncodable } = require ( '@discordjs/util' ) ;
4
5
const { InteractionResponseType, MessageFlags, Routes, InteractionType } = require ( 'discord-api-types/v10' ) ;
5
6
const { DiscordjsError, ErrorCodes } = require ( '../../errors' ) ;
7
+ const InteractionCallbackResponse = require ( '../InteractionCallbackResponse' ) ;
6
8
const InteractionCollector = require ( '../InteractionCollector' ) ;
7
9
const InteractionResponse = require ( '../InteractionResponse' ) ;
8
10
const MessagePayload = require ( '../MessagePayload' ) ;
@@ -23,21 +25,21 @@ class InteractionResponses {
23
25
* Options for deferring the reply to an {@link BaseInteraction}.
24
26
* @typedef {Object } InteractionDeferReplyOptions
25
27
* @property {MessageFlagsResolvable } [flags] Flags for the reply.
28
+ * @property {boolean } [withResponse] Whether to return an {@link InteractionCallbackResponse} as the response
26
29
* <info>Only `MessageFlags.Ephemeral` can be set.</info>
27
- * @property {boolean } [fetchReply] Whether to fetch the reply
28
30
*/
29
31
30
32
/**
31
33
* Options for deferring and updating the reply to a {@link MessageComponentInteraction}.
32
34
* @typedef {Object } InteractionDeferUpdateOptions
33
- * @property {boolean } [fetchReply ] Whether to fetch the reply
35
+ * @property {boolean } [withResponse ] Whether to return an { @link InteractionCallbackResponse} as the response
34
36
*/
35
37
36
38
/**
37
39
* Options for a reply to a {@link BaseInteraction}.
38
40
* @typedef {BaseMessageOptionsWithPoll } InteractionReplyOptions
39
41
* @property {boolean } [tts=false] Whether the message should be spoken aloud
40
- * @property {boolean } [fetchReply ] Whether to fetch the reply
42
+ * @property {boolean } [withResponse ] Whether to return an { @link InteractionCallbackResponse} as the response
41
43
* @property {MessageFlagsResolvable } [flags] Which flags to set for the message.
42
44
* <info>Only `MessageFlags.Ephemeral`, `MessageFlags.SuppressEmbeds`, and `MessageFlags.SuppressNotifications`
43
45
* can be set.</info>
@@ -46,13 +48,19 @@ class InteractionResponses {
46
48
/**
47
49
* Options for updating the message received from a {@link MessageComponentInteraction}.
48
50
* @typedef {MessageEditOptions } InteractionUpdateOptions
49
- * @property {boolean } [fetchReply] Whether to fetch the reply
51
+ * @property {boolean } [withResponse] Whether to return an {@link InteractionCallbackResponse} as the response
52
+ */
53
+
54
+ /**
55
+ * Options for showing a modal in response to a {@link BaseInteraction}
56
+ * @typedef {Object } ShowModalOptions
57
+ * @property {boolean } [withResponse] Whether to return an {@link InteractionCallbackResponse} as the response
50
58
*/
51
59
52
60
/**
53
61
* Defers the reply to this interaction.
54
62
* @param {InteractionDeferReplyOptions } [options] Options for deferring the reply to this interaction
55
- * @returns {Promise<Message| InteractionResponse> }
63
+ * @returns {Promise<InteractionResponse|InteractionCallbackResponse > }
56
64
* @example
57
65
* // Defer the reply to this interaction
58
66
* interaction.deferReply()
@@ -67,30 +75,34 @@ class InteractionResponses {
67
75
async deferReply ( options = { } ) {
68
76
if ( this . deferred || this . replied ) throw new DiscordjsError ( ErrorCodes . InteractionAlreadyReplied ) ;
69
77
70
- await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
78
+ const response = await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
71
79
body : {
72
80
type : InteractionResponseType . DeferredChannelMessageWithSource ,
73
81
data : {
74
82
flags : options . flags ,
75
83
} ,
76
84
} ,
77
85
auth : false ,
86
+ query : makeURLSearchParams ( { with_response : options . withResponse ?? false } ) ,
78
87
} ) ;
79
88
80
89
this . deferred = true ;
81
90
this . ephemeral = Boolean ( options . flags & MessageFlags . Ephemeral ) ;
82
- return options . fetchReply ? this . fetchReply ( ) : new InteractionResponse ( this ) ;
91
+
92
+ return options . withResponse
93
+ ? new InteractionCallbackResponse ( this . client , response )
94
+ : new InteractionResponse ( this ) ;
83
95
}
84
96
85
97
/**
86
98
* Creates a reply to this interaction.
87
- * <info>Use the `fetchReply ` option to get the bot's reply message .</info>
99
+ * <info>Use the `withResponse ` option to get the interaction callback response .</info>
88
100
* @param {string|MessagePayload|InteractionReplyOptions } options The options for the reply
89
- * @returns {Promise<Message| InteractionResponse> }
101
+ * @returns {Promise<InteractionResponse|InteractionCallbackResponse > }
90
102
* @example
91
103
* // Reply to the interaction and fetch the response
92
- * interaction.reply({ content: 'Pong!', fetchReply : true })
93
- * .then((message ) => console.log(`Reply sent with content ${message.content}`))
104
+ * interaction.reply({ content: 'Pong!', withResponse : true })
105
+ * .then((response ) => console.log(`Reply sent with content ${response.resource. message.content}`))
94
106
* .catch(console.error);
95
107
* @example
96
108
* // Create an ephemeral reply with an embed
@@ -109,18 +121,22 @@ class InteractionResponses {
109
121
110
122
const { body : data , files } = await messagePayload . resolveBody ( ) . resolveFiles ( ) ;
111
123
112
- await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
124
+ const response = await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
113
125
body : {
114
126
type : InteractionResponseType . ChannelMessageWithSource ,
115
127
data,
116
128
} ,
117
129
files,
118
130
auth : false ,
131
+ query : makeURLSearchParams ( { with_response : options . withResponse ?? false } ) ,
119
132
} ) ;
120
133
121
134
this . ephemeral = Boolean ( options . flags & MessageFlags . Ephemeral ) ;
122
135
this . replied = true ;
123
- return options . fetchReply ? this . fetchReply ( ) : new InteractionResponse ( this ) ;
136
+
137
+ return options . withResponse
138
+ ? new InteractionCallbackResponse ( this . client , response )
139
+ : new InteractionResponse ( this ) ;
124
140
}
125
141
126
142
/**
@@ -192,7 +208,7 @@ class InteractionResponses {
192
208
/**
193
209
* Defers an update to the message to which the component was attached.
194
210
* @param {InteractionDeferUpdateOptions } [options] Options for deferring the update to this interaction
195
- * @returns {Promise<Message| InteractionResponse> }
211
+ * @returns {Promise<InteractionResponse|InteractionCallbackResponse > }
196
212
* @example
197
213
* // Defer updating and reset the component's loading state
198
214
* interaction.deferUpdate()
@@ -201,21 +217,24 @@ class InteractionResponses {
201
217
*/
202
218
async deferUpdate ( options = { } ) {
203
219
if ( this . deferred || this . replied ) throw new DiscordjsError ( ErrorCodes . InteractionAlreadyReplied ) ;
204
- await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
220
+ const response = await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
205
221
body : {
206
222
type : InteractionResponseType . DeferredMessageUpdate ,
207
223
} ,
208
224
auth : false ,
225
+ query : makeURLSearchParams ( { with_response : options . withResponse ?? false } ) ,
209
226
} ) ;
210
227
this . deferred = true ;
211
228
212
- return options . fetchReply ? this . fetchReply ( ) : new InteractionResponse ( this , this . message ?. interaction ?. id ) ;
229
+ return options . withResponse
230
+ ? new InteractionCallbackResponse ( this . client , response )
231
+ : new InteractionResponse ( this , this . message ?. interaction ?. id ) ;
213
232
}
214
233
215
234
/**
216
235
* Updates the original message of the component on which the interaction was received on.
217
236
* @param {string|MessagePayload|InteractionUpdateOptions } options The options for the updated message
218
- * @returns {Promise<Message|void > }
237
+ * @returns {Promise<InteractionResponse|InteractionCallbackResponse > }
219
238
* @example
220
239
* // Remove the components from the message
221
240
* interaction.update({
@@ -234,34 +253,41 @@ class InteractionResponses {
234
253
235
254
const { body : data , files } = await messagePayload . resolveBody ( ) . resolveFiles ( ) ;
236
255
237
- await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
256
+ const response = await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
238
257
body : {
239
258
type : InteractionResponseType . UpdateMessage ,
240
259
data,
241
260
} ,
242
261
files,
243
262
auth : false ,
263
+ query : makeURLSearchParams ( { with_response : options . withResponse ?? false } ) ,
244
264
} ) ;
245
265
this . replied = true ;
246
266
247
- return options . fetchReply ? this . fetchReply ( ) : new InteractionResponse ( this , this . message . interaction ?. id ) ;
267
+ return options . withResponse
268
+ ? new InteractionCallbackResponse ( this . client , response )
269
+ : new InteractionResponse ( this , this . message . interaction ?. id ) ;
248
270
}
249
271
250
272
/**
251
273
* Shows a modal component
252
274
* @param {ModalBuilder|ModalComponentData|APIModalInteractionResponseCallbackData } modal The modal to show
253
- * @returns {Promise<void> }
275
+ * @param {ShowModalOptions } [options={}] The options for sending this interaction response
276
+ * @returns {Promise<InteractionCallbackResponse|undefined> }
254
277
*/
255
- async showModal ( modal ) {
278
+ async showModal ( modal , options = { } ) {
256
279
if ( this . deferred || this . replied ) throw new DiscordjsError ( ErrorCodes . InteractionAlreadyReplied ) ;
257
- await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
280
+ const response = await this . client . rest . post ( Routes . interactionCallback ( this . id , this . token ) , {
258
281
body : {
259
282
type : InteractionResponseType . Modal ,
260
283
data : isJSONEncodable ( modal ) ? modal . toJSON ( ) : this . client . options . jsonTransformer ( modal ) ,
261
284
} ,
262
285
auth : false ,
286
+ query : makeURLSearchParams ( { with_response : options . withResponse ?? false } ) ,
263
287
} ) ;
264
288
this . replied = true ;
289
+
290
+ return options . withResponse ? new InteractionCallbackResponse ( this . client , response ) : undefined ;
265
291
}
266
292
267
293
/**
0 commit comments