Skip to content

Commit

Permalink
Fix usage of builder pattern across code, docs, and examples (#1764)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski authored and arqunis committed Mar 15, 2022
1 parent c1ef6d9 commit 36e0909
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 143 deletions.
33 changes: 16 additions & 17 deletions examples/e17_message_components/src/main.rs
Expand Up @@ -91,10 +91,10 @@ impl Animal {
menu.custom_id("animal_select");
menu.placeholder("No animal selected");
menu.options(|f| {
f.add_option(Self::Cat.menu_option());
f.add_option(Self::Dog.menu_option());
f.add_option(Self::Horse.menu_option());
f.add_option(Self::Alpaca.menu_option())
f.add_option(Self::Cat.menu_option())
.add_option(Self::Dog.menu_option())
.add_option(Self::Horse.menu_option())
.add_option(Self::Alpaca.menu_option())
});
menu
}
Expand Down Expand Up @@ -183,9 +183,8 @@ impl EventHandler for Handler {
let m = msg
.channel_id
.send_message(&ctx, |m| {
m.content("Please select your favorite animal");
m.components(|c| c.add_action_row(Animal::action_row()));
m
m.content("Please select your favorite animal")
.components(|c| c.add_action_row(Animal::action_row()))
})
.await
.unwrap();
Expand All @@ -207,10 +206,9 @@ impl EventHandler for Handler {

// Acknowledge the interaction and edit the message
mci.create_interaction_response(&ctx, |r| {
r.kind(InteractionResponseType::UpdateMessage);
r.interaction_response_data(|d| {
d.content(format!("You chose: **{}**\nNow choose a sound!", animal));
d.components(|c| c.add_action_row(Sound::action_row()))
r.kind(InteractionResponseType::UpdateMessage).interaction_response_data(|d| {
d.content(format!("You chose: **{}**\nNow choose a sound!", animal))
.components(|c| c.add_action_row(Sound::action_row()))
})
})
.await
Expand All @@ -226,12 +224,13 @@ impl EventHandler for Handler {
// Acknowledge the interaction and send a reply
mci.create_interaction_response(&ctx, |r| {
// This time we dont edit the message but reply to it
r.kind(InteractionResponseType::ChannelMessageWithSource);
r.interaction_response_data(|d| {
// Make the message hidden for other users
d.flags(InteractionApplicationCommandCallbackDataFlags::EPHEMERAL);
d.content(format!("The **{}** says __{}__", animal, sound))
})
r.kind(InteractionResponseType::ChannelMessageWithSource).interaction_response_data(
|d| {
// Make the message hidden for other users
d.flags(InteractionApplicationCommandCallbackDataFlags::EPHEMERAL)
.content(format!("The **{}** says __{}__", animal, sound))
},
)
})
.await
.unwrap();
Expand Down
27 changes: 6 additions & 21 deletions src/builder/create_allowed_mentions.rs
Expand Up @@ -28,39 +28,24 @@ pub enum ParseValue {
/// use serenity::builder::ParseValue;
///
/// // Mention only the user 110372470472613888
/// m.allowed_mentions(|am| {
/// am.empty_parse();
/// am.users(vec![110372470472613888])
/// });
/// m.allowed_mentions(|am| am.empty_parse().users(vec![110372470472613888]));
///
/// // Mention all users and the role 182894738100322304
/// m.allowed_mentions(|am| {
/// am.parse(ParseValue::Users);
/// am.roles(vec![182894738100322304])
/// });
/// m.allowed_mentions(|am| am.parse(ParseValue::Users).roles(vec![182894738100322304]));
///
/// // Mention all roles and nothing else
/// m.allowed_mentions(|am| {
/// am.parse(ParseValue::Roles)
/// });
/// m.allowed_mentions(|am| am.parse(ParseValue::Roles));
///
/// // Mention all roles and users, but not everyone
/// m.allowed_mentions(|am| {
/// am.parse(ParseValue::Users);
/// am.parse(ParseValue::Roles)
/// });
/// m.allowed_mentions(|am| am.parse(ParseValue::Users).parse(ParseValue::Roles));
///
/// // Mention everyone and the users 182891574139682816, 110372470472613888
/// m.allowed_mentions(|am| {
/// am.parse(ParseValue::Everyone);
/// am.users(vec![182891574139682816, 110372470472613888])
/// am.parse(ParseValue::Everyone).users(vec![182891574139682816, 110372470472613888])
/// });
///
/// // Mention everyone and the message author.
/// m.allowed_mentions(|am| {
/// am.parse(ParseValue::Everyone);
/// am.users(vec![msg.author.id])
/// });
/// m.allowed_mentions(|am| am.parse(ParseValue::Everyone).users(vec![msg.author.id]));
/// ```
///
/// [`ChannelId::send_message`]: crate::model::id::ChannelId::send_message
Expand Down
16 changes: 4 additions & 12 deletions src/builder/create_embed.rs
Expand Up @@ -230,9 +230,7 @@ impl CreateEmbed {
/// let _ = msg
/// .channel_id
/// .send_message(&context.http, |m| {
/// m.embed(|e| e.title("hello").timestamp("2004-06-08T16:04:23"));
///
/// m
/// m.embed(|e| e.title("hello").timestamp("2004-06-08T16:04:23"))
/// })
/// .await;
/// }
Expand Down Expand Up @@ -274,14 +272,11 @@ impl CreateEmbed {
/// let _ = channel
/// .send_message(&context, |m| {
/// m.embed(|e| {
/// e.author(|a| a.icon_url(&user.face()).name(&user.name));
/// e.title("Member Join");
///
/// if let Some(ref joined_at) = member.joined_at {
/// e.timestamp(joined_at);
/// }
///
/// e
/// e.author(|a| a.icon_url(&user.face()).name(&user.name))
/// .title("Member Join")
/// })
/// })
/// .await;
Expand Down Expand Up @@ -403,13 +398,10 @@ impl From<Embed> for CreateEmbed {

if let Some(footer) = embed.footer {
b.footer(move |f| {
f.text(&footer.text);

if let Some(icon_url) = footer.icon_url {
f.icon_url(&icon_url);
}

f
f.text(&footer.text)
});
}

Expand Down
20 changes: 4 additions & 16 deletions src/builder/create_invite.rs
Expand Up @@ -92,10 +92,7 @@ impl CreateInvite {
/// # async fn example(context: &Context) -> CommandResult {
/// # let channel = context.cache.guild_channel(81384788765712384).unwrap();
/// #
/// let invite = channel.create_invite(context, |i| {
/// i.max_age(3600)
/// })
/// .await?;
/// let invite = channel.create_invite(context, |i| i.max_age(3600)).await?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -126,10 +123,7 @@ impl CreateInvite {
/// # async fn example(context: &Context) -> CommandResult {
/// # let channel = context.cache.guild_channel(81384788765712384).unwrap();
/// #
/// let invite = channel.create_invite(context, |i| {
/// i.max_uses(5)
/// })
/// .await?;
/// let invite = channel.create_invite(context, |i| i.max_uses(5)).await?;
/// # Ok(())
/// # }
/// ```
Expand Down Expand Up @@ -158,10 +152,7 @@ impl CreateInvite {
/// # async fn example(context: &Context) -> CommandResult {
/// # let channel = context.cache.guild_channel(81384788765712384).unwrap();
/// #
/// let invite = channel.create_invite(context, |i| {
/// i.temporary(true)
/// })
/// .await?;
/// let invite = channel.create_invite(context, |i| i.temporary(true)).await?;
/// # Ok(())
/// # }
/// #
Expand Down Expand Up @@ -192,10 +183,7 @@ impl CreateInvite {
/// # async fn example(context: &Context) -> CommandResult {
/// # let channel = context.cache.guild_channel(81384788765712384).unwrap();
/// #
/// let invite = channel.create_invite(context, |i| {
/// i.unique(true)
/// })
/// .await?;
/// let invite = channel.create_invite(context, |i| i.unique(true)).await?;
/// # Ok(())
/// # }
/// ```
Expand Down
14 changes: 3 additions & 11 deletions src/builder/create_message.rs
Expand Up @@ -38,17 +38,9 @@ use crate::model::id::StickerId;
/// let channel_id = ChannelId(7);
///
/// let _ = channel_id.send_message(&http, |m| {
/// m.content("test");
/// m.tts(true);
///
/// m.embed(|mut e| {
/// e.title("This is an embed");
/// e.description("With a description");
///
/// e
/// });
///
/// m
/// m.content("test")
/// .tts(true)
/// .embed(|e| e.title("This is an embed").description("With a description"))
/// });
/// ```
///
Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_guild.rs
Expand Up @@ -64,7 +64,7 @@ impl EditGuild {
///
/// let base64_icon = utils::read_image("./guild_icon.png")?;
///
/// guild.edit(&http, |mut g| g.icon(Some(&base64_icon))).await?;
/// guild.edit(&http, |g| g.icon(Some(&base64_icon))).await?;
/// # Ok(())
/// # }
/// ```
Expand Down
16 changes: 6 additions & 10 deletions src/framework/standard/configuration.rs
Expand Up @@ -333,16 +333,12 @@ impl Configuration {
/// use serenity::framework::StandardFramework;
///
/// let framework = StandardFramework::new().configure(|c| {
/// c
/// .dynamic_prefix(|_, msg| Box::pin(async move {
/// Some(if msg.channel_id.0 % 5 == 0 {
/// "!"
/// } else {
/// "*"
/// }.to_string())
/// }))
/// // This disables the default prefix "~"
/// .prefix("")
/// c.dynamic_prefix(|_, msg| {
/// Box::pin(
/// async move { Some(if msg.channel_id.0 % 5 == 0 { "!" } else { "*" }.to_string()) },
/// )
/// })
/// .prefix("") // This disables the default prefix "~"
/// });
/// ```
///
Expand Down
22 changes: 2 additions & 20 deletions src/framework/standard/help_commands.rs
Expand Up @@ -1145,16 +1145,7 @@ async fn send_suggestion_embed(
) -> Result<Message, Error> {
let text = help_description.replace("{}", &suggestions.join("`, `"));

channel_id
.send_message(&http, |m| {
m.embed(|e| {
e.colour(colour);
e.description(text);
e
});
m
})
.await
channel_id.send_message(&http, |m| m.embed(|e| e.colour(colour).description(text))).await
}

/// Sends an embed explaining fetching commands failed.
Expand All @@ -1165,16 +1156,7 @@ async fn send_error_embed(
input: &str,
colour: Colour,
) -> Result<Message, Error> {
channel_id
.send_message(&http, |m| {
m.embed(|e| {
e.colour(colour);
e.description(input);
e
});
m
})
.await
channel_id.send_message(&http, |m| m.embed(|e| e.colour(colour).description(input))).await
}

/// Posts an embed showing each individual command group and its commands.
Expand Down
5 changes: 2 additions & 3 deletions src/model/channel/guild_channel.rs
Expand Up @@ -762,9 +762,8 @@ impl GuildChannel {
///
/// let _ = msg
/// .channel_id
/// .send_files(&context.http, vec![(&file, "cat.png")], |mut m| {
/// m.content("here's a cat");
/// m
/// .send_files(&context.http, vec![(&file, "cat.png")], |m| {
/// m.content("here's a cat")
/// })
/// .await;
/// }
Expand Down
8 changes: 2 additions & 6 deletions src/model/guild/member.rs
Expand Up @@ -265,8 +265,7 @@ impl Member {
match self
.guild_id
.edit_member(http, self.user.id, |member| {
member.disable_communication_until_datetime(time);
member
member.disable_communication_until_datetime(time)
})
.await
{
Expand Down Expand Up @@ -325,10 +324,7 @@ impl Member {
pub async fn enable_communication(&mut self, http: impl AsRef<Http>) -> Result<()> {
match self
.guild_id
.edit_member(&http, self.user.id, |member| {
member.enable_communication();
member
})
.edit_member(&http, self.user.id, |member| member.enable_communication())
.await
{
Ok(_) => {
Expand Down
9 changes: 2 additions & 7 deletions src/model/guild/role.rs
Expand Up @@ -154,14 +154,9 @@ impl Role {
/// # use serenity::model::id::RoleId;
/// # let role = RoleId(7).to_role_cached(&cache).unwrap();
/// // assuming a `role` has already been bound
//
/// role.edit(|mut r| {
/// r.hoist(true);
///
/// r
/// });
/// role.edit(|r| r.hoist(true));
/// ```
///
///
/// # Errors
///
/// Returns [`Error::Http`] if the current user does not
Expand Down
27 changes: 8 additions & 19 deletions src/model/webhook.rs
Expand Up @@ -269,12 +269,7 @@ impl Webhook {
/// let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
/// let mut webhook = http.get_webhook_from_url(url).await?;
///
/// webhook
/// .execute(&http, false, |mut w| {
/// w.content("test");
/// w
/// })
/// .await?;
/// webhook.execute(&http, false, |w| w.content("test")).await?;
/// # Ok(())
/// # }
/// ```
Expand All @@ -292,24 +287,18 @@ impl Webhook {
/// let url = "https://discord.com/api/webhooks/245037420704169985/ig5AO-wdVWpCBtUUMxmgsWryqgsW3DChbKYOINftJ4DCrUbnkedoYZD0VOH1QLr-S3sV";
/// let mut webhook = http.get_webhook_from_url(url).await?;
///
/// let embed = Embed::fake(|mut e| {
/// e.title("Rust's website");
/// e.description(
/// "Rust is a systems programming language that runs
/// let embed = Embed::fake(|e| {
/// e.title("Rust's website")
/// .description(
/// "Rust is a systems programming language that runs
/// blazingly fast, prevents segfaults, and guarantees
/// thread safety.",
/// );
/// e.url("https://rust-lang.org");
/// e
/// )
/// .url("https://rust-lang.org")
/// });
///
/// webhook
/// .execute(&http, false, |mut w| {
/// w.content("test");
/// w.username("serenity");
/// w.embeds(vec![embed]);
/// w
/// })
/// .execute(&http, false, |w| w.content("test").username("serenity").embeds(vec![embed]))
/// .await?;
/// # Ok(())
/// # }
Expand Down

0 comments on commit 36e0909

Please sign in to comment.