Skip to content

Commit

Permalink
Add support for Entry Point commands (serenity-rs#3021)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mkrasnitski authored Nov 11, 2024
1 parent 17172da commit 85df0b0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/builder/create_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ pub struct CreateCommand {
#[serde(skip_serializing_if = "Option::is_none")]
contexts: Option<Vec<InteractionContext>>,
nsfw: bool,
#[serde(skip_serializing_if = "Option::is_none")]
handler: Option<EntryPointHandlerType>,
}

impl CreateCommand {
Expand All @@ -338,6 +340,7 @@ impl CreateCommand {

options: Vec::new(),
nsfw: false,
handler: None,
}
}

Expand Down Expand Up @@ -455,6 +458,15 @@ impl CreateCommand {
self.nsfw = nsfw;
self
}

/// Sets the command's entry point handler type. Only valid for commands of type
/// [`PrimaryEntryPoint`].
///
/// [`PrimaryEntryPoint`]: CommandType::PrimaryEntryPoint
pub fn handler(mut self, handler: EntryPointHandlerType) -> Self {
self.handler = Some(handler);
self
}
}

#[cfg(feature = "http")]
Expand Down
9 changes: 9 additions & 0 deletions src/builder/create_interaction_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ pub enum CreateInteractionResponse {
/// Corresponds to Discord's `PREMIUM_REQUIRED'.
#[deprecated = "use premium button components via `CreateButton::new_premium` instead"]
PremiumRequired,
/// Not valid for autocomplete and Ping interactions. Only available for applications with
/// Activities enabled.
///
/// Responds to the interaction by launching the Activity associated with the app.
///
/// Corresponds to Discord's `LAUNCH_ACTIVITY`.
LaunchActivity,
}

impl serde::Serialize for CreateInteractionResponse {
Expand All @@ -83,6 +90,7 @@ impl serde::Serialize for CreateInteractionResponse {
Self::Autocomplete(_) => 8,
Self::Modal(_) => 9,
Self::PremiumRequired => 10,
Self::LaunchActivity => 12,
})?;

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

map.end()
Expand Down
21 changes: 21 additions & 0 deletions src/model/application/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ pub struct Command {
pub contexts: Option<Vec<InteractionContext>>,
/// An autoincremented version identifier updated during substantial record changes.
pub version: CommandVersionId,
/// Only present for commands of type [`PrimaryEntryPoint`].
///
/// [`PrimaryEntryPoint`]: CommandType::PrimaryEntryPoint
pub handler: Option<EntryPointHandlerType>,
}

#[cfg(feature = "model")]
Expand Down Expand Up @@ -242,6 +246,23 @@ enum_number! {
ChatInput = 1,
User = 2,
Message = 3,
PrimaryEntryPoint = 4,
_ => Unknown(u8),
}
}

enum_number! {
/// Signifies how the invocation of a command of type [`PrimaryEntryPoint`] should be handled.
///
/// [`PrimaryEntryPoint`]: CommandType::PrimaryEntryPoint
/// [Discord docs](https://discord.com/developers/docs/interactions/application-commands#application-command-object-entry-point-command-handler-types)
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum EntryPointHandlerType {
AppHandler = 1,
DiscordLaunchActivity = 2,
_ => Unknown(u8),
}
}
Expand Down

0 comments on commit 85df0b0

Please sign in to comment.