Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply changes to interaction response types and add a new interaction type #1252

Merged
merged 3 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
}