Skip to content

Commit

Permalink
Improve Mentions, fix MessageBuilder
Browse files Browse the repository at this point in the history
Remove the obsolete Mention struct as well as related methods, improve
the way mentioning works, fix the message builder, add a test for all
this.
  • Loading branch information
fwrs authored and zeyla committed Dec 6, 2016
1 parent 2a77e5c commit 13de5c2
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 101 deletions.
9 changes: 1 addition & 8 deletions src/model/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,13 +955,6 @@ impl GuildChannel {
CACHE.read().unwrap().get_guild(self.guild_id).cloned()
}

/// Return a [`Mention`] which will link to this channel.
///
/// [`Mention`]: struct.Mention.html
pub fn mention(&self) -> Mention {
self.id.mention()
}

/// Gets all channel's pins.
#[cfg(feature = "methods")]
pub fn pins(&self) -> Result<Vec<Message>> {
Expand Down Expand Up @@ -1023,7 +1016,7 @@ impl GuildChannel {
impl fmt::Display for GuildChannel {
/// Formas the channel, creating a mention of it.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.mention(), f)
fmt::Display::fmt(&self.id.mention(), f)
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/model/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,11 +1102,6 @@ impl Role {
self.permissions.contains(permissions)
}
}

/// Return a `Mention` which will ping members of the role.
pub fn mention(&self) -> Mention {
self.id.mention()
}
}

impl fmt::Display for Role {
Expand Down
68 changes: 28 additions & 40 deletions src/model/id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use std::fmt;

#[cfg(all(feature = "cache", feature = "methods"))]
use ::client::CACHE;
Expand Down Expand Up @@ -27,17 +28,6 @@ impl ChannelId {
rest::get_channel(self.0)
}

/// Returns a [`Mention`] which will link to the [`Channel`].
///
/// [`Channel`]: enum.Channel.html
/// [`Mention`]: struct.Mention.html
pub fn mention(&self) -> Mention {
Mention {
id: self.0,
prefix: "<#",
}
}

/// Retrieves the channel's webhooks.
///
/// **Note**: Requires the [Manage Webhooks] permission.
Expand Down Expand Up @@ -97,16 +87,6 @@ impl GuildId {
rest::get_guild(self.0)
}

/// Mentions the [`Guild`]'s default channel.
///
/// [`Guild`]: struct.Guild.html
pub fn mention(&self) -> Mention {
Mention {
id: self.0,
prefix: "<#",
}
}

/// Returns this Id as a `ChannelId`, which is useful when needing to use
/// the guild Id to send a message to the default channel.
#[cfg(feature = "methods")]
Expand Down Expand Up @@ -190,16 +170,6 @@ impl RoleId {
})
.cloned()
}

/// Returns a [`Mention`] which will ping members of the role.
///
/// [`Mention`]: struct.Mention.html
pub fn mention(&self) -> Mention {
Mention {
id: self.0,
prefix: "<@&",
}
}
}

impl From<CurrentUser> for UserId {
Expand All @@ -223,15 +193,33 @@ impl From<User> for UserId {
}
}

impl UserId {
/// Returns a [`Mention`] which will ping the user.
///
/// [`Mention`]: struct.Mention.html
pub fn mention(&self) -> Mention {
Mention {
id: self.0,
prefix: "<@",
}
impl fmt::Display for UserId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.mention(), f)
}
}

impl fmt::Display for RoleId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.mention(), f)
}
}

impl fmt::Display for ChannelId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.mention(), f)
}
}

impl fmt::Display for GuildId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

impl fmt::Display for EmojiId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

Expand Down
51 changes: 18 additions & 33 deletions src/model/misc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fmt;
use super::{
ChannelId,
Channel,
Expand All @@ -19,73 +18,59 @@ pub trait Mentionable {

impl Mentionable for ChannelId {
fn mention(&self) -> String {
format!("{}", self)
format!("<#{}>", self.0)
}
}

impl Mentionable for Channel {
fn mention(&self) -> String {
format!("{}", self)
match *self {
Channel::Guild(ref x) => {
format!("<#{}>", x.id.0)
},
Channel::Private(ref x) => {
format!("<#{}>", x.id.0)
},
Channel::Group(ref x) => {
format!("<#{}>", x.channel_id.0)
}
}
}
}

impl Mentionable for Emoji {
fn mention(&self) -> String {
format!("{}", self)
format!("<:{}:{}>", self.name, self.id.0)
}
}

impl Mentionable for Member {
fn mention(&self) -> String {
format!("{}", self.user)
format!("<@{}>", self.user.id.0)
}
}

impl Mentionable for RoleId {
fn mention(&self) -> String {
format!("{}", self)
format!("<@&{}>", self.0)
}
}

impl Mentionable for Role {
fn mention(&self) -> String {
format!("{}", self)
format!("<@&{}>", self.id.0)
}
}

impl Mentionable for UserId {
fn mention(&self) -> String {
format!("{}", self)
format!("<@{}>", self.0)
}
}

impl Mentionable for User {
fn mention(&self) -> String {
format!("{}", self)
}
}

/// A mention targeted at a certain model.
///
/// A mention can be created by calling `.mention()` on anything that is
/// mentionable - or an item's Id - and can be formatted into a string using
/// [`format!`]:
///
/// ```rust,ignore
/// let message = format!("Mentioning {}", user.mention());
/// ```
///
/// If a `String` is required, call `mention.to_string()`.
pub struct Mention {
pub prefix: &'static str,
pub id: u64,
}

impl fmt::Display for Mention {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(f.write_str(self.prefix));
try!(fmt::Display::fmt(&self.id, f));
fmt::Write::write_char(f, '>')
format!("<@{}>", self.id.0)
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ pub use self::webhook::*;

use self::utils::*;
use std::collections::HashMap;
use std::fmt;
use time::Timespec;
use ::internal::prelude::*;
use ::utils::{Colour, decode_array};
Expand Down Expand Up @@ -90,12 +89,6 @@ macro_rules! id {
}
}

impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<u64> for $name {
fn from(id_as_u64: u64) -> $name {
$name(id_as_u64)
Expand Down
9 changes: 1 addition & 8 deletions src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use super::{
GuildContainer,
GuildId,
GuildInfo,
Mention,
RoleId,
UserSettings,
User,
Expand All @@ -15,6 +14,7 @@ use ::client::rest::GuildPagination;
use ::internal::prelude::*;
use ::utils::builder::EditProfile;
use ::utils::decode_array;
use ::model::misc::Mentionable;

#[cfg(feature = "methods")]
use serde_json::builder::ObjectBuilder;
Expand Down Expand Up @@ -197,13 +197,6 @@ impl User {
},
}
}

/// Return a [`Mention`] which will ping this user.
///
/// [`Mention`]: struct.Mention.html
pub fn mention(&self) -> Mention {
self.id.mention()
}
}

impl fmt::Display for User {
Expand Down
53 changes: 53 additions & 0 deletions tests/test_msg_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
extern crate serenity;

use serenity::utils::MessageBuilder;
use serenity::model::Emoji;
use serenity::model::EmojiId;
use serenity::model::UserId;

#[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!(content != "@everyone discord.gg/discord-api");
}


#[test]
fn no_free_formatting() {
let content = MessageBuilder::new()
.push_bold_safe("test**test")
.build();
assert!(content != "**test**test**");
}

#[test]
fn mentions() {
let content_emoji = MessageBuilder::new()
.emoji(Emoji {
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>");
}

0 comments on commit 13de5c2

Please sign in to comment.