diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/model/misc.rs | 2 | ||||
| -rw-r--r-- | src/utils/mod.rs | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/src/model/misc.rs b/src/model/misc.rs index ff0f3de..9c09a9b 100644 --- a/src/model/misc.rs +++ b/src/model/misc.rs @@ -156,7 +156,7 @@ macro_rules! impl_from_str { type Err = $err; fn from_str(s: &str) -> StdResult<Self, Self::Err> { - Ok(match utils::parse_username(s) { + Ok(match utils::parse_mention(s) { Some(id) => $id(id), None => s.parse::<u64>().map($id).map_err(|_| $err::InvalidFormat)?, }) 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`. /// |