diff options
| author | Illia <[email protected]> | 2017-01-11 15:59:40 +0300 |
|---|---|---|
| committer | Illia <[email protected]> | 2017-01-11 15:59:40 +0300 |
| commit | e5a83dd1873e5af2df18835d960fe19286c70f1e (patch) | |
| tree | 1a59db66800217f36d1fef4dbfd74cacaca33726 /src/model | |
| parent | Stop abusing unicode for sanitisation (diff) | |
| download | serenity-e5a83dd1873e5af2df18835d960fe19286c70f1e.tar.xz serenity-e5a83dd1873e5af2df18835d960fe19286c70f1e.zip | |
Add Message.content_safe and fix distinct method not being included in User.
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel.rs | 27 | ||||
| -rw-r--r-- | src/model/guild.rs | 6 | ||||
| -rw-r--r-- | src/model/user.rs | 8 |
3 files changed, 40 insertions, 1 deletions
diff --git a/src/model/channel.rs b/src/model/channel.rs index e9d60a8..9478949 100644 --- a/src/model/channel.rs +++ b/src/model/channel.rs @@ -625,6 +625,33 @@ impl Message { } } + /// Returns message content, but with user and role mentions replaced with + /// names and everyone/here mentions cancelled. + #[cfg(all(feature="cache", feature="methods"))] + pub fn content_safe(&self) -> String { + let mut result = self.content; + + // First replace all user mentions. + for u in self.mentions { + result = result.replace(u.mention(), u.distinct()); + } + + // Then replace all role mentions. + for id in self.mention_roles { + let mention = id.mention(); + + if let Some(role) = id.find() { + result = result.replace(mention, format!("@{}", role.name)); + } else { + result = result.replace(mention, "@deleted-role"); + } + } + + // And finally replace everyone and here mentions. + result.replace("@everyone", "@\u{200B}everyone") + .replace("@here", "@\u{200B}here") + } + /// Retrieves the Id of the guild that the message was sent in, if sent in /// one. /// diff --git a/src/model/guild.rs b/src/model/guild.rs index ba0f99c..f28188f 100644 --- a/src/model/guild.rs +++ b/src/model/guild.rs @@ -1079,6 +1079,12 @@ impl Member { self.nick.as_ref().unwrap_or(&self.user.name) } + /// Returns the DiscordTag of a Member, taking possible nickname into account. + #[cfg(feature="methods")] + pub fn distinct(&self) -> String { + format!("{}#{}", self.display_name(), self.discriminator) + } + /// Edits the member with the given data. See [`Context::edit_member`] for /// more information. /// diff --git a/src/model/user.rs b/src/model/user.rs index 83e80b9..253219a 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -50,7 +50,7 @@ impl CurrentUser { }) } - /// Returns the DiscordTag™ of a User. + /// Returns the DiscordTag of a User. #[cfg(feature="methods")] pub fn distinct(&self) -> String { format!("{}#{}", self.name, self.discriminator) @@ -132,6 +132,12 @@ impl User { }) } + /// Returns the DiscordTag of a User. + #[cfg(feature="methods")] + pub fn distinct(&self) -> String { + format!("{}#{}", self.name, self.discriminator) + } + /// Retrieves the time that this user was created at. #[cfg(feature="methods")] #[inline] |