use model::{
guild::Emoji,
id::{ChannelId, RoleId, UserId},
misc::Mentionable
};
use std::{
default::Default,
fmt::{self, Display, Write},
ops::Add
};
#[derive(Clone, Debug, Default)]
pub struct MessageBuilder(pub String);
impl MessageBuilder {
pub fn new() -> MessageBuilder { MessageBuilder::default() }
pub fn build(self) -> String { self.0 }
#[inline]
pub fn channel<C: Into<ChannelId>>(self, channel: C) -> Self {
self._channel(channel.into())
}
fn _channel(mut self, channel: ChannelId) -> Self {
let _ = write!(self.0, "{}", channel.mention());
self
}
pub fn emoji(mut self, emoji: &Emoji) -> Self {
let _ = write!(self.0, "{}", emoji);
self
}
pub fn mention<M: Mentionable>(mut self, item: &M) -> Self {
let _ = write!(self.0, "{}", item.mention());
self
}
#[inline]
pub fn push<D: I>(self, content: D) -> Self {
self._push(&content.into().to_string())
}
fn _push(mut self, content: &str) -> Self {
self.0.push_str(content);
self
}
pub fn push_codeblock<D: I>(mut self, content: D, language: Option<&str>) -> Self {
self.0.push_str("```");
if let Some(language) = language {
self.0.push_str(language);
}
self.0.push('\n');
self.0.push_str(&content.into().to_string());
self.0.push_str("\n```");
self
}
pub fn push_mono<D: I>(mut self, content: D) -> Self {
self.0.push('`');
self.0.push_str(&content.into().to_string());
self.0.push('`');
self
}
pub fn push_italic<D: I>(mut self, content: D) -> Self {
self.0.push('_');
self.0.push_str(&content.into().to_string());
self.0.push('_');
self
}
pub fn push_bold<D: I>(mut self, content: D) -> Self {
self.0.push_str("**");
self.0.push_str(&content.into().to_string());
self.0.push_str("**");
self
}
pub fn push_underline<D: I>(mut self, content: D) -> Self {
self.0.push_str("__");
self.0.push_str(&content.into().to_string());
self.0.push_str("__");
self
}
pub fn push_strike<D: I>(mut self, content: D) -> Self {
self.0.push_str("~~");
self.0.push_str(&content.into().to_string());
self.0.push_str("~~");
self
}
pub fn push_line<D: I>(mut self, content: D) -> Self {
self = self.push(content);
self.0.push('\n');
self
}
pub fn push_mono_line<D: I>(mut self, content: D) -> Self {
self = self.push_mono(content);
self.0.push('\n');
self
}
pub fn push_italic_line<D: I>(mut self, content: D) -> Self {
self = self.push_italic(content);
self.0.push('\n');
self
}
pub fn push_bold_line<D: I>(mut self, content: D) -> Self {
self = self.push_bold(content);
self.0.push('\n');
self
}
pub fn push_underline_line<D: I>(mut self, content: D) -> Self {
self = self.push_underline(content);
self.0.push('\n');
self
}
pub fn push_strike_line<D: I>(mut self, content: D) -> Self {
self = self.push_strike(content);
self.0.push('\n');
self
}
pub fn push_safe<C: I>(mut self, content: C) -> Self {
{
let mut c = content.into();
c.inner = normalize(&c.inner)
.replace('*', "\\*")
.replace('`', "\\`")
.replace('_', "\\_");
self.0.push_str(&c.to_string());
}
self
}
pub fn push_codeblock_safe<D: I>(mut self, content: D, language: Option<&str>) -> Self {
self.0.push_str("```");
if let Some(language) = language {
self.0.push_str(language);
}
{
let mut c = content.into();
c.inner = normalize(&c.inner).replace("```", " ");
self.0.push_str(&c.to_string());
}
self.0.push_str("```");
self
}
pub fn push_mono_safe<D: I>(mut self, content: D) -> Self {
self.0.push('`');
{
let mut c = content.into();
c.inner = normalize(&c.inner).replace('`', "'");
self.0.push_str(&c.to_string());
}
self.0.push('`');
self
}
pub fn push_italic_safe<D: I>(mut self, content: D) -> Self {
self.0.push('_');
{
let mut c = content.into();
c.inner = normalize(&c.inner).replace('_', " ");
self.0.push_str(&c.to_string());
}
self.0.push('_');
self
}
pub fn push_bold_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.0.push_str("**");
self
}
pub fn push_underline_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.0.push_str("__");
self
}
pub fn push_strike_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.0.push_str("~~");
self
}
pub fn push_line_safe<D: I>(mut self, content: D) -> Self {
self = self.push_safe(content);
self.0.push('\n');
self
}
pub fn push_mono_line_safe<D: I>(mut self, content: D) -> Self {
self = self.push_mono_safe(content);
self.0.push('\n');
self
}
pub fn push_italic_line_safe<D: I>(mut self, content: D) -> Self {
self = self.push_italic_safe(content);
self.0.push('\n');
self
}
pub fn push_bold_line_safe<D: I>(mut self, content: D) -> Self {
self = self.push_bold_safe(content);
self.0.push('\n');
self
}
pub fn push_underline_line_safe<D: I>(mut self, content: D) -> Self {
self = self.push_underline_safe(content);
self.0.push('\n');
self
}
pub fn push_strike_line_safe<D: I>(mut self, content: D) -> Self {
self = self.push_strike_safe(content);
self.0.push('\n');
self
}
pub fn role<R: Into<RoleId>>(mut self, role: R) -> Self {
let _ = write!(self.0, "{}", role.into().mention());
self
}
pub fn user<U: Into<UserId>>(mut self, user: U) -> Self {
let _ = write!(self.0, "{}", user.into().mention());
self
}
}
impl Display for MessageBuilder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}
pub enum ContentModifier {
Italic,
Bold,
Strikethrough,
Code,
Underline,
}
#[derive(Debug, Default, Clone)]
pub struct Content {
pub italic: bool,
pub bold: bool,
pub strikethrough: bool,
pub inner: String,
pub code: bool,
pub underline: bool,
}
impl<T: ToString> Add<T> for Content {
type Output = Content;
fn add(mut self, rhs: T) -> Content {
self.inner = self.inner + &rhs.to_string();
self
}
}
impl<T: ToString> Add<T> for ContentModifier {
type Output = Content;
fn add(self, rhs: T) -> Content {
let mut nc = self.to_content();
nc.inner = nc.inner + &rhs.to_string();
nc
}
}
impl Add<ContentModifier> for Content {
type Output = Content;
fn add(mut self, rhs: ContentModifier) -> Content {
self.apply(&rhs);
self
}
}
impl Add<ContentModifier> for ContentModifier {
type Output = Content;
fn add(self, rhs: ContentModifier) -> Content {
let mut nc = self.to_content();
nc.apply(&rhs);
nc
}
}
impl ContentModifier {
fn to_content(&self) -> Content {
let mut nc = Content::default();
nc.apply(self);
nc
}
}
impl Content {
pub fn apply(&mut self, modifier: &ContentModifier) {
match *modifier {
ContentModifier::Italic => {
self.italic = true;
},
ContentModifier::Bold => {
self.bold = true;
},
ContentModifier::Strikethrough => {
self.strikethrough = true;
},
ContentModifier::Code => {
self.code = true;
},
ContentModifier::Underline => {
self.underline = true;
},
}
}
pub fn to_string(&self) -> String {
let capacity =
self.inner.len() + if self.bold { 4 } else { 0 } + if self.italic { 2 } else { 0 } +
if self.strikethrough { 4 } else { 0 } + if self.underline {
4
} else {
0
} + if self.code { 2 } else { 0 };
let mut new_str = String::with_capacity(capacity);
if self.bold {
new_str.push_str("**");
}
if self.italic {
new_str.push('*');
}
if self.strikethrough {
new_str.push_str("~~");
}
if self.underline {
new_str.push_str("__");
}
if self.code {
new_str.push('`');
}
new_str.push_str(&self.inner);
if self.code {
new_str.push('`');
}
if self.underline {
new_str.push_str("__");
}
if self.strikethrough {
new_str.push_str("~~");
}
if self.italic {
new_str.push('*');
}
if self.bold {
new_str.push_str("**");
}
new_str
}
}
impl From<ContentModifier> for Content {
fn from(cm: ContentModifier) -> Content { cm.to_content() }
}
mod private {
use super::{Content, ContentModifier};
use std::fmt;
pub trait A {}
impl A for ContentModifier {}
impl A for Content {}
impl<T: fmt::Display> A for T {}
}
pub trait I: self::private::A {
fn into(self) -> Content;
}
impl<T: fmt::Display> I for T {
fn into(self) -> Content {
Content {
italic: false,
bold: false,
strikethrough: false,
inner: self.to_string(),
code: false,
underline: false,
}
}
}
impl I for ContentModifier {
fn into(self) -> Content { self.to_content() }
}
impl I for Content {
fn into(self) -> Content { self }
}
fn normalize(text: &str) -> String {
text.replace("discord.gg", "discord\u{2024}gg")
.replace("discord.me", "discord\u{2024}me")
.replace("discordlist.net", "discordlist\u{2024}net")
.replace("discordservers.com", "discordservers\u{2024}com")
.replace("discordapp.com/invite", "discordapp\u{2024}com/invite")
.replace('\u{202E}', " ") .replace('\u{200F}', " ") .replace('\u{202B}', " ") .replace('\u{200B}', " ") .replace('\u{200D}', " ") .replace('\u{200C}', " ") .replace("@everyone", "@\u{200B}everyone")
.replace("@here", "@\u{200B}here")
}
#[cfg(test)]
mod test {
use model::prelude::*;
use super::{
ContentModifier::*,
MessageBuilder,
};
#[test]
fn code_blocks() {
let content = MessageBuilder::new()
.push_codeblock("test", Some("rb"))
.build();
assert_eq!(content, "```rb\ntest\n```");
}
#[test]
fn safe_content() {
let content = MessageBuilder::new()
.push_safe("@everyone discord.gg/discord-api")
.build();
assert_ne!(content, "@everyone discord.gg/discord-api");
}
#[test]
fn no_free_formatting() {
let content = MessageBuilder::new().push_bold_safe("test**test").build();
assert_ne!(content, "**test**test**");
}
#[test]
fn mentions() {
let content_emoji = MessageBuilder::new()
.emoji(&Emoji {
animated: false,
id: EmojiId(32),
name: "Rohrkatze".to_string(),
managed: false,
require_colons: true,
roles: vec![],
})
.build();
let content_mentions = MessageBuilder::new()
.channel(1)
.mention(&UserId(2))
.role(3)
.user(4)
.build();
assert_eq!(content_mentions, "<#1><@2><@&3><@4>");
assert_eq!(content_emoji, "<:Rohrkatze:32>");
}
#[test]
fn content() {
let content = Bold + Italic + Code + "Fun!";
assert_eq!(content.to_string(), "***`Fun!`***");
}
#[test]
fn message_content() {
let message_content = MessageBuilder::new()
.push(Bold + Italic + Code + "Fun!")
.build();
assert_eq!(message_content, "***`Fun!`***");
}
#[test]
fn message_content_safe() {
let message_content = MessageBuilder::new()
.push_safe(Bold + Italic + "test**test")
.build();
assert_eq!(message_content, "***test\\*\\*test***");
}
}