Esempio n. 1
0
        private async Task FindInDocs(DocumentationProvider docProvider, string query, ICommandContext context)
        {
            var builder = new EmbedBuilder()
                          .WithColor(100, 149, 237);

            if (!docProvider.IsAvailable)
            {
                builder.WithDescription($"Sorry, but the {docProvider.FriendlyName} is not available at this time");
                await context.Channel.SendMessageAsync("", embed : builder.Build());

                return;
            }

            builder.WithDescription($"Searching the {docProvider.FriendlyName}...");
            var msg = await context.Channel.SendMessageAsync("", embed : builder.Build());

            var timer     = Stopwatch.StartNew();
            var articles  = cache.Get(docProvider.FriendlyName, query);
            var fromCache = true;

            if (articles != null)
            {
                await logger.LogDebug("Query found in cache", "DocumentationService");
            }
            else
            {
                fromCache = false;
                await logger.LogDebug("Query not in cache", "DocumentationService");

                try
                {
                    articles = await docProvider.SearchArticlesAsync(query);
                }
                catch (Exception ex)
                {
                    await logger.LogError("Query failed",
                                          "DocumentationService", ex);

                    builder = new EmbedBuilder()
                              .WithColor(100, 149, 237)
                              .WithDescription("I couldn't find anything because of an error :(");
                    await msg.ModifyAsync(f => f.Embed = builder.Build());

                    throw;
                }
                finally
                {
                    timer.Stop();
                    await logger.LogVerbose($"Query completed in {Math.Round(timer.Elapsed.TotalMilliseconds)}ms",
                                            "DocumentationService");
                }
                await cache.Add(docProvider.FriendlyName, query, articles, docProvider.CacheTtl);
            }

            await logger.LogDebug($"{articles?.Count ?? 0} articles found", "DocumentationService");

            builder = new EmbedBuilder()
                      .WithColor(100, 149, 237)
                      .WithFooter(
                fromCache
                    ? $"This query was retrieved from the cache in {Math.Round(timer.Elapsed.TotalMilliseconds)}ms"
                    : $"This query completed in {Math.Round(timer.Elapsed.TotalMilliseconds)}ms");

            if (!articles?.Any() ?? true)
            {
                builder.WithDescription("No results");
            }
            else
            {
                foreach (var article in articles.Take(3))
                {
                    article.AddToEmbed(builder);
                }
            }

            await msg.ModifyAsync(f => f.Embed = builder.Build());
        }