示例#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));
        }
    }
示例#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.");
        }
    }