Skip to content

Commit

Permalink
feat(InteractionResponses): support with_response query parameter (#…
Browse files Browse the repository at this point in the history
…10636)

feat(InteractionResponses): support with_response query parameter

Co-authored-by: Ryan Munro <monbrey@gmail.com>
  • Loading branch information
Jiralite and monbrey authored Dec 18, 2024
1 parent b2754d4 commit 622acbc
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 20 deletions.
3 changes: 3 additions & 0 deletions packages/discord.js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,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');
exports.BaseInteraction = require('./structures/BaseInteraction');
exports.InteractionCollector = require('./structures/InteractionCollector');
exports.InteractionResponse = require('./structures/InteractionResponse');
Expand Down
74 changes: 74 additions & 0 deletions packages/discord.js/src/structures/InteractionCallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'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);
}
}

module.exports = InteractionCallback;
52 changes: 52 additions & 0 deletions packages/discord.js/src/structures/InteractionCallbackResource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'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;
33 changes: 33 additions & 0 deletions packages/discord.js/src/structures/InteractionCallbackResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'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

0 comments on commit 622acbc

Please sign in to comment.