private async Task <KonachanRoot> GetPosts(string tags, int page = 1) { string queryUrl = $"{BASE_URL}{page}&{tags}"; using (HttpClient client = new HttpClient()) { HttpResponseMessage responseMessage = await client.GetAsync(queryUrl); if (!responseMessage.IsSuccessStatusCode) { return(null); } KonachanRoot konachanRoot = XmlHelper.DeserializeFromXmlString <KonachanRoot>(await responseMessage.Content.ReadAsStringAsync()); return(konachanRoot); } }
public async Task FetchWallpaper(CommandContext ctx, params string[] args) { await ctx.TriggerTypingAsync(); string tags = GetTagsString(args.ToList()); int postCount = (await GetPosts(tags))?.Count ?? 0; if (postCount <= 0) { await ctx.RespondAsync("No posts found with the given tag"); return; } int page = new RealRandom().Next(postCount); KonachanRoot posts = await GetPosts(tags, page); ERating rating = posts.KonachanPost.Rating switch { "e" => ERating.Explicit, "q" => ERating.Questionable, "s" => ERating.Safe, _ => ERating.Safe }; DiscordEmbed embed = new DiscordEmbedBuilder() .WithImageUrl(posts.KonachanPost.FileUrl) .WithAuthor(posts.KonachanPost.Author) .WithDescription( $"**Rating:** {rating}\n" + $"**Score:** {posts.KonachanPost.Score}\n" + $"**Tags:** {posts.KonachanPost.Tags}" ) .Build(); await ctx.RespondAsync(embed : embed); }