aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2018-09-21 22:45:14 +0200
committeracdenisSK <[email protected]>2018-09-22 18:34:32 +0200
commitd529cf79af4e493700aa9c69bbb690dbc47a80b8 (patch)
treeaf23e20d1812a309707ccd2600c64f325bd925be /src/utils
parentMake `trim` return `&mut self` (#395) (diff)
downloadserenity-d529cf79af4e493700aa9c69bbb690dbc47a80b8.tar.xz
serenity-d529cf79af4e493700aa9c69bbb690dbc47a80b8.zip
Generalise mention parsing
Fixes #396
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mod.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs
index 347e19a..2d1df4a 100644
--- a/src/utils/mod.rs
+++ b/src/utils/mod.rs
@@ -261,6 +261,31 @@ pub fn parse_channel(mention: &str) -> Option<u64> {
}
}
+/// Retrieve the ID number out of a channel, role, or user mention.
+///
+/// If the mention is invalid, `None` is returned.
+///
+/// # Examples
+///
+/// ```rust
+/// use serenity::utils::parse_mention;
+///
+/// assert_eq!(parse_mention("<@136510335967297536>"), Some(136510335967297536));
+/// assert_eq!(parse_mention("<@&137235212097683456>"), Some(137235212097683456));
+/// assert_eq!(parse_mention("<#137234234728251392>"), Some(137234234728251392));
+/// ```
+pub fn parse_mention(mention: &str) -> Option<u64> {
+ if mention.starts_with("<@&") {
+ parse_role(mention)
+ } else if mention.starts_with("<@") || mention.starts_with("<@!") {
+ parse_username(mention)
+ } else if mention.starts_with("<#") {
+ parse_channel(mention)
+ } else {
+ None
+ }
+}
+
/// Retrieves the name and Id from an emoji mention, in the form of an
/// `EmojiIdentifier`.
///