public async Task RemindList(int page = 1) { if (--page < 0) { return; } var embed = new EmbedBuilder() .WithOkColor() .WithTitle(GetText("reminder_list")); List <Reminder> rems; using (var uow = _db.GetDbContext()) { rems = uow.Reminders.RemindersFor(ctx.User.Id, page) .ToList(); } if (rems.Any()) { var i = 0; foreach (var rem in rems) { var when = rem.When; var diff = when - DateTime.UtcNow; embed.AddField( $"#{++i + (page * 10)} {rem.When:HH:mm yyyy-MM-dd} UTC (in {(int) diff.TotalHours}h {(int) diff.Minutes}m)", $@"`Target:` {(rem.IsPrivate ? "DM" : "Channel")} `TargetId:` {rem.ChannelId} `Message:` {rem.Message?.TrimTo(50)}", false); } } else { embed.WithDescription(GetText("reminders_none")); } embed.AddPaginatedFooter(page + 1, null); await ctx.Channel.EmbedAsync(embed).ConfigureAwait(false); }
private static async Task SendPaginatedMessageAsync(this ICommandContext ctx, int currentPage, Func <int, Task <EmbedBuilder> > pageFunc, int totalElements, int itemsPerPage, bool addPaginatedFooter = true) { EmbedBuilder embed = await pageFunc(currentPage).ConfigureAwait(false); int lastPage = (totalElements - 1) / itemsPerPage; if (addPaginatedFooter) { embed.AddPaginatedFooter(currentPage, lastPage); } IUserMessage message = await ctx.Channel.EmbedAsync(embed).ConfigureAwait(false); if (lastPage == 0) { return; } await message.AddReactionAsync(First).ConfigureAwait(false); await message.AddReactionAsync(ArrowLeft).ConfigureAwait(false); await message.AddReactionAsync(ArrowRight).ConfigureAwait(false); await message.AddReactionAsync(Last).ConfigureAwait(false); await Task.Delay(2000).ConfigureAwait(false); var lastPageChange = DateTime.MinValue; async Task ChangePage(SocketReaction r) { try { if (r.UserId != ctx.User.Id) { return; } if (DateTime.UtcNow - lastPageChange < TimeSpan.FromMilliseconds(500)) { return; } EmbedBuilder updatedEmbed; if (r.Emote.Name == ArrowLeft.Name) { if (currentPage == 0) { return; } lastPageChange = DateTime.UtcNow; updatedEmbed = await pageFunc(--currentPage).ConfigureAwait(false); await message.RemoveReactionAsync(ArrowLeft, ctx.User).ConfigureAwait(false); } else if (r.Emote.Name == ArrowRight.Name) { if (currentPage == lastPage) { return; } lastPageChange = DateTime.UtcNow; updatedEmbed = await pageFunc(++currentPage).ConfigureAwait(false); await message.RemoveReactionAsync(ArrowRight, ctx.User).ConfigureAwait(false); } else if (r.Emote.Name == First.Name) { if (currentPage == 0) { return; } lastPageChange = DateTime.UtcNow; updatedEmbed = await pageFunc(0).ConfigureAwait(false); await message.RemoveReactionAsync(First, ctx.User).ConfigureAwait(false); } else if (r.Emote.Name == Last.Name) { if (currentPage == lastPage) { return; } lastPageChange = DateTime.UtcNow; updatedEmbed = await pageFunc(lastPage).ConfigureAwait(false); await message.RemoveReactionAsync(First, ctx.User).ConfigureAwait(false); } else { return; } if (addPaginatedFooter) { updatedEmbed.AddPaginatedFooter(currentPage, lastPage); } await message.ModifyAsync(x => x.Embed = updatedEmbed.Build()).ConfigureAwait(false); } catch (Exception) { //ignored } } using (message.OnReaction((DiscordSocketClient)ctx.Client, ChangePage)) { await Task.Delay(TimeSpan.FromMinutes(5)).ConfigureAwait(false); } try { if (message.Channel is ITextChannel && ((SocketGuild)ctx.Guild).CurrentUser.GuildPermissions.ManageMessages) { await message.RemoveAllReactionsAsync().ConfigureAwait(false); } else { await Task.WhenAll(message.Reactions.Where(x => x.Value.IsMe) .Select(x => message.RemoveReactionAsync(x.Key, ctx.Client.CurrentUser))); } } catch { // ignored } }