Skip to content

Commit

Permalink
Apply changes to interaction response types and add a new interaction…
Browse files Browse the repository at this point in the history
… type (#1252)

This updates the interaction types to reflect the changes as laid in the slash commands UI changes pull request for Discord's API documentation: discord/discord-api-docs#2615. 

This removes the `Acknowledge` and `ChannelMessage` interaction response types, as they have been deprecated and scheduled for removal on the 9th of April, and renames `AcknowledgeWithSource` to `DeferredChannelMessageWithSource` to suit a better purpose. 

Furthermore, a new `MessageInteraction` type and its corresponding `interaction` field in `Message` is added.

Additionally, a fix is incorporated to the interaction responses builders to use the correct field name for embeds (`embed` -> `embeds`).
  • Loading branch information
arqunis authored Mar 8, 2021
1 parent 3aa6aae commit eba755c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/builder/create_interaction_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl CreateInteractionResponse {
impl<'a> Default for CreateInteractionResponse {
fn default() -> CreateInteractionResponse {
let mut map = HashMap::new();
map.insert("type", Value::Number(serde_json::Number::from(2)));
map.insert("type", Value::Number(serde_json::Number::from(4)));

CreateInteractionResponse(map)
}
Expand Down Expand Up @@ -89,7 +89,12 @@ impl CreateInteractionResponseData {
let map = utils::hashmap_to_json_map(embed.0);
let embed = Value::Object(map);

self.0.insert("embed", embed);
let mut embeds = self.0.entry("embeds").or_insert_with(|| Value::Array(vec![]));

if let Some(embeds) = embeds.as_array_mut() {
embeds.push(embed);
}

self
}

Expand Down
7 changes: 6 additions & 1 deletion src/builder/edit_interaction_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ impl EditInteractionResponse {
let map = utils::hashmap_to_json_map(embed.0);
let embed = Value::Object(map);

self.0.insert("embed", embed);
let mut embeds = self.0.entry("embeds").or_insert_with(|| Value::Array(vec![]));

if let Some(embeds) = embeds.as_array_mut() {
embeds.push(embed);
}

self
}

Expand Down
2 changes: 2 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ mod test {
flags: None,
stickers: vec![],
referenced_message: None,
#[cfg(feature = "unstable_discord_api")]
interaction: None,
},
};

Expand Down
8 changes: 8 additions & 0 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use crate::client::bridge::gateway::ShardMessenger;
use crate::collector::{CollectReaction, ReactionCollectorBuilder};
#[cfg(feature = "model")]
use crate::http::{CacheHttp, Http};
#[cfg(feature = "unstable_discord_api")]
use crate::model::interactions::MessageInteraction;
use crate::model::prelude::*;
#[cfg(feature = "model")]
use crate::model::utils::U64Visitor;
Expand Down Expand Up @@ -104,6 +106,12 @@ pub struct Message {
pub stickers: Vec<Sticker>,
/// The message that was replied to using this message.
pub referenced_message: Option<Box<Message>>, // Boxed to avoid recusion
/// Sent if the message is a response to an [`Interaction`].
///
/// [`Interaction`]: crate::model::interactions::Interaction
#[cfg(feature = "unstable_discord_api")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable_discord_api")))]
pub interaction: Option<MessageInteraction>,
}

#[cfg(feature = "model")]
Expand Down
16 changes: 13 additions & 3 deletions src/model/interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,8 @@ pub struct ApplicationCommandOptionChoice {
#[repr(u8)]
pub enum InteractionResponseType {
Pong = 1,
Acknowledge = 2,
ChannelMessage = 3,
ChannelMessageWithSource = 4,
AcknowledgeWithSource = 5,
DeferredChannelMessageWithSource = 5,
}

#[derive(Clone, Serialize)]
Expand All @@ -231,6 +229,18 @@ __impl_bitflags! {
}
}

/// Sent when a [`Message`] is a response to an [`Interaction`].
///
/// [`Message`]: crate::model::channel::Message
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MessageInteraction {
pub id: InteractionId,
#[serde(rename = "type")]
pub kind: InteractionType,
pub name: String,
pub user: User,
}

impl Interaction {
/// Creates a global [`ApplicationCommand`],
/// overriding an existing one with the same name if it exists.
Expand Down
2 changes: 2 additions & 0 deletions src/utils/custom_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,7 @@ fn dummy_message() -> Message {
flags: None,
stickers: Vec::new(),
referenced_message: None,
#[cfg(feature = "unstable_discord_api")]
interaction: None,
}
}

0 comments on commit eba755c

Please sign in to comment.