Skip to content

Commit 2b0944a

Browse files
monbreyJiralite
andauthored
feat(InteractionResponses)!: support with_response query parameter (#10499)
BREAKING CHANGE: `InteractionDeferUpdateOptions#fetchReply` was removed, use `InteractionDeferUpdateOptions#withResponse` instead --------- Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
1 parent 108943a commit 2b0944a

File tree

7 files changed

+320
-86
lines changed

7 files changed

+320
-86
lines changed

packages/discord.js/src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ exports.GuildScheduledEvent = require('./structures/GuildScheduledEvent').GuildS
143143
exports.GuildTemplate = require('./structures/GuildTemplate');
144144
exports.Integration = require('./structures/Integration');
145145
exports.IntegrationApplication = require('./structures/IntegrationApplication');
146+
exports.InteractionCallback = require('./structures/InteractionCallback');
147+
exports.InteractionCallbackResource = require('./structures/InteractionCallbackResource');
148+
exports.InteractionCallbackResponse = require('./structures/InteractionCallbackResponse');
146149
exports.BaseInteraction = require('./structures/BaseInteraction');
147150
exports.InteractionCollector = require('./structures/InteractionCollector');
148151
exports.InteractionResponse = require('./structures/InteractionResponse');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
const { DiscordSnowflake } = require('@sapphire/snowflake');
4+
5+
/**
6+
* Represents an interaction callback response from Discord
7+
*/
8+
class InteractionCallback {
9+
constructor(client, data) {
10+
/**
11+
* The client that instantiated this.
12+
* @name InteractionCallback#client
13+
* @type {Client}
14+
* @readonly
15+
*/
16+
Object.defineProperty(this, 'client', { value: client });
17+
18+
/**
19+
* The id of the original interaction response
20+
* @type {Snowflake}
21+
*/
22+
this.id = data.id;
23+
24+
/**
25+
* The type of the original interaction
26+
* @type {InteractionType}
27+
*/
28+
this.type = data.type;
29+
30+
/**
31+
* The instance id of the Activity if one was launched or joined
32+
* @type {?string}
33+
*/
34+
this.activityInstanceId = data.activity_instance_id ?? null;
35+
36+
/**
37+
* The id of the message that was created by the interaction
38+
* @type {?Snowflake}
39+
*/
40+
this.responseMessageId = data.response_message_id ?? null;
41+
42+
/**
43+
* Whether the message is in a loading state
44+
* @type {?boolean}
45+
*/
46+
this.responseMessageLoading = data.response_message_loading ?? null;
47+
48+
/**
49+
* Whether the response message was ephemeral
50+
* @type {?boolean}
51+
*/
52+
this.responseMessageEphemeral = data.response_message_ephemeral ?? null;
53+
}
54+
55+
/**
56+
* The timestamp the original interaction was created at
57+
* @type {number}
58+
* @readonly
59+
*/
60+
get createdTimestamp() {
61+
return DiscordSnowflake.timestampFrom(this.id);
62+
}
63+
64+
/**
65+
* The time the original interaction was created at
66+
* @type {Date}
67+
* @readonly
68+
*/
69+
get createdAt() {
70+
return new Date(this.createdTimestamp);
71+
}
72+
}
73+
74+
module.exports = InteractionCallback;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const { lazy } = require('@discordjs/util');
4+
5+
const getMessage = lazy(() => require('./Message').Message);
6+
7+
/**
8+
* Represents the resource that was created by the interaction response.
9+
*/
10+
class InteractionCallbackResource {
11+
constructor(client, data) {
12+
/**
13+
* The client that instantiated this
14+
* @name InteractionCallbackResource#client
15+
* @type {Client}
16+
* @readonly
17+
*/
18+
Object.defineProperty(this, 'client', { value: client });
19+
20+
/**
21+
* The interaction callback type
22+
* @type {InteractionResponseType}
23+
*/
24+
this.type = data.type;
25+
26+
/**
27+
* The Activity launched by an interaction
28+
* @typedef {Object} ActivityInstance
29+
* @property {string} id The instance id of the Activity
30+
*/
31+
32+
/**
33+
* Represents the Activity launched by this interaction
34+
* @type {?ActivityInstance}
35+
*/
36+
this.activityInstance = data.activity_instance ?? null;
37+
38+
if ('message' in data) {
39+
/**
40+
* The message created by the interaction
41+
* @type {?Message}
42+
*/
43+
this.message =
44+
this.client.channels.cache.get(data.message.channel_id)?.messages._add(data.message) ??
45+
new (getMessage())(client, data.message);
46+
} else {
47+
this.message = null;
48+
}
49+
}
50+
}
51+
52+
module.exports = InteractionCallbackResource;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const InteractionCallback = require('./InteractionCallback');
4+
const InteractionCallbackResource = require('./InteractionCallbackResource');
5+
6+
/**
7+
* Represents an interaction's response
8+
*/
9+
class InteractionCallbackResponse {
10+
constructor(client, data) {
11+
/**
12+
* The client that instantiated this
13+
* @name InteractionCallbackResponse#client
14+
* @type {Client}
15+
* @readonly
16+
*/
17+
Object.defineProperty(this, 'client', { value: client });
18+
19+
/**
20+
* The interaction object associated with the interaction callback response
21+
* @type {InteractionCallback}
22+
*/
23+
this.interaction = new InteractionCallback(client, data.interaction);
24+
25+
/**
26+
* The resource that was created by the interaction response
27+
* @type {?InteractionCallbackResource}
28+
*/
29+
this.resource = data.resource ? new InteractionCallbackResource(client, data.resource) : null;
30+
}
31+
}
32+
33+
module.exports = InteractionCallbackResponse;

packages/discord.js/src/structures/interfaces/InteractionResponses.js

+48-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22

3+
const { makeURLSearchParams } = require('@discordjs/rest');
34
const { isJSONEncodable } = require('@discordjs/util');
45
const { InteractionResponseType, MessageFlags, Routes, InteractionType } = require('discord-api-types/v10');
56
const { DiscordjsError, ErrorCodes } = require('../../errors');
7+
const InteractionCallbackResponse = require('../InteractionCallbackResponse');
68
const InteractionCollector = require('../InteractionCollector');
79
const InteractionResponse = require('../InteractionResponse');
810
const MessagePayload = require('../MessagePayload');
@@ -23,21 +25,21 @@ class InteractionResponses {
2325
* Options for deferring the reply to an {@link BaseInteraction}.
2426
* @typedef {Object} InteractionDeferReplyOptions
2527
* @property {MessageFlagsResolvable} [flags] Flags for the reply.
28+
* @property {boolean} [withResponse] Whether to return an {@link InteractionCallbackResponse} as the response
2629
* <info>Only `MessageFlags.Ephemeral` can be set.</info>
27-
* @property {boolean} [fetchReply] Whether to fetch the reply
2830
*/
2931

3032
/**
3133
* Options for deferring and updating the reply to a {@link MessageComponentInteraction}.
3234
* @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
3436
*/
3537

3638
/**
3739
* Options for a reply to a {@link BaseInteraction}.
3840
* @typedef {BaseMessageOptionsWithPoll} InteractionReplyOptions
3941
* @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
4143
* @property {MessageFlagsResolvable} [flags] Which flags to set for the message.
4244
* <info>Only `MessageFlags.Ephemeral`, `MessageFlags.SuppressEmbeds`, and `MessageFlags.SuppressNotifications`
4345
* can be set.</info>
@@ -46,13 +48,19 @@ class InteractionResponses {
4648
/**
4749
* Options for updating the message received from a {@link MessageComponentInteraction}.
4850
* @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
5058
*/
5159

5260
/**
5361
* Defers the reply to this interaction.
5462
* @param {InteractionDeferReplyOptions} [options] Options for deferring the reply to this interaction
55-
* @returns {Promise<Message|InteractionResponse>}
63+
* @returns {Promise<InteractionResponse|InteractionCallbackResponse>}
5664
* @example
5765
* // Defer the reply to this interaction
5866
* interaction.deferReply()
@@ -67,30 +75,34 @@ class InteractionResponses {
6775
async deferReply(options = {}) {
6876
if (this.deferred || this.replied) throw new DiscordjsError(ErrorCodes.InteractionAlreadyReplied);
6977

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), {
7179
body: {
7280
type: InteractionResponseType.DeferredChannelMessageWithSource,
7381
data: {
7482
flags: options.flags,
7583
},
7684
},
7785
auth: false,
86+
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
7887
});
7988

8089
this.deferred = true;
8190
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);
8395
}
8496

8597
/**
8698
* 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>
88100
* @param {string|MessagePayload|InteractionReplyOptions} options The options for the reply
89-
* @returns {Promise<Message|InteractionResponse>}
101+
* @returns {Promise<InteractionResponse|InteractionCallbackResponse>}
90102
* @example
91103
* // 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}`))
94106
* .catch(console.error);
95107
* @example
96108
* // Create an ephemeral reply with an embed
@@ -109,18 +121,22 @@ class InteractionResponses {
109121

110122
const { body: data, files } = await messagePayload.resolveBody().resolveFiles();
111123

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), {
113125
body: {
114126
type: InteractionResponseType.ChannelMessageWithSource,
115127
data,
116128
},
117129
files,
118130
auth: false,
131+
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
119132
});
120133

121134
this.ephemeral = Boolean(options.flags & MessageFlags.Ephemeral);
122135
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);
124140
}
125141

126142
/**
@@ -192,7 +208,7 @@ class InteractionResponses {
192208
/**
193209
* Defers an update to the message to which the component was attached.
194210
* @param {InteractionDeferUpdateOptions} [options] Options for deferring the update to this interaction
195-
* @returns {Promise<Message|InteractionResponse>}
211+
* @returns {Promise<InteractionResponse|InteractionCallbackResponse>}
196212
* @example
197213
* // Defer updating and reset the component's loading state
198214
* interaction.deferUpdate()
@@ -201,21 +217,24 @@ class InteractionResponses {
201217
*/
202218
async deferUpdate(options = {}) {
203219
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), {
205221
body: {
206222
type: InteractionResponseType.DeferredMessageUpdate,
207223
},
208224
auth: false,
225+
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
209226
});
210227
this.deferred = true;
211228

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);
213232
}
214233

215234
/**
216235
* Updates the original message of the component on which the interaction was received on.
217236
* @param {string|MessagePayload|InteractionUpdateOptions} options The options for the updated message
218-
* @returns {Promise<Message|void>}
237+
* @returns {Promise<InteractionResponse|InteractionCallbackResponse>}
219238
* @example
220239
* // Remove the components from the message
221240
* interaction.update({
@@ -234,34 +253,41 @@ class InteractionResponses {
234253

235254
const { body: data, files } = await messagePayload.resolveBody().resolveFiles();
236255

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), {
238257
body: {
239258
type: InteractionResponseType.UpdateMessage,
240259
data,
241260
},
242261
files,
243262
auth: false,
263+
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
244264
});
245265
this.replied = true;
246266

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);
248270
}
249271

250272
/**
251273
* Shows a modal component
252274
* @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>}
254277
*/
255-
async showModal(modal) {
278+
async showModal(modal, options = {}) {
256279
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), {
258281
body: {
259282
type: InteractionResponseType.Modal,
260283
data: isJSONEncodable(modal) ? modal.toJSON() : this.client.options.jsonTransformer(modal),
261284
},
262285
auth: false,
286+
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
263287
});
264288
this.replied = true;
289+
290+
return options.withResponse ? new InteractionCallbackResponse(this.client, response) : undefined;
265291
}
266292

267293
/**

0 commit comments

Comments
 (0)