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

Creates a new, empty builder.

Examples

Create a new MessageBuilder:

use serenity::utils::MessageBuilder;

let message = MessageBuilder::new();

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

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(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");

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(81384788765712384);

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

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

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:

use serenity::model::guild::Emoji;
use serenity::model::id::EmojiId;
use serenity::utils::MessageBuilder;


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

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

Mentions something that implements the Mentionable trait.

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"
);

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```");

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);

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);

Pushes an inline bold text to the content.

Pushes an underlined inline text to the content.

Pushes a strikethrough inline text to the content.

Pushes a spoiler’d inline text to the content.

Pushes a quoted inline text to the content

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");

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");

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");

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");

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");

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");

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");

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");

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

Pushes a code-block to your message normalizing content.

Pushes an inline monospaced text to the content normalizing content.

Pushes an inline italicized text to the content normalizing content.

Pushes an inline bold text to the content normalizing content.

Pushes an underlined inline text to the content normalizing content.

Pushes a strikethrough inline text to the content normalizing content.

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

Pushes a quoted inline text to the content normalizing content.

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?");

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");

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.");

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.");

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.");

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.");

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.");

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.");

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

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.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

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;

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

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

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

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

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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

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