Skip to content

Commit

Permalink
Add quoting functionality to MessageBuilder (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
AregevDev authored and arqunis committed Jul 31, 2019
1 parent 60336e7 commit 720d9ad
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/utils/message_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,14 @@ impl MessageBuilder {
self
}

/// Pushes a quoted inline text to the content
pub fn push_quote<D: I>(mut self, content: D) -> Self {
self.0.push_str("> ");
self.0.push_str(&content.into().to_string());

self
}

/// Pushes the given text with a newline appended to the content.
///
/// # Examples
Expand Down Expand Up @@ -523,6 +531,26 @@ impl MessageBuilder {
self
}

/// Pushes a quoted inline text to the content
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new().push_quote_line("hello").push("world").build();
///
/// assert_eq!(content, "> hello\nworld");
/// ```
pub fn push_quote_line<D: I>(mut self, content: D) -> Self {
self = self.push_quote(content);
self.0.push('\n');

self
}

/// Pushes text to your message, but normalizing content - that means
/// ensuring that there's no unwanted formatting, mention spam etc.
pub fn push_safe<C: I>(&mut self, content: C) -> &mut Self {
Expand Down Expand Up @@ -636,6 +664,18 @@ impl MessageBuilder {
self
}

/// Pushes a quoted inline text to the content normalizing content.
pub fn push_quote_safe<D: I>(mut self, content: D) -> Self {
self.0.push_str("> ");
{
let mut c = content.into();
c.inner = normalize(&c.inner).replace("> ", " ");
self.0.push_str(&c.to_string());
}

self
}

/// Pushes text with a newline appended to the content normalizing content.
///
/// # Examples
Expand Down Expand Up @@ -792,6 +832,36 @@ impl MessageBuilder {
self
}

/// Pushes a quoted inline text with added newline to the content normalizing
/// content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// 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.");
/// ```
pub fn push_quote_line_safe<D: I>(mut self, content: D) -> Self {
self = self.push_quote_safe(content);
self.0.push('\n');

self
}

/// Starts a multi-line quote, every push after this one will be quoted
pub fn quote_rest(mut self) -> Self {
self.0.push_str("\n>>> ");

self
}

/// Mentions the [`Role`] in the built message.
///
/// This accepts anything that converts _into_ a [`RoleId`]. Refer to
Expand Down

0 comments on commit 720d9ad

Please sign in to comment.