public async Task Help(int page = 1) { if (page <= 0) { throw new DiscordCommandException("Number too low", $"{Context.User.Mention}, you can't go to {(page == 0 ? "the zeroth" : "a negative")} page"); } const int NUM_PER_PAGE = 5; int totalPages = (int)Math.Ceiling(HelpHelper.AllCommands.Count / (double)NUM_PER_PAGE); if (page > totalPages) { throw new DiscordCommandException("Number too high", $"{Context.User.Mention}, you can't go to page {page}, there are only {totalPages}"); } ReactionMessageHelper.CreatePaginatedMessage(Context, await ReplyAsync(embed: buildPage(page)), totalPages, page, m => { return(Task.FromResult(((string)null, buildPage(m.CurrentPage)))); }); Embed buildPage(int num) { EmbedBuilder result = new EmbedBuilder(); result.WithTitle("Help"); result.WithFooter($"Page {num} of {totalPages}"); var commands = HelpHelper.AllCommands.Skip((num - 1) * NUM_PER_PAGE).Take(NUM_PER_PAGE); foreach (var c in commands.OrderBy(c => c.Command)) { result.AddField(c.ToString(), c.Summary ?? "*No help text provided*"); } return(result.Build()); } }
public async Task Pages() { var message = await ReplyAsync("Page 1"); ReactionMessageHelper.CreatePaginatedMessage(Context, message, 10, 1, m => { return(Task.FromResult(($"Page {m.CurrentPage}", (Embed)null))); });
public async Task ViewListings([Summary("Which market to see listings in, either \"global\" or the \"local\" server market")] Market market, [Summary("Which emoji to see listings of, or \"all\"")] string emoji = "all", [Summary("How to sort the listings. Try \"pricy\" or \"cheap\""), Remainder] string sorting = "lowest") { string[] HIGH_TO_LOW = { "highest", "highest first", "highest to lowest", "greatest to least", "expensive", "pricy", "g2l", "h2l" }; string[] LOW_TO_HIGH = { "lowest", "lowest first", "lowest to highest", "least to greatest", "cheap", "affordable", "l2g", "l2h" }; var m = MarketHelper.GetOrCreate(Context.MarketCollection, MarketId(market)); IEnumerable <(string e, Listing l)> listings; if (emoji == "all") { listings = m.Listings.SelectMany(kv => kv.Value.Select(l => (kv.Key, l))); if (listings.Count() == 0) { throw new DiscordCommandException("Nothing to show", $"There are no listings in {Context.GetMarketName(MarketId(market))}"); } } else { if (!EmojiHelper.IsValidEmoji(emoji)) { throw new DiscordCommandException("Bad emoji", $"{emoji} cannot be bought or sold"); } if (!m.Listings.ContainsKey(emoji)) { throw new DiscordCommandException("Nothing to show", $"There are no listings for {emoji} in {Context.GetMarketName(MarketId(market))}"); } listings = m.Listings[emoji].Select(l => (emoji, l)); } if (HIGH_TO_LOW.Contains(sorting.Trim())) { listings = listings.OrderByDescending(p => p.l.Price); } else { listings = listings.OrderBy(p => p.l.Price); } string title = $"{(emoji == "all" ? "All listings" : emoji)} in {Context.GetMarketName(MarketId(market))}"; const int NUM_PER_PAGE = 10; int totalPages = (listings.Count() + NUM_PER_PAGE - 1) / NUM_PER_PAGE; Embed getPage(int page) { EmbedBuilder builder = new EmbedBuilder(); StringBuilder stringBuilder = new StringBuilder(); List <string> contents = new List <string>(); foreach (var(e, listing) in listings.Skip((page - 1) * NUM_PER_PAGE).Take(NUM_PER_PAGE)) { stringBuilder.Append($"{e} for {Context.Money(listing.Price)} by {(market == Market.G ? Context.Bot.Client.GetUser(listing.SellerId).ToString() : Context.WhatDoICall(listing.SellerId))}\n"); } builder.AddField(new EmbedFieldBuilder().WithName(title).WithValue(stringBuilder.ToString())); builder.Footer = new EmbedFooterBuilder().WithText($"Page {page} of {totalPages}"); return(builder.Build()); } var message = await ReplyAsync(embed : getPage(1)); ReactionMessageHelper.CreatePaginatedMessage(Context, message, totalPages, 1, pg => Task.FromResult(("", getPage(pg.CurrentPage)))); }