Skip to content

Commit c0eae34

Browse files
authored
feat: backport entrypoint command (#10908)
1 parent f2f757c commit c0eae34

File tree

13 files changed

+220
-21
lines changed

13 files changed

+220
-21
lines changed

packages/discord.js/src/client/actions/InteractionCreate.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const ChatInputCommandInteraction = require('../../structures/ChatInputCommandIn
99
const MentionableSelectMenuInteraction = require('../../structures/MentionableSelectMenuInteraction');
1010
const MessageContextMenuCommandInteraction = require('../../structures/MessageContextMenuCommandInteraction');
1111
const ModalSubmitInteraction = require('../../structures/ModalSubmitInteraction');
12+
const PrimaryEntryPointCommandInteraction = require('../../structures/PrimaryEntryPointCommandInteraction');
1213
const RoleSelectMenuInteraction = require('../../structures/RoleSelectMenuInteraction');
1314
const StringSelectMenuInteraction = require('../../structures/StringSelectMenuInteraction');
1415
const UserContextMenuCommandInteraction = require('../../structures/UserContextMenuCommandInteraction');
@@ -38,6 +39,9 @@ class InteractionCreateAction extends Action {
3839
if (channel && !channel.isTextBased()) return;
3940
InteractionClass = MessageContextMenuCommandInteraction;
4041
break;
42+
case ApplicationCommandType.PrimaryEntryPoint:
43+
InteractionClass = PrimaryEntryPointCommandInteraction;
44+
break;
4145
default:
4246
client.emit(
4347
Events.Debug,

packages/discord.js/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ exports.PartialGroupDMChannel = require('./structures/PartialGroupDMChannel');
180180
exports.PermissionOverwrites = require('./structures/PermissionOverwrites');
181181
exports.Poll = require('./structures/Poll').Poll;
182182
exports.PollAnswer = require('./structures/PollAnswer').PollAnswer;
183+
exports.PrimaryEntryPointCommandInteraction = require('./structures/PrimaryEntryPointCommandInteraction');
183184
exports.Presence = require('./structures/Presence').Presence;
184185
exports.ReactionCollector = require('./structures/ReactionCollector');
185186
exports.ReactionEmoji = require('./structures/ReactionEmoji');

packages/discord.js/src/managers/ApplicationCommandManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ class ApplicationCommandManager extends CachedManager {
261261
dm_permission: command.dmPermission ?? command.dm_permission,
262262
integration_types: command.integrationTypes ?? command.integration_types,
263263
contexts: command.contexts,
264+
handler: command.handler,
264265
};
265266
}
266267
}

packages/discord.js/src/structures/ApplicationCommand.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ class ApplicationCommand extends Base {
174174
this.contexts ??= null;
175175
}
176176

177+
if ('handler' in data) {
178+
/**
179+
* Determines whether the interaction is handled by the app's interactions handler or by Discord.
180+
* <info>Only available for {@link ApplicationCommandType.PrimaryEntryPoint} commands on
181+
* applications with the {@link ApplicationFlags.Embedded} flag (i.e, those that have an Activity)</info>
182+
* @type {?EntryPointCommandHandlerType}
183+
*/
184+
this.handler = data.handler;
185+
} else {
186+
this.handler ??= null;
187+
}
188+
177189
if ('version' in data) {
178190
/**
179191
* Autoincrementing version identifier updated during substantial record changes
@@ -216,15 +228,20 @@ class ApplicationCommand extends Base {
216228
* @property {string} name The name of the command, must be in all lowercase if type is
217229
* {@link ApplicationCommandType.ChatInput}
218230
* @property {Object<Locale, string>} [nameLocalizations] The localizations for the command name
219-
* @property {string} description The description of the command, if type is {@link ApplicationCommandType.ChatInput}
231+
* @property {string} description The description of the command,
232+
* if type is {@link ApplicationCommandType.ChatInput} or {@link ApplicationCommandType.PrimaryEntryPoint}
220233
* @property {boolean} [nsfw] Whether the command is age-restricted
221234
* @property {Object<Locale, string>} [descriptionLocalizations] The localizations for the command description,
222-
* if type is {@link ApplicationCommandType.ChatInput}
235+
* if type is {@link ApplicationCommandType.ChatInput} or {@link ApplicationCommandType.PrimaryEntryPoint}
223236
* @property {ApplicationCommandType} [type=ApplicationCommandType.ChatInput] The type of the command
224237
* @property {ApplicationCommandOptionData[]} [options] Options for the command
225238
* @property {?PermissionResolvable} [defaultMemberPermissions] The bitfield used to determine the default permissions
226239
* a member needs in order to run the command
227240
* @property {boolean} [dmPermission] Whether the command is enabled in DMs
241+
* @property {ApplicationIntegrationType[]} [integrationTypes] Installation contexts where the command is available
242+
* @property {InteractionContextType[]} [contexts] Interaction contexts where the command can be used
243+
* @property {EntryPointCommandHandlerType} [handler] Whether the interaction is handled by the app's
244+
* interactions handler or by Discord.
228245
*/
229246

230247
/**
@@ -419,7 +436,8 @@ class ApplicationCommand extends Base {
419436
this.descriptionLocalizations ?? {},
420437
) ||
421438
!isEqual(command.integrationTypes ?? command.integration_types ?? [], this.integrationTypes ?? []) ||
422-
!isEqual(command.contexts ?? [], this.contexts ?? [])
439+
!isEqual(command.contexts ?? [], this.contexts ?? []) ||
440+
('handler' in command && command.handler !== this.handler)
423441
) {
424442
return false;
425443
}

packages/discord.js/src/structures/BaseInteraction.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ class BaseInteraction extends Base {
225225
);
226226
}
227227

228+
/**
229+
* Indicates whether this interaction is a {@link PrimaryEntryPointCommandInteraction}
230+
* @returns {boolean}
231+
*/
232+
isPrimaryEntryPointCommand() {
233+
return (
234+
this.type === InteractionType.ApplicationCommand && this.commandType === ApplicationCommandType.PrimaryEntryPoint
235+
);
236+
}
237+
228238
/**
229239
* Indicates whether this interaction is a {@link MessageComponentInteraction}
230240
* @returns {boolean}

packages/discord.js/src/structures/CommandInteraction.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class CommandInteraction extends BaseInteraction {
152152
editReply() {}
153153
deleteReply() {}
154154
followUp() {}
155+
launchActivity() {}
155156
showModal() {}
156157
sendPremiumRequired() {}
157158
awaitModalSubmit() {}

packages/discord.js/src/structures/MessageComponentInteraction.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class MessageComponentInteraction extends BaseInteraction {
9797
followUp() {}
9898
deferUpdate() {}
9999
update() {}
100+
launchActivity() {}
100101
showModal() {}
101102
sendPremiumRequired() {}
102103
awaitModalSubmit() {}

packages/discord.js/src/structures/ModalSubmitInteraction.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class ModalSubmitInteraction extends BaseInteraction {
119119
deferUpdate() {}
120120
update() {}
121121
sendPremiumRequired() {}
122+
launchActivity() {}
122123
}
123124

124125
InteractionResponses.applyToClass(ModalSubmitInteraction, 'showModal');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const CommandInteraction = require('./CommandInteraction.js');
4+
5+
/**
6+
* Represents a primary entry point command interaction.
7+
* @extends {CommandInteraction}
8+
*/
9+
class PrimaryEntryPointCommandInteraction extends CommandInteraction {}
10+
11+
module.exports = PrimaryEntryPointCommandInteraction;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ class InteractionResponses {
6969
* <warn>This option is deprecated. Use `withResponse` or fetch the response instead.</warn>
7070
*/
7171

72+
/**
73+
* Options for launching activity in response to a {@link BaseInteraction}
74+
* @typedef {Object} LaunchActivityOptions
75+
* @property {boolean} [withResponse] Whether to return an {@link InteractionCallbackResponse} as the response
76+
*/
77+
7278
/**
7379
* Options for showing a modal in response to a {@link BaseInteraction}
7480
* @typedef {Object} ShowModalOptions
@@ -370,6 +376,25 @@ class InteractionResponses {
370376
: new InteractionResponse(this, this.message.interactionMetadata?.id);
371377
}
372378

379+
/**
380+
* Launches this application's activity, if enabled
381+
* @param {LaunchActivityOptions} [options={}] Options for launching the activity
382+
* @returns {Promise<InteractionCallbackResponse|undefined>}
383+
*/
384+
async launchActivity({ withResponse } = {}) {
385+
if (this.deferred || this.replied) throw new DiscordjsError(ErrorCodes.InteractionAlreadyReplied);
386+
const response = await this.client.rest.post(Routes.interactionCallback(this.id, this.token), {
387+
query: makeURLSearchParams({ with_response: withResponse ?? false }),
388+
body: {
389+
type: InteractionResponseType.LaunchActivity,
390+
},
391+
auth: false,
392+
});
393+
this.replied = true;
394+
395+
return withResponse ? new InteractionCallbackResponse(this.client, response) : undefined;
396+
}
397+
373398
/**
374399
* Shows a modal component
375400
* @param {ModalBuilder|ModalComponentData|APIModalInteractionResponseCallbackData} modal The modal to show
@@ -450,6 +475,7 @@ class InteractionResponses {
450475
'followUp',
451476
'deferUpdate',
452477
'update',
478+
'launchActivity',
453479
'showModal',
454480
'sendPremiumRequired',
455481
'awaitModalSubmit',

0 commit comments

Comments
 (0)