Ejemplo n.º 1
0
        /// <summary>
        /// Creates a DiscordEmoji from emote name that includes colons (eg. :thinking:). This method also supports skin tone variations (eg. :ok_hand::skin-tone-2:), standard emoticons (eg. :D), as well as guild emoji (still specified by :name:).
        /// </summary>
        /// <param name="client"><see cref="BaseDiscordClient"/> to attach to the object.</param>
        /// <param name="name">Name of the emote to find, including colons (eg. :thinking:).</param>
        /// <returns>Create <see cref="DiscordEmoji"/> object.</returns>
        public static DiscordEmoji FromName(BaseDiscordClient client, string name)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client), "Client cannot be null.");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name), "Name cannot be empty or null.");
            }

            if (UnicodeEmojis.ContainsKey(name))
            {
                return new DiscordEmoji {
                           Discord = client, Name = UnicodeEmojis[name]
                }
            }
            ;

            var allEmojis = client.Guilds.Values.SelectMany(xg => xg.Emojis.Values).OrderBy(xe => xe.Name);

            var ek = name.Substring(1, name.Length - 2);

            foreach (var emoji in allEmojis)
            {
                if (emoji.Name == ek)
                {
                    return(emoji);
                }
            }

            throw new ArgumentException("Invalid emoji name specified.", nameof(name));
        }
    }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a DiscordEmoji from emote name that includes colons (eg. :thinking:). This method also supports skin tone variations (eg. :ok_hand::skin-tone-2:), standard emoticons (eg. :D), as well as guild emoji (still specified by :name:).
        /// </summary>
        /// <param name="client"><see cref="BaseDiscordClient"/> to attach to the object.</param>
        /// <param name="name">Name of the emote to find, including colons (eg. :thinking:).</param>
        /// <returns>Create <see cref="DiscordEmoji"/> object.</returns>
        public static DiscordEmoji FromName(BaseDiscordClient client, string name)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client), "Client cannot be null.");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name), "Name cannot be empty or null.");
            }

            if (UnicodeEmojis.ContainsKey(name))
            {
                return new DiscordEmoji {
                           Discord = client, Name = UnicodeEmojis[name]
                }
            }
            ;

            var ed = client.Guilds.Values.SelectMany(xg => xg.Emojis)
                     .OrderBy(xe => xe.Name)
                     .GroupBy(xe => xe.Name)
                     .ToDictionary(xg => xg.Key, xg => xg);
            var ek = name.Substring(1, name.Length - 2);

            if (ed.ContainsKey(ek))
            {
                return(ed[ek].First());
            }

            throw new ArgumentException(nameof(name), "Invalid emoji name specified.");
        }
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Attempts to create an emoji object from a unicode entity.
        /// </summary>
        /// <param name="client"><see cref="BaseDiscordClient"/> to attach to the object.</param>
        /// <param name="unicodeEntity">Unicode entity to create the object from.</param>
        /// <param name="emoji">Resulting <see cref="DiscordEmoji"/> object.</param>
        /// <returns>Whether the operation was successful.</returns>
        public static bool TryFromUnicode(BaseDiscordClient client, string unicodeEntity, out DiscordEmoji emoji)
        {
            // this is a round-trip operation because of FE0F inconsistencies.
            // through this, the inconsistency is normalized.

            emoji = null;
            if (!DiscordNameLookup.TryGetValue(unicodeEntity, out var discordName))
            {
                return(false);
            }

            if (!UnicodeEmojis.TryGetValue(discordName, out unicodeEntity))
            {
                return(false);
            }

            emoji = new DiscordEmoji {
                Name = unicodeEntity, Discord = client
            };
            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Attempts to create an emoji obejct from emote name that includes colons (eg. :thinking:). This method also
        /// supports skin tone variations (eg. :ok_hand::skin-tone-2:), standard emoticons (eg. :D), as well as guild
        /// emoji (still specified by :name:).
        /// </summary>
        /// <param name="client"><see cref="BaseDiscordClient"/> to attach to the object.</param>
        /// <param name="name">Name of the emote to find, including colons (eg. :thinking:).</param>
        /// <param name="includeGuilds">Should guild emojis be included in the search.</param>
        /// <param name="emoji">Resulting <see cref="DiscordEmoji"/> object.</param>
        /// <returns>Whether the operation was successful.</returns>
        public static bool TryFromName(BaseDiscordClient client, string name, bool includeGuilds, out DiscordEmoji emoji)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client), "Client cannot be null.");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name), "Name cannot be empty or null.");
            }

            if (UnicodeEmojis.TryGetValue(name, out var unicodeEntity))
            {
                emoji = new DiscordEmoji {
                    Discord = client, Name = unicodeEntity
                };
                return(true);
            }

            if (includeGuilds)
            {
                var allEmojis = client.Guilds.Values
                                .SelectMany(xg => xg.Emojis.Values); // save cycles - don't order

                var ek = name.AsSpan().Slice(1, name.Length - 2);
                foreach (var xemoji in allEmojis)
                {
                    if (xemoji.Name.AsSpan().SequenceEqual(ek))
                    {
                        emoji = xemoji;
                        return(true);
                    }
                }
            }

            emoji = null;
            return(false);
        }