1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
use std::default::Default; use std::fmt::{self, Display, Write}; use std::ops::Add; use model::{ChannelId, Emoji, Mentionable, RoleId, UserId}; /// 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 [`build`] or the inner value. /// /// # Examples /// /// Build a message, mentioning a [`user`] and an [`emoji`], and retrieving the /// value: /// /// ```rust,no_run /// # use serenity::model::{Emoji, EmojiId, UserId}; /// # /// # let user = UserId(1); /// # let emoji = Emoji { /// # id: EmojiId(2), /// # name: "test".to_owned(), /// # managed: false, /// # require_colons: true, /// # roles: vec![], /// # }; /// # /// 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("! ") /// .mention(emoji) /// .build(); /// ``` /// /// [`build`]: #method.build /// [`emoji`]: #method.emoji /// [`user`]: #method.user #[derive(Clone, Debug, Default)] pub struct MessageBuilder(pub String); impl MessageBuilder { /// Creates a new, empty builder. /// /// # Examples /// /// Create a new `MessageBuilder`: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let message = MessageBuilder::new(); /// /// // alternatively: /// let message = MessageBuilder::default(); /// ``` pub fn new() -> MessageBuilder { 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: /// /// ```rust /// use serenity::model::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: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let content = MessageBuilder::new().push("test").0; /// /// assert_eq!(content, "test"); /// ``` pub fn build(self) -> String { self.0 } /// 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: /// /// ```rust /// use serenity::model::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>"); /// ``` /// /// [`Channel`]: ../model/enum.Channel.html /// [`ChannelId`]: ../model/struct.ChannelId.html /// [`GuildChannel`]: ../model/struct.GuildChannel.html /// [Display implementation]: ../model/struct.ChannelId.html#method.fmt-1 pub fn channel<C: Into<ChannelId>>(mut self, channel: C) -> Self { let _ = write!(self.0, "{}", channel.into().mention()); self } /// Displays the given emoji in the built message. /// /// Refer to `Emoji`s [Display implementation] for more information on how /// this is formatted. /// /// # Examples /// /// Mention an emoji in a message's content: /// /// ```rust /// use serenity::model::{Emoji, EmojiId}; /// use serenity::utils::MessageBuilder; /// /// let emoji = Emoji { /// id: EmojiId(302516740095606785), /// managed: true, /// name: "smugAnimeFace".to_owned(), /// require_colons: true, /// roles: vec![], /// }; /// /// let message = MessageBuilder::new() /// .push("foo ") /// .emoji(emoji) /// .push(".") /// .build(); /// /// assert_eq!(message, "foo <:smugAnimeFace:302516740095606785>."); /// ``` /// /// [Display implementation]: ../model/struct.Emoji.html#method.fmt pub fn emoji(mut self, emoji: Emoji) -> Self { let _ = write!(self.0, "{}", emoji); self } /// Mentions something that implements the [`Mentionable`] trait. /// /// [`Mentionable`]: ../model/trait.Mentionable.html pub fn mention<M: Mentionable>(mut self, item: M) -> Self { let _ = write!(self.0, "{}", item.mention()); 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 /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let message = MessageBuilder::new().push("test"); /// /// assert_eq!(message.push("ing").0, "testing"); /// ``` pub fn push<D: I>(mut self, content: D) -> Self { self.0.push_str(&content.into().to_string()); self } /// Pushes a codeblock to the content, with optional syntax highlighting. /// /// # Examples /// /// Pushing a Rust codeblock: /// /// ```rust,ignore /// 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: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let content = MessageBuilder::new() /// .push_codeblock("hello", None) /// .build(); /// /// assert_eq!(content, "```\nhello\n```"); /// ``` 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 } /// Pushes inlined monospaced text to the content. /// /// # Examples /// /// Display a server configuration value to the user: /// /// ```rust /// 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); /// ``` 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 } /// Pushes inlined italicized text to the content. /// /// # Examples /// /// Emphasize information to the user: /// /// ```rust /// 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); /// ``` 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 } /// Pushes an inline bold text to the content. 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 } /// Pushes an underlined inline text to the content. 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 } /// Pushes a strikethrough inline text to the content. 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 } /// Pushes the given text with a newline appended to the content. /// /// # Examples /// /// Push content and then append a newline: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let content = MessageBuilder::new().push_line("hello").push("world").build(); /// /// assert_eq!(content, "hello\nworld"); /// ``` pub fn push_line<D: I>(mut self, content: D) -> Self { self = self.push(content); self.0.push('\n'); self } /// Pushes inlined monospace text with an added newline to the content. /// /// # Examples /// /// Push content and then append a newline: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let content = MessageBuilder::new().push_mono_line("hello").push("world").build(); /// /// assert_eq!(content, "`hello`\nworld"); /// ``` pub fn push_mono_line<D: I>(mut self, content: D) -> Self { self = self.push_mono(content); self.0.push('\n'); self } /// Pushes an inlined italicized text with an added newline to the content. /// /// # Examples /// /// Push content and then append a newline: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let content = MessageBuilder::new().push_italic_line("hello").push("world").build(); /// /// assert_eq!(content, "_hello_\nworld"); /// ``` pub fn push_italic_line<D: I>(mut self, content: D) -> Self { self = self.push_italic(content); self.0.push('\n'); self } /// Pushes an inline bold text with an added newline to the content. /// /// # Examples /// /// Push content and then append a newline: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let content = MessageBuilder::new().push_bold_line("hello").push("world").build(); /// /// assert_eq!(content, "**hello**\nworld"); /// ``` pub fn push_bold_line<D: I>(mut self, content: D) -> Self { self = self.push_bold(content); self.0.push('\n'); self } /// Pushes an underlined inline text with an added newline to the content. /// /// # Examples /// /// Push content and then append a newline: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let content = MessageBuilder::new().push_underline_line("hello").push("world").build(); /// /// assert_eq!(content, "__hello__\nworld"); /// ``` pub fn push_underline_line<D: I>(mut self, content: D) -> Self { self = self.push_underline(content); self.0.push('\n'); self } /// Pushes a strikethrough inline text with a newline added to the content. /// /// # Examples /// /// Push content and then append a newline: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// let content = MessageBuilder::new().push_strike_line("hello").push("world").build(); /// /// assert_eq!(content, "~~hello~~\nworld"); /// ``` pub fn push_strike_line<D: I>(mut self, content: D) -> Self { self = self.push_strike(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) -> Self { { let mut c = content.into(); c.inner = normalize(&c.inner) .replace('*', "\\*") .replace('`', "\\`") .replace('_', "\\_"); self.0.push_str(&c.to_string()); } self } /// Pushes a code-block to your message normalizing content. 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 } /// Pushes an inline monospaced text to the content normalizing content. 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 } /// Pushes an inline italicized text to the content normalizing content. 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 } /// Pushes an inline bold text to the content normalizing content. 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 } /// Pushes an underlined inline text to the content normalizing content. 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 } /// Pushes a strikethrough inline text to the content normalizing content. 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 } /// Pushes text with a newline appended to the content normalizing content. /// /// # Examples /// /// Push content and then append a newline: /// /// ```rust /// 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?"); /// ``` pub fn push_line_safe<D: I>(mut self, content: D) -> Self { self = self.push_safe(content); self.0.push('\n'); self } /// Pushes an inline monospaced 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_mono_line_safe("`hello @everyone`") /// .push("world").build(); /// /// assert_eq!(content, "`'hello @\u{200B}everyone'`\nworld"); /// ``` pub fn push_mono_line_safe<D: I>(mut self, content: D) -> Self { self = self.push_mono_safe(content); self.0.push('\n'); self } /// Pushes an inline italicized 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_italic_line_safe("@everyone") /// .push("Isn't a mention.").build(); /// /// assert_eq!(content, "_@\u{200B}everyone_\nIsn't a mention."); /// ``` pub fn push_italic_line_safe<D: I>(mut self, content: D) -> Self { self = self.push_italic_safe(content); self.0.push('\n'); self } /// Pushes an inline bold 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_bold_line_safe("@everyone") /// .push("Isn't a mention.").build(); /// /// assert_eq!(content, "**@\u{200B}everyone**\nIsn't a mention."); /// ``` pub fn push_bold_line_safe<D: I>(mut self, content: D) -> Self { self = self.push_bold_safe(content); self.0.push('\n'); self } /// Pushes an underlined 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_underline_line_safe("@everyone") /// .push("Isn't a mention.").build(); /// /// assert_eq!(content, "__@\u{200B}everyone__\nIsn't a mention."); /// ``` pub fn push_underline_line_safe<D: I>(mut self, content: D) -> Self { self = self.push_underline_safe(content); self.0.push('\n'); self } /// Pushes a strikethrough 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_strike_line_safe("@everyone") /// .push("Isn't a mention.").build(); /// /// assert_eq!(content, "~~@\u{200B}everyone~~\nIsn't a mention."); /// ``` pub fn push_strike_line_safe<D: I>(mut self, content: D) -> Self { self = self.push_strike_safe(content); self.0.push('\n'); 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. /// /// [`Role`]: ../model/struct.Role.html /// [`RoleId`]: ../model/struct.RoleId.html /// [Display implementation]: ../model/struct.RoleId.html#method.fmt-1 pub fn role<R: Into<RoleId>>(mut self, role: R) -> Self { let _ = write!(self.0, "{}", role.into().mention()); 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. /// /// [`User`]: ../model/struct.User.html /// [`UserId`]: ../model/struct.UserId.html /// [Display implementation]: ../model/struct.UserId.html#method.fmt-1 pub fn user<U: Into<UserId>>(mut self, user: U) -> Self { let _ = write!(self.0, "{}", user.into().mention()); self } } impl Display for MessageBuilder { /// 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: /// /// ```rust /// use serenity::utils::MessageBuilder; /// /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) } } /// Formatting modifiers for MessageBuilder content pushes /// /// Provides an enum of formatting modifiers for a string, for combination with /// string types and Content types. /// /// # Examples /// /// Create a new Content type which describes a bold-italic "text": /// /// ```rust,no_run /// use serenity::utils::ContentModifier::{Bold, Italic}; /// use serenity::utils::Content; /// let content: Content = Bold + Italic + "text"; /// ``` pub enum ContentModifier { Italic, Bold, Strikethrough, Code, Underline, } /// Describes formatting on string content #[derive(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() } } /// This trait only exists as way to bypass the shouting of the compiler. Specifically "conflicting /// implementations in core" and alike. /// However is not meant to be used outside, nor implemented. pub trait I { fn into(self) -> Content; } impl<T: fmt::Display> I for T { fn into(self) -> Content { Content { italic: false, bold: false, strikethrough: false, inner: format!("{}", self), 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 { // Remove invite links and popular scam websites, mostly to prevent the // current user from triggering various ad detectors and prevent embeds. 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") // Remove right-to-left override and other similar annoying symbols .replace('\u{202E}', " ") // RTL Override .replace('\u{200F}', " ") // RTL Mark .replace('\u{202B}', " ") // RTL Embedding .replace('\u{200B}', " ") // Zero-width space .replace('\u{200D}', " ") // Zero-width joiner .replace('\u{200C}', " ") // Zero-width non-joiner // Remove everyone and here mentions. Has to be put after ZWS replacement // because it utilises it itself. .replace("@everyone", "@\u{200B}everyone") .replace("@here", "@\u{200B}here") }