Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(InteractionResponses): support with_response query parameter #10499

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/discord.js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ exports.GuildScheduledEvent = require('./structures/GuildScheduledEvent').GuildS
exports.GuildTemplate = require('./structures/GuildTemplate');
exports.Integration = require('./structures/Integration');
exports.IntegrationApplication = require('./structures/IntegrationApplication');
exports.InteractionCallback = require('./structures/InteractionCallback');
exports.InteractionCallbackResource = require('./structures/InteractionCallbackResource');
exports.InteractionCallbackResponse = require('./structures/InteractionCallbackResponse');
monbrey marked this conversation as resolved.
Show resolved Hide resolved
exports.BaseInteraction = require('./structures/BaseInteraction');
exports.InteractionCollector = require('./structures/InteractionCollector');
exports.InteractionResponse = require('./structures/InteractionResponse');
Expand Down
92 changes: 92 additions & 0 deletions packages/discord.js/src/structures/InteractionCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';

const { DiscordSnowflake } = require('@sapphire/snowflake');

/**
* Represents an interaction callback response from Discord
*/
class InteractionCallback {
constructor(client, data) {
/**
* The client that instantiated this.
* @name InteractionCallback#client
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: client });

/**
* The id of the original interaction response
* @type {Snowflake}
*/
this.id = data.id;

/**
* The type of the original interaction
* @type {InteractionType}
*/
this.type = data.type;

/**
* The instance id of the Activity if one was launched or joined
* @type {?string}
*/
this.activityInstanceId = data.activity_instance_id ?? null;

/**
* The id of the message that was created by the interaction
* @type {?Snowflake}
*/
this.responseMessageId = data.response_message_id ?? null;

/**
* Whether the message is in a loading state
* @type {?boolean}
*/
this.responseMessageLoading = data.response_message_loading ?? null;

/**
* Whether the response message was ephemeral
* @type {?boolean}
*/
this.responseMessageEphemeral = data.response_message_ephemeral ?? null;
}

/**
* The timestamp the original interaction was created at
* @type {number}
* @readonly
*/
get createdTimestamp() {
return DiscordSnowflake.timestampFrom(this.id);
}

/**
* The time the original interaction was created at
* @type {Date}
* @readonly
*/
get createdAt() {
return new Date(this.createdTimestamp);
}

/**
* The channel the original interaction was sent in
* @type {?TextBasedChannels}
* @readonly
*/
get channel() {
return this.client.channels.cache.get(this.channelId) ?? null;
}

/**
* The guild the original interaction was sent in
* @type {?Guild}
* @readonly
*/
get guild() {
return this.client.guilds.cache.get(this.guildId) ?? null;
}
}

module.exports = InteractionCallback;
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
monbrey marked this conversation as resolved.
Show resolved Hide resolved

const { lazy } = require('@discordjs/util');

const getMessage = lazy(() => require('./Message').Message);

/**
* Represents the resource that was created by the interaction response.
*/
class InteractionCallbackResource {
constructor(client, data) {
/**
* The client that instantiated this
* @name InteractionCallbackResource#client
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: client });

/**
* The interaction callback type
* @type {InteractionResponseType}
*/
this.type = data.type;

/**
* The Activity launched by an interaction
* @typedef {Object} ActivityInstance
* @property {string} id The instance id of the Activity
*/

/**
* Represents the Activity launched by this interaction
* @type {?ActivityInstance}
*/
this.activityInstance = data.activity_instance ?? null;

if ('message' in data) {
/**
* The message created by the interaction
* @type {?Message}
*/
this.message =
this.client.channels.cache.get(data.message.channel_id)?.messages._add(data.message) ??
new (getMessage())(client, data.message);
} else {
this.message = null;
}
}
}

module.exports = InteractionCallbackResource;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const InteractionCallback = require('./InteractionCallback');
const InteractionCallbackResource = require('./InteractionCallbackResource');

/**
monbrey marked this conversation as resolved.
Show resolved Hide resolved
* Represents an interaction's response
*/
class InteractionCallbackResponse {
constructor(client, data) {
/**
* The client that instantiated this
* @name InteractionCallbackResponse#client
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: client });

/**
* The interaction object associated with the interaction callback response
* @type {InteractionCallback}
*/
this.interaction = new InteractionCallback(client, data.interaction);

/**
* The resource that was created by the interaction response
* @type {?InteractionCallbackResource}
*/
this.resource = data.resource ? new InteractionCallbackResource(client, data.resource) : null;
}
}

module.exports = InteractionCallbackResponse;
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const { makeURLSearchParams } = require('@discordjs/rest');
const { isJSONEncodable } = require('@discordjs/util');
const { InteractionResponseType, MessageFlags, Routes, InteractionType } = require('discord-api-types/v10');
const { DiscordjsError, ErrorCodes } = require('../../errors');
const InteractionCallbackResponse = require('../InteractionCallbackResponse');
const InteractionCollector = require('../InteractionCollector');
const InteractionResponse = require('../InteractionResponse');
const MessagePayload = require('../MessagePayload');
Expand All @@ -24,20 +26,19 @@ class InteractionResponses {
* @typedef {Object} InteractionDeferReplyOptions
* @property {MessageFlagsResolvable} [flags] Flags for the reply.
* <info>Only `MessageFlags.Ephemeral` can be set.</info>
* @property {boolean} [fetchReply] Whether to fetch the reply
*/

/**
* Options for deferring and updating the reply to a {@link MessageComponentInteraction}.
* @typedef {Object} InteractionDeferUpdateOptions
* @property {boolean} [fetchReply] Whether to fetch the reply
* @property {boolean} [withResponse] Whether to return an {@link InteractionCallbackResponse} as the response
*/

/**
* Options for a reply to a {@link BaseInteraction}.
* @typedef {BaseMessageOptionsWithPoll} InteractionReplyOptions
* @property {boolean} [tts=false] Whether the message should be spoken aloud
* @property {boolean} [fetchReply] Whether to fetch the reply
* @property {boolean} [withResponse] Whether to return an {@link InteractionCallbackResponse} as the response
* @property {MessageFlagsResolvable} [flags] Which flags to set for the message.
* <info>Only `MessageFlags.Ephemeral`, `MessageFlags.SuppressEmbeds`, and `MessageFlags.SuppressNotifications`
* can be set.</info>
Expand All @@ -46,13 +47,19 @@ class InteractionResponses {
/**
* Options for updating the message received from a {@link MessageComponentInteraction}.
* @typedef {MessageEditOptions} InteractionUpdateOptions
* @property {boolean} [fetchReply] Whether to fetch the reply
* @property {boolean} [withResponse] Whether to return an {@link InteractionCallbackResponse} as the response
*/

/**
* Options for showing a modal in response to a {@link BaseInteraction}
* @typedef {Object} ShowModalOptions
* @property {boolean} [withResponse] Whether to return an {@link InteractionCallbackResponse} as the response
*/

/**
* Defers the reply to this interaction.
* @param {InteractionDeferReplyOptions} [options] Options for deferring the reply to this interaction
* @returns {Promise<Message|InteractionResponse>}
* @returns {Promise<InteractionResponse|InteractionCallbackResponse>}
* @example
* // Defer the reply to this interaction
* interaction.deferReply()
Expand All @@ -67,30 +74,34 @@ class InteractionResponses {
async deferReply(options = {}) {
if (this.deferred || this.replied) throw new DiscordjsError(ErrorCodes.InteractionAlreadyReplied);

await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
const response = await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
body: {
type: InteractionResponseType.DeferredChannelMessageWithSource,
data: {
flags: options.flags,
},
},
auth: false,
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
});

this.deferred = true;
this.ephemeral = Boolean(options.flags & MessageFlags.Ephemeral);
return options.fetchReply ? this.fetchReply() : new InteractionResponse(this);

return options.withResponse
? new InteractionCallbackResponse(this.client, response)
: new InteractionResponse(this);
}

/**
* Creates a reply to this interaction.
* <info>Use the `fetchReply` option to get the bot's reply message.</info>
* <info>Use the `withResponse` option to get the interaction callback response.</info>
* @param {string|MessagePayload|InteractionReplyOptions} options The options for the reply
* @returns {Promise<Message|InteractionResponse>}
* @returns {Promise<InteractionResponse|InteractionCallbackResponse>}
* @example
* // Reply to the interaction and fetch the response
* interaction.reply({ content: 'Pong!', fetchReply: true })
* .then((message) => console.log(`Reply sent with content ${message.content}`))
* interaction.reply({ content: 'Pong!', withResponse: true })
* .then((response) => console.log(`Reply sent with content ${response.resource.message.content}`))
* .catch(console.error);
* @example
* // Create an ephemeral reply with an embed
Expand All @@ -109,18 +120,22 @@ class InteractionResponses {

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

await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
const response = await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
body: {
type: InteractionResponseType.ChannelMessageWithSource,
data,
},
files,
auth: false,
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
});

this.ephemeral = Boolean(options.flags & MessageFlags.Ephemeral);
this.replied = true;
return options.fetchReply ? this.fetchReply() : new InteractionResponse(this);

return options.withResponse
? new InteractionCallbackResponse(this.client, response)
: new InteractionResponse(this);
}

/**
Expand Down Expand Up @@ -192,7 +207,7 @@ class InteractionResponses {
/**
* Defers an update to the message to which the component was attached.
* @param {InteractionDeferUpdateOptions} [options] Options for deferring the update to this interaction
* @returns {Promise<Message|InteractionResponse>}
* @returns {Promise<InteractionResponse|InteractionCallbackResponse>}
* @example
* // Defer updating and reset the component's loading state
* interaction.deferUpdate()
Expand All @@ -201,21 +216,24 @@ class InteractionResponses {
*/
async deferUpdate(options = {}) {
if (this.deferred || this.replied) throw new DiscordjsError(ErrorCodes.InteractionAlreadyReplied);
await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
const response = await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
body: {
type: InteractionResponseType.DeferredMessageUpdate,
},
auth: false,
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
});
this.deferred = true;

return options.fetchReply ? this.fetchReply() : new InteractionResponse(this, this.message?.interaction?.id);
return options.withResponse
? new InteractionCallbackResponse(this.client, response)
: new InteractionResponse(this, this.message?.interaction?.id);
}

/**
* Updates the original message of the component on which the interaction was received on.
* @param {string|MessagePayload|InteractionUpdateOptions} options The options for the updated message
* @returns {Promise<Message|void>}
* @returns {Promise<InteractionResponse|InteractionCallbackResponse>}
* @example
* // Remove the components from the message
* interaction.update({
Expand All @@ -234,34 +252,41 @@ class InteractionResponses {

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

await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
const response = await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
body: {
type: InteractionResponseType.UpdateMessage,
data,
},
files,
auth: false,
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
});
this.replied = true;

return options.fetchReply ? this.fetchReply() : new InteractionResponse(this, this.message.interaction?.id);
return options.withResponse
? new InteractionCallbackResponse(this.client, response)
: new InteractionResponse(this, this.message.interaction?.id);
}

/**
* Shows a modal component
* @param {ModalBuilder|ModalComponentData|APIModalInteractionResponseCallbackData} modal The modal to show
* @returns {Promise<void>}
* @param {ShowModalOptions} [options={}] The options for sending this interaction response
* @returns {Promise<InteractionCallbackResponse|undefined>}
monbrey marked this conversation as resolved.
Show resolved Hide resolved
*/
async showModal(modal) {
async showModal(modal, options = {}) {
if (this.deferred || this.replied) throw new DiscordjsError(ErrorCodes.InteractionAlreadyReplied);
await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
const response = await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
body: {
type: InteractionResponseType.Modal,
data: isJSONEncodable(modal) ? modal.toJSON() : this.client.options.jsonTransformer(modal),
},
auth: false,
query: makeURLSearchParams({ with_response: options.withResponse ?? false }),
});
this.replied = true;

return options.withResponse ? new InteractionCallbackResponse(this.client, response) : undefined;
}

/**
Expand Down
Loading