Exemplo n.º 1
0
        /// <summary>
        /// Handles a search for Denizen meta.
        /// </summary>
        /// <typeparam name="T">The meta type.</typeparam>
        /// <param name="searchInput">The search input.</param>
        /// <param name="command">The command that initiated this search.</param>
        /// <param name="listOnly">Whether to only allow a list of results, rather than replying with exact or only matches.</param>
        /// <returns>Whether any results were found.</returns>
        public async Task HandleSearch <T>(string searchInput, BotCommand command, bool listOnly = false) where T : IDenizenMetaType
        {
            searchInput = searchInput.ToLower();
            if (string.IsNullOrWhiteSpace(searchInput))
            {
                return;
            }
            if (searchInput == "all")
            {
                string       allOf = typeof(T) == typeof(IDenizenMetaType) ? "meta" : (Meta.KnownMetaTypeNames[typeof(T)] + "s");
                List <Embed> pages = command.Bot.Meta.AllOf <T>().Paginate((tag) => tag.GetListString())
                                     .Select((page) => new EmbedBuilder().WithColor(Color.Gold).WithTitle("All known " + allOf).WithDescription(page).Build())
                                     .ToList();
                await command.ReplyAsync(new DiscordPaginatedMessage(pages));

                return;
            }
            if (searchInput.StartsWith('\\'))
            {
                searchInput = searchInput.Substring(1);
            }
            IEnumerable <SearchResult <T> > results = Meta.Search <T>(searchInput).OrderByDescending((x) => x.MatchLevel);
            SearchResult <T> first = results.FirstOrDefault();

            if (first.Result == null)
            {
                Type   type = typeof(T);
                string error;
                if (type == typeof(IDenizenMetaType))
                {
                    error = "Nothing was found matching the specified input. :pensive:";
                }
                else
                {
                    error = "No " + Meta.KnownMetaTypeNames[typeof(T)] + "s were found matching the specified input. :pensive:";
                }
                await command.ReplyAsync(new DiscordEmbedMessage(new EmbedBuilder().WithColor(Color.Red).WithDescription(error).Build()));

                return;
            }
            else if (!listOnly && (first.MatchLevel == SearchMatchLevel.EXACT || results.ElementAtOrDefault(1).Result == null))
            {
                await command.ReplyAsync(new DenizenMetaMessage(first.Result, first.MatchLevel));

                return;
            }
            else
            {
                List <Embed>      pages     = new List <Embed>();
                EmbedBuilder      builder   = new EmbedBuilder().WithColor(Color.Gold);
                SearchMatchLevel  lastLevel = SearchMatchLevel.NONE;
                EmbedFieldBuilder field     = null;
                string            value     = string.Empty;
                int length     = 0;
                int pageLength = 0;
                foreach (SearchResult <T> result in results)
                {
                    if (result.MatchLevel != lastLevel)
                    {
                        if (field != null)
                        {
                            builder.AddField(field.WithValue(value.Substring(0, length - 2)));
                            pageLength += length;
                            value       = string.Empty;
                            length      = 0;
                        }
                        field     = new EmbedFieldBuilder().WithName(MetaCommands.AdaptMatchLevelPlural(result.MatchLevel));
                        lastLevel = result.MatchLevel;
                        if (pageLength > 1250)
                        {
                            pages.Add(builder.Build());
                            builder = new EmbedBuilder().WithColor(Color.Gold);
                        }
                    }
                    string next      = result.Result.GetListString() + ", ";
                    int    newLength = next.Length + length;
                    if (newLength > 1000 || newLength + pageLength > 1500)
                    {
                        builder.AddField(field.WithValue(value.Substring(0, length - 2)));
                        pages.Add(builder.Build());
                        builder    = new EmbedBuilder().WithColor(Color.Gold);
                        field      = new EmbedFieldBuilder().WithName(MetaCommands.AdaptMatchLevelPlural(result.MatchLevel) + " [Continued]");
                        pageLength = 0;
                        value      = next;
                        length     = next.Length;
                    }
                    else
                    {
                        value += next;
                        length = newLength;
                    }
                }
                builder.AddField(field.WithValue(value.Substring(0, length - 2)));
                pages.Add(builder.Build());
                await command.ReplyAsync(new DiscordPaginatedMessage(pages));

                return;
            }
        }