Beispiel #1
0
        // https://discord.com/developers/docs/reference#message-formatting
        private object ParseReference(Type expectedType, string reference)
        {
            string value = reference.Substring(1, reference.Length - 2);

            // Get the object's ID (always last thing in the sequence)

            MatchCollection matches = Regex.Matches(value, @"\d{18,}");

            if (matches.Count > 0)
            {
                Match match = matches[matches.Count - 1];

                if (match.Index + match.Length == value.Length)
                {
                    ulong anyId = ulong.Parse(match.Value);

                    string forSpecific = value.Substring(0, match.Index);

                    if (expectedType.IsAssignableFrom(typeof(MinimalChannel)))
                    {
                        if (forSpecific == "#")
                        {
                            if (expectedType.IsAssignableFrom(typeof(DiscordChannel)))
                            {
                                if (_client.Config.Cache)
                                {
                                    return(_client.GetChannel(anyId));
                                }
                                else
                                {
                                    throw new InvalidOperationException("Caching must be enabled to parse DiscordChannels");
                                }
                            }
                            else
                            {
                                return(new MinimalTextChannel(anyId).SetClient(_client));
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Invalid reference type");
                        }
                    }
                    else if (expectedType == typeof(DiscordRole))
                    {
                        if (forSpecific.StartsWith("@&"))
                        {
                            if (_client.Config.Cache)
                            {
                                return(_client.GetGuildRole(anyId));
                            }
                            else
                            {
                                throw new InvalidOperationException("Caching must be enabled to parse DiscordChannels");
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Invalid reference type");
                        }
                    }
                    else if (expectedType.IsAssignableFrom(typeof(PartialEmoji)))
                    {
                        if (Regex.IsMatch(forSpecific, @"a?:\w+:"))
                        {
                            string[] split = forSpecific.Split(':');

                            bool   animated = split[0] == "a";
                            string name     = split[1];

                            if (expectedType == typeof(DiscordEmoji))
                            {
                                if (_client.Config.Cache)
                                {
                                    return(_client.GetGuildEmoji(anyId));
                                }
                                else
                                {
                                    throw new InvalidOperationException("Caching must be enabled to parase DiscordEmojis");
                                }
                            }
                            else
                            {
                                return(new PartialEmoji(anyId, name, animated).SetClient(_client));
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Invalid reference type");
                        }
                    }
                    else
                    {
                        return(anyId);
                    }
                }
            }

            throw new ArgumentException("Invalid reference");
        }