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 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -146,6 +146,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
104 changes: 104 additions & 0 deletions packages/discord.js/src/structures/InteractionCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
'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 id of the channel the original interaction was sent in
* @type {?Snowflake}
*/
this.channelId = data.channel_id ?? null;

/**
* The id of the guild the original interaction was sent in
* @type {Snowflake}
*/
this.guildId = data.guild_id ?? null;

/**
* 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,51 @@
'use strict';
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,32 @@
'use strict';

const InteractionCallback = require('./InteractionCallback');
const InteractionCallbackResource = require('./InteractionCallbackResource');
/**
* 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;
Loading