Struct MessageBuilder

Source
pub struct MessageBuilder(pub String);
Expand description

The Message Builder is an ergonomic utility to easily build a message, by adding text and mentioning mentionable structs.

The finalized value can be accessed via Self::build or the inner value.

§Examples

Build a message, mentioning a Self::user and an Self::emoji, and retrieving the value:

use serenity::utils::MessageBuilder;

// assuming an `emoji` and `user` have already been bound

let content = MessageBuilder::new()
    .push("You sent a message, ")
    .mention(&user)
    .push("! ")
    .emoji(&emoji)
    .build();

Tuple Fields§

§0: String

Implementations§

Source§

impl MessageBuilder

Source

pub fn new() -> MessageBuilder

Creates a new, empty builder.

§Examples

Create a new MessageBuilder:

use serenity::utils::MessageBuilder;

let message = MessageBuilder::new();

// alternatively:
let message = MessageBuilder::default();
Source

pub fn build(&mut self) -> String

Pulls the inner value out of the builder.

§Examples

Create a string mentioning a channel by Id, and then suffixing "!", and finally building it to retrieve the inner String:

use serenity::model::id::ChannelId;
use serenity::utils::MessageBuilder;

let channel_id = ChannelId::new(81384788765712384);

let content = MessageBuilder::new().channel(channel_id).push("!").build();

assert_eq!(content, "<#81384788765712384>!");

This is equivalent to simply retrieving the tuple struct’s first value:

use serenity::utils::MessageBuilder;

let mut content = MessageBuilder::new();
content.push("test");

assert_eq!(content.build(), "test");
Source

pub fn channel<C: Into<ChannelId>>(&mut self, channel: C) -> &mut Self

Mentions the GuildChannel in the built message.

This accepts anything that converts into a ChannelId. Refer to ChannelId’s documentation for more information.

Refer to ChannelId’s Display implementation for more information on how this is formatted.

§Examples

Mentioning a Channel by Id:

use serenity::model::id::ChannelId;
use serenity::utils::MessageBuilder;

let channel_id = ChannelId::new(81384788765712384);

let content = MessageBuilder::new().push("The channel is: ").channel(channel_id).build();

assert_eq!(content, "The channel is: <#81384788765712384>");
Source

pub fn emoji(&mut self, emoji: &Emoji) -> &mut Self

Displays the given emoji in the built message.

Refer to Emojis Display implementation for more information on how this is formatted.

§Examples

Mention an emoji in a message’s content:



let message = MessageBuilder::new().push("foo ").emoji(&emoji).push(".").build();

assert_eq!(message, "foo <:smugAnimeFace:302516740095606785>.");
Source

pub fn mention<M: Mentionable>(&mut self, item: &M) -> &mut Self

Mentions something that implements the Mentionable trait.

Source

pub fn push(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a string to the internal message content.

Note that this does not mutate either the given data or the internal message content in anyway prior to appending the given content to the internal message.

§Examples
use serenity::utils::MessageBuilder;

let mut message = MessageBuilder::new();
message.push("test");

assert_eq!(
    {
        message.push("ing");
        message.build()
    },
    "testing"
);
Source

pub fn push_codeblock( &mut self, content: impl Into<Content>, language: Option<&str>, ) -> &mut Self

Pushes a codeblock to the content, with optional syntax highlighting.

§Examples

Pushing a Rust codeblock:

use serenity::utils::MessageBuilder;

let code = r#"
fn main() {
    println!("Hello, world!");
}
"#;

let content = MessageBuilder::new()
    .push_codeblock(code, Some("rust"))
    .build();

let expected = r#"```rust
fn main() {
    println!("Hello, world!");
}
```"#;

assert_eq!(content, expected);

Pushing a codeblock without a language:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new()
    .push_codeblock("hello", None)
    .build();

assert_eq!(content, "```\nhello\n```");
Source

pub fn push_mono(&mut self, content: impl Into<Content>) -> &mut Self

Pushes inlined monospaced text to the content.

§Examples

Display a server configuration value to the user:

use serenity::utils::MessageBuilder;

let key = "prefix";
let value = "&";

let content = MessageBuilder::new()
    .push("The setting ")
    .push_mono(key)
    .push(" for this server is ")
    .push_mono(value)
    .push(".")
    .build();

let expected = format!("The setting `{}` for this server is `{}`.", key, value);

assert_eq!(content, expected);
Source

pub fn push_italic(&mut self, content: impl Into<Content>) -> &mut Self

Pushes inlined italicized text to the content.

§Examples

Emphasize information to the user:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new()
    .push("You don't ")
    .push_italic("always need")
    .push(" to italicize ")
    .push_italic("everything")
    .push(".")
    .build();

let expected = "You don't _always need_ to italicize _everything_.";

assert_eq!(content, expected);
Source

pub fn push_bold(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an inline bold text to the content.

Source

pub fn push_underline(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an underlined inline text to the content.

Source

pub fn push_strike(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a strikethrough inline text to the content.

Source

pub fn push_spoiler(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a spoiler’d inline text to the content.

Source

pub fn push_quote(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a quoted inline text to the content

Source

pub fn push_line(&mut self, content: impl Into<Content>) -> &mut Self

Pushes the given text with a newline appended to the content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new().push_line("hello").push("world").build();

assert_eq!(content, "hello\nworld");
Source

pub fn push_mono_line(&mut self, content: impl Into<Content>) -> &mut Self

Pushes inlined monospace text with an added newline to the content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new().push_mono_line("hello").push("world").build();

assert_eq!(content, "`hello`\nworld");
Source

pub fn push_italic_line(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an inlined italicized text with an added newline to the content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new().push_italic_line("hello").push("world").build();

assert_eq!(content, "_hello_\nworld");
Source

pub fn push_bold_line(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an inline bold text with an added newline to the content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new().push_bold_line("hello").push("world").build();

assert_eq!(content, "**hello**\nworld");
Source

pub fn push_underline_line(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an underlined inline text with an added newline to the content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new().push_underline_line("hello").push("world").build();

assert_eq!(content, "__hello__\nworld");
Source

pub fn push_strike_line(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a strikethrough inline text with a newline added to the content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new().push_strike_line("hello").push("world").build();

assert_eq!(content, "~~hello~~\nworld");
Source

pub fn push_spoiler_line(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a spoiler’d inline text with a newline added to the content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new().push_spoiler_line("hello").push("world").build();

assert_eq!(content, "||hello||\nworld");
Source

pub fn push_quote_line(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a quoted inline text to the content

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new().push_quote_line("hello").push("world").build();

assert_eq!(content, "> hello\nworld");
Source

pub fn push_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes text to your message, but normalizing content - that means ensuring that there’s no unwanted formatting, mention spam etc.

Source

pub fn push_codeblock_safe( &mut self, content: impl Into<Content>, language: Option<&str>, ) -> &mut Self

Pushes a code-block to your message normalizing content.

Source

pub fn push_mono_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an inline monospaced text to the content normalizing content.

Source

pub fn push_italic_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an inline italicized text to the content normalizing content.

Source

pub fn push_bold_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an inline bold text to the content normalizing content.

Source

pub fn push_underline_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an underlined inline text to the content normalizing content.

Source

pub fn push_strike_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a strikethrough inline text to the content normalizing content.

Source

pub fn push_spoiler_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a spoiler’d inline text to the content normalizing content.

Source

pub fn push_quote_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a quoted inline text to the content normalizing content.

Source

pub fn push_line_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes text with a newline appended to the content normalizing content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content =
    MessageBuilder::new().push_line_safe("Hello @everyone").push("How are you?").build();

assert_eq!(content, "Hello @\u{200B}everyone\nHow are you?");
Source

pub fn push_mono_line_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an inline monospaced text with added newline to the content normalizing content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content =
    MessageBuilder::new().push_mono_line_safe("`hello @everyone`").push("world").build();

assert_eq!(content, "`'hello @\u{200B}everyone'`\nworld");
Source

pub fn push_italic_line_safe( &mut self, content: impl Into<Content>, ) -> &mut Self

Pushes an inline italicized text with added newline to the content normalizing content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content =
    MessageBuilder::new().push_italic_line_safe("@everyone").push("Isn't a mention.").build();

assert_eq!(content, "_@\u{200B}everyone_\nIsn't a mention.");
Source

pub fn push_bold_line_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes an inline bold text with added newline to the content normalizing content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content =
    MessageBuilder::new().push_bold_line_safe("@everyone").push("Isn't a mention.").build();

assert_eq!(content, "**@\u{200B}everyone**\nIsn't a mention.");
Source

pub fn push_underline_line_safe( &mut self, content: impl Into<Content>, ) -> &mut Self

Pushes an underlined inline text with added newline to the content normalizing content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content = MessageBuilder::new()
    .push_underline_line_safe("@everyone")
    .push("Isn't a mention.")
    .build();

assert_eq!(content, "__@\u{200B}everyone__\nIsn't a mention.");
Source

pub fn push_strike_line_safe( &mut self, content: impl Into<Content>, ) -> &mut Self

Pushes a strikethrough inline text with added newline to the content normalizing content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content =
    MessageBuilder::new().push_strike_line_safe("@everyone").push("Isn't a mention.").build();

assert_eq!(content, "~~@\u{200B}everyone~~\nIsn't a mention.");
Source

pub fn push_spoiler_line_safe( &mut self, content: impl Into<Content>, ) -> &mut Self

Pushes a spoiler’d inline text with added newline to the content normalizing content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content =
    MessageBuilder::new().push_spoiler_line_safe("@everyone").push("Isn't a mention.").build();

assert_eq!(content, "||@\u{200B}everyone||\nIsn't a mention.");
Source

pub fn push_quote_line_safe(&mut self, content: impl Into<Content>) -> &mut Self

Pushes a quoted inline text with added newline to the content normalizing content.

§Examples

Push content and then append a newline:

use serenity::utils::MessageBuilder;

let content =
    MessageBuilder::new().push_quote_line_safe("@everyone").push("Isn't a mention.").build();

assert_eq!(content, "> @\u{200B}everyone\nIsn't a mention.");
Source

pub fn quote_rest(&mut self) -> &mut Self

Starts a multi-line quote, every push after this one will be quoted

Source

pub fn role<R: Into<RoleId>>(&mut self, role: R) -> &mut Self

Mentions the Role in the built message.

This accepts anything that converts into a RoleId. Refer to RoleId’s documentation for more information.

Refer to RoleId’s Display implementation for more information on how this is formatted.

Source

pub fn user<U: Into<UserId>>(&mut self, user: U) -> &mut Self

Mentions the User in the built message.

This accepts anything that converts into a UserId. Refer to UserId’s documentation for more information.

Refer to UserId’s Display implementation for more information on how this is formatted.

Trait Implementations§

Source§

impl Clone for MessageBuilder

Source§

fn clone(&self) -> MessageBuilder

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MessageBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MessageBuilder

Source§

fn default() -> MessageBuilder

Returns the “default value” for a type. Read more
Source§

impl Display for MessageBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the message builder into a string.

This is done by simply taking the internal value of the tuple-struct and writing it into the formatter.

§Examples

Create a message builder, and format it into a string via the format! macro:

use serenity::utils::MessageBuilder;
Source§

impl EmbedMessageBuilding for MessageBuilder

Pushes a named link to a message, intended for use in embeds. Read more
Pushes a named link intended for use in an embed, but with a normalized name to avoid escaping issues. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneDebuggableStorage for T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> CloneableStorage for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DebuggableStorage for T
where T: Any + Send + Sync + Debug,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T