Skip to content

Commit 85df0b0

Browse files
authored
Add support for Entry Point commands (#3021)
The discord docs are a little confusing for this feature, but I think it's basically pretty simple. Applications with activities enabled can define a global command of type `PrimaryEntryPoint` - based on the value of the `handler` field, this command can be treated essentially like normal, allowing the application to respond with a followup message when called; or, the behavior of the command can be automatically handled by Discord.
1 parent 17172da commit 85df0b0

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/builder/create_command.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ pub struct CreateCommand {
318318
#[serde(skip_serializing_if = "Option::is_none")]
319319
contexts: Option<Vec<InteractionContext>>,
320320
nsfw: bool,
321+
#[serde(skip_serializing_if = "Option::is_none")]
322+
handler: Option<EntryPointHandlerType>,
321323
}
322324

323325
impl CreateCommand {
@@ -338,6 +340,7 @@ impl CreateCommand {
338340

339341
options: Vec::new(),
340342
nsfw: false,
343+
handler: None,
341344
}
342345
}
343346

@@ -455,6 +458,15 @@ impl CreateCommand {
455458
self.nsfw = nsfw;
456459
self
457460
}
461+
462+
/// Sets the command's entry point handler type. Only valid for commands of type
463+
/// [`PrimaryEntryPoint`].
464+
///
465+
/// [`PrimaryEntryPoint`]: CommandType::PrimaryEntryPoint
466+
pub fn handler(mut self, handler: EntryPointHandlerType) -> Self {
467+
self.handler = Some(handler);
468+
self
469+
}
458470
}
459471

460472
#[cfg(feature = "http")]

src/builder/create_interaction_response.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ pub enum CreateInteractionResponse {
6666
/// Corresponds to Discord's `PREMIUM_REQUIRED'.
6767
#[deprecated = "use premium button components via `CreateButton::new_premium` instead"]
6868
PremiumRequired,
69+
/// Not valid for autocomplete and Ping interactions. Only available for applications with
70+
/// Activities enabled.
71+
///
72+
/// Responds to the interaction by launching the Activity associated with the app.
73+
///
74+
/// Corresponds to Discord's `LAUNCH_ACTIVITY`.
75+
LaunchActivity,
6976
}
7077

7178
impl serde::Serialize for CreateInteractionResponse {
@@ -83,6 +90,7 @@ impl serde::Serialize for CreateInteractionResponse {
8390
Self::Autocomplete(_) => 8,
8491
Self::Modal(_) => 9,
8592
Self::PremiumRequired => 10,
93+
Self::LaunchActivity => 12,
8694
})?;
8795

8896
match self {
@@ -94,6 +102,7 @@ impl serde::Serialize for CreateInteractionResponse {
94102
Self::Autocomplete(x) => map.serialize_entry("data", &x)?,
95103
Self::Modal(x) => map.serialize_entry("data", &x)?,
96104
Self::PremiumRequired => map.serialize_entry("data", &None::<()>)?,
105+
Self::LaunchActivity => map.serialize_entry("data", &None::<()>)?,
97106
}
98107

99108
map.end()

src/model/application/command.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ pub struct Command {
9494
pub contexts: Option<Vec<InteractionContext>>,
9595
/// An autoincremented version identifier updated during substantial record changes.
9696
pub version: CommandVersionId,
97+
/// Only present for commands of type [`PrimaryEntryPoint`].
98+
///
99+
/// [`PrimaryEntryPoint`]: CommandType::PrimaryEntryPoint
100+
pub handler: Option<EntryPointHandlerType>,
97101
}
98102

99103
#[cfg(feature = "model")]
@@ -242,6 +246,23 @@ enum_number! {
242246
ChatInput = 1,
243247
User = 2,
244248
Message = 3,
249+
PrimaryEntryPoint = 4,
250+
_ => Unknown(u8),
251+
}
252+
}
253+
254+
enum_number! {
255+
/// Signifies how the invocation of a command of type [`PrimaryEntryPoint`] should be handled.
256+
///
257+
/// [`PrimaryEntryPoint`]: CommandType::PrimaryEntryPoint
258+
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-entry-point-command-handler-types)
259+
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
260+
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
261+
#[serde(from = "u8", into = "u8")]
262+
#[non_exhaustive]
263+
pub enum EntryPointHandlerType {
264+
AppHandler = 1,
265+
DiscordLaunchActivity = 2,
245266
_ => Unknown(u8),
246267
}
247268
}

0 commit comments

Comments
 (0)