Skip to content

Commit

Permalink
Make parse_emoji accept animated emojis (#952)
Browse files Browse the repository at this point in the history
Previously, `parse_emoji` only returned true when the emoji was static
and animated emojis passed to the function returned `None` since they
include an extra character (<a:) to denote whether the emoji is animated.

Since we can tell whether an emoji is animated through the mention,
support to the parse_emoji function has been added to detect if an emoji is 
animated or not.

The `EmojiIdentifier` struct is altered to include the animated bool to reflect 
the change.
  • Loading branch information
bdashore3 committed Sep 5, 2020
1 parent 659448b commit 66e05ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/model/misc.rs
Expand Up @@ -181,9 +181,11 @@ impl_from_str! { struct:
Role, RoleId, RoleParseError, InvalidRole, parse_role, "invalid role";
}

/// A version of an emoji used only when solely the Id and name are known.
/// A version of an emoji used only when solely the animated state, Id, and name are known.
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct EmojiIdentifier {
/// Whether the emoji is animated
pub animated: bool,
/// The Id of the emoji.
pub id: EmojiId,
/// The name of the emoji. It must be at least 2 characters long and can
Expand Down
13 changes: 9 additions & 4 deletions src/utils/mod.rs
Expand Up @@ -247,7 +247,7 @@ pub fn parse_mention(mention: impl AsRef<str>) -> Option<u64> {
}
}

/// Retrieves the name and Id from an emoji mention, in the form of an
/// Retrieves the animated state, name and Id from an emoji mention, in the form of an
/// `EmojiIdentifier`.
///
/// If the emoji usage is invalid, then `None` is returned.
Expand All @@ -262,6 +262,7 @@ pub fn parse_mention(mention: impl AsRef<str>) -> Option<u64> {
/// use serenity::utils::parse_emoji;
///
/// let expected = EmojiIdentifier {
/// animated: false,
/// id: EmojiId(302516740095606785),
/// name: "smugAnimeFace".to_string(),
/// };
Expand All @@ -287,13 +288,16 @@ pub fn parse_emoji(mention: impl AsRef<str>) -> Option<EmojiIdentifier> {
return None;
}

if mention.starts_with("<:") && mention.ends_with('>') {
if (mention.starts_with("<:") || mention.starts_with("<a:")) && mention.ends_with('>') {
let mut name = String::default();
let mut id = String::default();
let animated = &mention[1..3] == "a:";

for (i, x) in mention[2..].chars().enumerate() {
let start = if animated { 3 } else { 2 };

for (i, x) in mention[start..].chars().enumerate() {
if x == ':' {
let from = i + 3;
let from = i + start + 1;

for y in mention[from..].chars() {
if y == '>' {
Expand All @@ -311,6 +315,7 @@ pub fn parse_emoji(mention: impl AsRef<str>) -> Option<EmojiIdentifier> {

match id.parse::<u64>() {
Ok(x) => Some(EmojiIdentifier {
animated,
name,
id: EmojiId(x),
}),
Expand Down

0 comments on commit 66e05ea

Please sign in to comment.