aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIllia <[email protected]>2016-12-06 23:51:42 +0200
committerzeyla <[email protected]>2016-12-06 13:51:42 -0800
commit13de5c2e50410c3a68435dc774537b490bb7115c (patch)
tree798c633e9e06e520083b29fa4417f089f476d0e0 /src
parentFix changelog header (diff)
downloadserenity-13de5c2e50410c3a68435dc774537b490bb7115c.tar.xz
serenity-13de5c2e50410c3a68435dc774537b490bb7115c.zip
Improve Mentions, fix MessageBuilder
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.
Diffstat (limited to 'src')
-rw-r--r--src/model/channel.rs9
-rw-r--r--src/model/guild.rs5
-rw-r--r--src/model/id.rs68
-rw-r--r--src/model/misc.rs51
-rw-r--r--src/model/mod.rs7
-rw-r--r--src/model/user.rs9
6 files changed, 48 insertions, 101 deletions
diff --git a/src/model/channel.rs b/src/model/channel.rs
index 13f51b9..7036a1d 100644
--- a/src/model/channel.rs
+++ b/src/model/channel.rs
@@ -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>> {
@@ -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)
}
}
diff --git a/src/model/guild.rs b/src/model/guild.rs
index 2b5fbee..f11cc80 100644
--- a/src/model/guild.rs
+++ b/src/model/guild.rs
@@ -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 {
diff --git a/src/model/id.rs b/src/model/id.rs
index 5f534ec..032e722 100644
--- a/src/model/id.rs
+++ b/src/model/id.rs
@@ -1,4 +1,5 @@
use super::*;
+use std::fmt;
#[cfg(all(feature = "cache", feature = "methods"))]
use ::client::CACHE;
@@ -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.
@@ -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")]
@@ -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 {
@@ -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)
}
}
diff --git a/src/model/misc.rs b/src/model/misc.rs
index ebf1979..6e208bf 100644
--- a/src/model/misc.rs
+++ b/src/model/misc.rs
@@ -1,4 +1,3 @@
-use std::fmt;
use super::{
ChannelId,
Channel,
@@ -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)
}
}
diff --git a/src/model/mod.rs b/src/model/mod.rs
index 7cb0dfb..b5eda3c 100644
--- a/src/model/mod.rs
+++ b/src/model/mod.rs
@@ -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};
@@ -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)
diff --git a/src/model/user.rs b/src/model/user.rs
index 969b051..da50e46 100644
--- a/src/model/user.rs
+++ b/src/model/user.rs
@@ -6,7 +6,6 @@ use super::{
GuildContainer,
GuildId,
GuildInfo,
- Mention,
RoleId,
UserSettings,
User,
@@ -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;
@@ -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 {