Skip to content

Commit

Permalink
Add support to set a single child builder for several parent builders (
Browse files Browse the repository at this point in the history
…serenity-rs#1737)

Additionally, rename/add some methods in order to be consistent with other
builders.
  • Loading branch information
natto1784 authored Feb 22, 2022
1 parent e5073ae commit 91ee596
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/builder/create_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ impl CreateComponents {
self
}

/// Set a single action row.
/// Calling this will overwrite all action rows.
pub fn set_action_row(&mut self, mut row: CreateActionRow) -> &mut Self {
self.0 = vec![row.build()];

self
}

/// Sets all the action rows.
pub fn set_action_rows(&mut self, rows: Vec<CreateActionRow>) -> &mut Self {
let new_rows = rows.into_iter().map(|mut f| f.build()).collect::<Vec<Value>>();
Expand Down
25 changes: 23 additions & 2 deletions src/builder/create_interaction_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl CreateInteractionResponseData {
}

/// Create an embed for the message.
pub fn create_embed<F>(&mut self, f: F) -> &mut Self
pub fn embed<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed,
{
Expand All @@ -99,11 +99,32 @@ impl CreateInteractionResponseData {
self
}

/// Adds multiple embeds for the message.
pub fn add_embeds(&mut self, embeds: Vec<CreateEmbed>) -> &mut Self {
for embed in embeds {
self.add_embed(embed);
}

self
}

/// Sets a single embed to include in the message
///
/// Calling this will overwrite the embed list.
/// To append embeds, call [`Self::add_embed`] instead.
pub fn set_embed(&mut self, embed: CreateEmbed) -> &mut Self {
let map = utils::hashmap_to_json_map(embed.0);
let embed = Value::Object(map);
self.0.insert("embeds", Value::Array(vec![embed]));

self
}

/// Sets a list of embeds to include in the message.
///
/// Calling this multiple times will overwrite the embed list.
/// To append embeds, call [`Self::add_embed`] instead.
pub fn embeds(&mut self, embeds: impl IntoIterator<Item = CreateEmbed>) -> &mut Self {
pub fn set_embeds(&mut self, embeds: impl IntoIterator<Item = CreateEmbed>) -> &mut Self {
let embeds =
embeds.into_iter().map(|embed| utils::hashmap_to_json_map(embed.0).into()).collect();

Expand Down
25 changes: 23 additions & 2 deletions src/builder/create_interaction_response_followup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<'a> CreateInteractionResponseFollowup<'a> {
}

/// Create an embed for the message.
pub fn create_embed<F>(&mut self, f: F) -> &mut Self
pub fn embed<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed,
{
Expand All @@ -110,11 +110,32 @@ impl<'a> CreateInteractionResponseFollowup<'a> {
self
}

/// Adds multiple embeds to the message.
pub fn add_embeds(&mut self, embeds: Vec<CreateEmbed>) -> &mut Self {
for embed in embeds {
self.add_embed(embed);
}

self
}

/// Sets a single embed to include in the message
///
/// Calling this will overwrite the embed list.
/// To append embeds, call [`Self::add_embed`] instead.
pub fn set_embed(&mut self, embed: CreateEmbed) -> &mut Self {
let map = utils::hashmap_to_json_map(embed.0);
let embed = Value::Object(map);
self.0.insert("embeds", Value::Array(vec![embed]));

self
}

/// Sets a list of embeds to include in the message.
///
/// Calling this multiple times will overwrite the embed list.
/// To append embeds, call [`Self::add_embed`] instead.
pub fn embeds(&mut self, embeds: impl IntoIterator<Item = CreateEmbed>) -> &mut Self {
pub fn set_embeds(&mut self, embeds: impl IntoIterator<Item = CreateEmbed>) -> &mut Self {
let embeds =
embeds.into_iter().map(|embed| utils::hashmap_to_json_map(embed.0).into()).collect();

Expand Down
23 changes: 22 additions & 1 deletion src/builder/edit_interaction_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl EditInteractionResponse {
}

/// Creates an embed for the message.
pub fn create_embed<F>(&mut self, f: F) -> &mut Self
pub fn embed<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut CreateEmbed) -> &mut CreateEmbed,
{
Expand All @@ -49,6 +49,27 @@ impl EditInteractionResponse {
self
}

/// Adds multiple embeds to the message.
pub fn add_embeds(&mut self, embeds: Vec<CreateEmbed>) -> &mut Self {
for embed in embeds {
self.add_embed(embed);
}

self
}

/// Sets a single embed to include in the message
///
/// Calling this will overwrite the embed list.
/// To append embeds, call [`Self::add_embed`] instead.
pub fn set_embed(&mut self, embed: CreateEmbed) -> &mut Self {
let map = utils::hashmap_to_json_map(embed.0);
let embed = Value::Object(map);
self.0.insert("embeds", Value::Array(vec![embed]));

self
}

/// Sets the embeds for the message.
///
/// **Note**: You can only have up to 10 embeds per message.
Expand Down

0 comments on commit 91ee596

Please sign in to comment.