private static void PrintResults(string searchString, WikiSearchSettings searchSettings = null) { WikiSearchResponse response = WikiSearcher.Search(searchString, searchSettings); Console.WriteLine($"\nResults found ({searchString}):\n"); foreach (WikiSearchResult result in response.Query.SearchResults) { Console.WriteLine( $"\t{result.Title} ({result.WordCount} words, {result.Size} bytes, id {result.PageId}):\t{result.Preview}...\n\tAt {result.Url(searchSettings.Language)} and {result.ConstantUrl(searchSettings.Language)}\n\tLast edited at {result.LastEdited}\n"); } }
private async Task WikiSearch(string search, ISocketMessageChannel channel, int maxSearch = 10) { EmbedBuilder embed = new EmbedBuilder(); StringBuilder sb = new StringBuilder(); embed.WithTitle($"Wikipedia Search '{search}'"); embed.WithColor(FunCmdsConfig.wikipediaSearchColor); embed.WithFooter($"Search by {Context.User}", Context.User.GetAvatarUrl()); embed.WithCurrentTimestamp(); embed.WithDescription("Searching Wikipedia..."); RestUserMessage message = await channel.SendMessageAsync("", false, embed.Build()); WikiSearchResponse response = WikiSearcher.Search(search, new WikiSearchSettings { ResultLimit = maxSearch }); foreach (WikiSearchResult result in response.Query.SearchResults) { string link = $"**[{result.Title}]({result.ConstantUrl("en")})** (Words: {result.WordCount})\n{result.Preview}\n\n"; //There is a character limit of 2048, so lets make sure we don't hit that if (sb.Length >= 2048) { continue; } if (sb.Length + link.Length >= 2048) { continue; } sb.Append(link); } embed.WithDescription(sb.ToString()); embed.WithCurrentTimestamp(); await MessageUtils.ModifyMessage(message, embed); }
public async Task SearchAsync(CommandContext ctx, [RemainingText, Description("Query.")] string query) { WikiSearchResponse res = await WikiService.SearchAsync(query); if (res is null || !res.Any()) { await this.InformFailureAsync(ctx, "No results..."); return; } await ctx.Client.GetInteractivity().SendPaginatedMessageAsync(ctx.Channel, ctx.User, res.Select(r => new Page(embed: new DiscordEmbedBuilder { Title = r.Title, Description = string.IsNullOrWhiteSpace(r.Snippet) ? "No description provided" : r.Snippet, Url = r.Url, Color = this.ModuleColor }.WithFooter("Powered by Wikipedia API", WikiService.WikipediaIconUrl) ))); }