From e54ceba0aba5d770ffca2bfc368867384762aefd Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Tue, 17 Oct 2017 16:10:51 +0200 Subject: [PATCH] Small pagination bugfix --- .../Administration/SelfAssignedRolesCommands.cs | 2 +- .../Modules/Administration/SelfCommands.cs | 2 +- .../Modules/Administration/TimeZoneCommands.cs | 2 +- .../Modules/Administration/UserPunishCommands.cs | 2 +- .../Modules/CustomReactions/CustomReactions.cs | 14 ++++++-------- .../Modules/Gambling/FlowerShopCommands.cs | 2 +- NadekoBot.Core/Modules/Music/Music.cs | 6 +++--- .../Modules/Permissions/FilterCommands.cs | 2 +- NadekoBot.Core/Modules/Searches/FeedCommands.cs | 2 +- .../Modules/Utility/CommandMapCommands.cs | 2 +- .../Utility/Services/VerboseErrorsService.cs | 4 +--- NadekoBot.Core/Modules/Xp/Club.cs | 6 +++--- NadekoBot.Core/Modules/Xp/Xp.cs | 2 +- .../_Extensions/IMessageChannelExtensions.cs | 10 ++++++---- 14 files changed, 28 insertions(+), 30 deletions(-) diff --git a/NadekoBot.Core/Modules/Administration/SelfAssignedRolesCommands.cs b/NadekoBot.Core/Modules/Administration/SelfAssignedRolesCommands.cs index 7972b7d3..dc606fd9 100644 --- a/NadekoBot.Core/Modules/Administration/SelfAssignedRolesCommands.cs +++ b/NadekoBot.Core/Modules/Administration/SelfAssignedRolesCommands.cs @@ -146,7 +146,7 @@ namespace NadekoBot.Modules.Administration .WithTitle(GetText("self_assign_list", roleCnt)) .WithDescription(string.Join("\n", roles.Skip(curPage * 10).Take(10))) .WithOkColor(); - }, roles.Count / 10); + }, roles.Count, 10); } [NadekoCommand, Usage, Description, Aliases] diff --git a/NadekoBot.Core/Modules/Administration/SelfCommands.cs b/NadekoBot.Core/Modules/Administration/SelfCommands.cs index dd570b25..a672ba2d 100644 --- a/NadekoBot.Core/Modules/Administration/SelfCommands.cs +++ b/NadekoBot.Core/Modules/Administration/SelfCommands.cs @@ -259,7 +259,7 @@ namespace NadekoBot.Modules.Administration .WithTitle(status) .WithOkColor() .WithDescription(str); - }, allShardStrings.Length / 25); + }, allShardStrings.Length, 25); } [NadekoCommand, Usage, Description, Aliases] diff --git a/NadekoBot.Core/Modules/Administration/TimeZoneCommands.cs b/NadekoBot.Core/Modules/Administration/TimeZoneCommands.cs index 25d55dd2..3c433973 100644 --- a/NadekoBot.Core/Modules/Administration/TimeZoneCommands.cs +++ b/NadekoBot.Core/Modules/Administration/TimeZoneCommands.cs @@ -34,7 +34,7 @@ namespace NadekoBot.Modules.Administration .WithOkColor() .WithTitle(GetText("timezones_available")) .WithDescription(string.Join("\n", timezones.Skip(curPage * timezonesPerPage).Take(timezonesPerPage).Select(x => $"`{x.Id,-25}` {(x.BaseUtcOffset < TimeSpan.Zero? "-" : "+")}{x.BaseUtcOffset:hhmm}"))), - timezones.Length / timezonesPerPage); + timezones.Length, timezonesPerPage); } [NadekoCommand, Usage, Description, Aliases] diff --git a/NadekoBot.Core/Modules/Administration/UserPunishCommands.cs b/NadekoBot.Core/Modules/Administration/UserPunishCommands.cs index 1eb7dc94..1303e4e2 100644 --- a/NadekoBot.Core/Modules/Administration/UserPunishCommands.cs +++ b/NadekoBot.Core/Modules/Administration/UserPunishCommands.cs @@ -154,7 +154,7 @@ namespace NadekoBot.Modules.Administration .WithTitle(GetText("warnings_list")) .WithDescription(string.Join("\n", ws)); - }, warnings.Length / 15); + }, warnings.Length, 15); } [NadekoCommand, Usage, Description, Aliases] diff --git a/NadekoBot.Core/Modules/CustomReactions/CustomReactions.cs b/NadekoBot.Core/Modules/CustomReactions/CustomReactions.cs index 43b429bb..ca380d72 100644 --- a/NadekoBot.Core/Modules/CustomReactions/CustomReactions.cs +++ b/NadekoBot.Core/Modules/CustomReactions/CustomReactions.cs @@ -152,7 +152,6 @@ namespace NadekoBot.Modules.CustomReactions return; } - var lastPage = customReactions.Length / 20; await Context.Channel.SendPaginatedConfirmAsync(_client, page, curPage => new EmbedBuilder().WithOkColor() .WithTitle(GetText("name")) @@ -171,7 +170,7 @@ namespace NadekoBot.Modules.CustomReactions str = "📪" + str; } return str; - }))), lastPage) + }))), customReactions.Length, 20) .ConfigureAwait(false); } @@ -230,16 +229,15 @@ namespace NadekoBot.Modules.CustomReactions .GroupBy(cr => cr.Trigger) .OrderBy(cr => cr.Key) .ToList(); - - var lastPage = ordered.Count / 20; + await Context.Channel.SendPaginatedConfirmAsync(_client, page, (curPage) => new EmbedBuilder().WithOkColor() .WithTitle(GetText("name")) .WithDescription(string.Join("\r\n", ordered .Skip(curPage * 20) .Take(20) - .Select(cr => $"**{cr.Key.Trim().ToLowerInvariant()}** `x{cr.Count()}`"))), lastPage) - .ConfigureAwait(false); + .Select(cr => $"**{cr.Key.Trim().ToLowerInvariant()}** `x{cr.Count()}`"))), + ordered.Count, 20).ConfigureAwait(false); } } @@ -492,12 +490,12 @@ namespace NadekoBot.Modules.CustomReactions var ordered = _service.ReactionStats.OrderByDescending(x => x.Value).ToArray(); if (!ordered.Any()) return; - var lastPage = ordered.Length / 9; await Context.Channel.SendPaginatedConfirmAsync(_client, page, (curPage) => ordered.Skip(curPage * 9) .Take(9) .Aggregate(new EmbedBuilder().WithOkColor().WithTitle(GetText("stats")), - (agg, cur) => agg.AddField(efb => efb.WithName(cur.Key).WithValue(cur.Value.ToString()).WithIsInline(true))), lastPage) + (agg, cur) => agg.AddField(efb => efb.WithName(cur.Key).WithValue(cur.Value.ToString()).WithIsInline(true))), + ordered.Length, 9) .ConfigureAwait(false); } } diff --git a/NadekoBot.Core/Modules/Gambling/FlowerShopCommands.cs b/NadekoBot.Core/Modules/Gambling/FlowerShopCommands.cs index dc506ed2..27faf749 100644 --- a/NadekoBot.Core/Modules/Gambling/FlowerShopCommands.cs +++ b/NadekoBot.Core/Modules/Gambling/FlowerShopCommands.cs @@ -73,7 +73,7 @@ namespace NadekoBot.Modules.Gambling embed.AddField(efb => efb.WithName($"#{curPage * 9 + i + 1} - {entry.Price}{_bc.BotConfig.CurrencySign}").WithValue(EntryToString(entry)).WithIsInline(true)); } return embed; - }, entries.Count / 9, true); + }, entries.Count, 9, true); } [NadekoCommand, Usage, Description, Aliases] diff --git a/NadekoBot.Core/Modules/Music/Music.cs b/NadekoBot.Core/Modules/Music/Music.cs index bb5f58bc..d19266d7 100644 --- a/NadekoBot.Core/Modules/Music/Music.cs +++ b/NadekoBot.Core/Modules/Music/Music.cs @@ -258,7 +258,6 @@ namespace NadekoBot.Modules.Music total.Minutes, total.Seconds); var maxPlaytime = mp.MaxPlaytimeSeconds; - var lastPage = songs.Length / itemsPerPage; Func printAction = curPage => { var startAt = itemsPerPage * curPage; @@ -300,7 +299,7 @@ namespace NadekoBot.Modules.Music desc = add + "\n" + desc; var embed = new EmbedBuilder() - .WithAuthor(eab => eab.WithName(GetText("player_queue", curPage + 1, lastPage + 1)) + .WithAuthor(eab => eab.WithName(GetText("player_queue", curPage + 1, (songs.Length / itemsPerPage) + 1)) .WithMusicIcon()) .WithDescription(desc) .WithFooter(ef => ef.WithText($"{mp.PrettyVolume} | {songs.Length} " + @@ -309,7 +308,8 @@ namespace NadekoBot.Modules.Music return embed; }; - await Context.Channel.SendPaginatedConfirmAsync(_client, page, printAction, lastPage, false).ConfigureAwait(false); + await Context.Channel.SendPaginatedConfirmAsync(_client, + page, printAction, songs.Length, itemsPerPage, false).ConfigureAwait(false); } [NadekoCommand, Usage, Description, Aliases] diff --git a/NadekoBot.Core/Modules/Permissions/FilterCommands.cs b/NadekoBot.Core/Modules/Permissions/FilterCommands.cs index 5bb47657..7332c6ad 100644 --- a/NadekoBot.Core/Modules/Permissions/FilterCommands.cs +++ b/NadekoBot.Core/Modules/Permissions/FilterCommands.cs @@ -210,7 +210,7 @@ namespace NadekoBot.Modules.Permissions new EmbedBuilder() .WithTitle(GetText("filter_word_list")) .WithDescription(string.Join("\n", fws.Skip(curPage * 10).Take(10))) - , fws.Length / 10).ConfigureAwait(false); + , fws.Length, 10).ConfigureAwait(false); } } } diff --git a/NadekoBot.Core/Modules/Searches/FeedCommands.cs b/NadekoBot.Core/Modules/Searches/FeedCommands.cs index 629209c8..d5e57c39 100644 --- a/NadekoBot.Core/Modules/Searches/FeedCommands.cs +++ b/NadekoBot.Core/Modules/Searches/FeedCommands.cs @@ -103,7 +103,7 @@ namespace NadekoBot.Modules.Searches return embed.WithDescription(fs); - }, feeds.Count / 10); + }, feeds.Count, 10); } } } diff --git a/NadekoBot.Core/Modules/Utility/CommandMapCommands.cs b/NadekoBot.Core/Modules/Utility/CommandMapCommands.cs index 90fc7b26..212e8191 100644 --- a/NadekoBot.Core/Modules/Utility/CommandMapCommands.cs +++ b/NadekoBot.Core/Modules/Utility/CommandMapCommands.cs @@ -126,7 +126,7 @@ namespace NadekoBot.Modules.Utility .WithDescription(string.Join("\n", arr.Skip(curPage * 10).Take(10).Select(x => $"`{x.Key}` => `{x.Value}`"))); - }, arr.Length / 10).ConfigureAwait(false); + }, arr.Length, 10).ConfigureAwait(false); } } } diff --git a/NadekoBot.Core/Modules/Utility/Services/VerboseErrorsService.cs b/NadekoBot.Core/Modules/Utility/Services/VerboseErrorsService.cs index 32d6bb1b..58cb8db6 100644 --- a/NadekoBot.Core/Modules/Utility/Services/VerboseErrorsService.cs +++ b/NadekoBot.Core/Modules/Utility/Services/VerboseErrorsService.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Threading.Tasks; using Discord; using Discord.Commands; @@ -7,7 +6,6 @@ using NadekoBot.Common.Collections; using NadekoBot.Extensions; using NadekoBot.Modules.Help.Services; using NadekoBot.Core.Services; -using NadekoBot.Core.Services.Database.Models; namespace NadekoBot.Modules.Utility.Services { diff --git a/NadekoBot.Core/Modules/Xp/Club.cs b/NadekoBot.Core/Modules/Xp/Club.cs index f6791d4f..a6351cbf 100644 --- a/NadekoBot.Core/Modules/Xp/Club.cs +++ b/NadekoBot.Core/Modules/Xp/Club.cs @@ -139,7 +139,7 @@ namespace NadekoBot.Modules.Xp return embed.WithThumbnailUrl(club.ImageUrl); return embed; - }, club.Users.Count / 10); + }, club.Users.Count, 10); } [NadekoCommand, Usage, Description, Aliases] @@ -170,7 +170,7 @@ namespace NadekoBot.Modules.Xp .WithDescription(toShow) .WithOkColor(); - }, bans.Length / 10); + }, bans.Length, 10); } @@ -202,7 +202,7 @@ namespace NadekoBot.Modules.Xp .WithDescription(toShow) .WithOkColor(); - }, apps.Length / 10); + }, apps.Length, 10); } [NadekoCommand, Usage, Description, Aliases] diff --git a/NadekoBot.Core/Modules/Xp/Xp.cs b/NadekoBot.Core/Modules/Xp/Xp.cs index 5db4341a..aa4224f5 100644 --- a/NadekoBot.Core/Modules/Xp/Xp.cs +++ b/NadekoBot.Core/Modules/Xp/Xp.cs @@ -236,7 +236,7 @@ namespace NadekoBot.Modules.Xp } return embed; } - }, addPaginatedFooter: false); + }, 1000, 10, addPaginatedFooter: false); } [NadekoCommand, Usage, Description, Aliases] diff --git a/NadekoBot.Core/_Extensions/IMessageChannelExtensions.cs b/NadekoBot.Core/_Extensions/IMessageChannelExtensions.cs index 992d7453..ef2297d9 100644 --- a/NadekoBot.Core/_Extensions/IMessageChannelExtensions.cs +++ b/NadekoBot.Core/_Extensions/IMessageChannelExtensions.cs @@ -55,15 +55,17 @@ namespace NadekoBot.Extensions private static readonly IEmote arrow_left = new Emoji("⬅"); private static readonly IEmote arrow_right = new Emoji("➡"); - public static Task SendPaginatedConfirmAsync(this IMessageChannel channel, DiscordSocketClient client, int currentPage, Func pageFunc, int? lastPage = null, bool addPaginatedFooter = true) => - channel.SendPaginatedConfirmAsync(client, currentPage, (x) => Task.FromResult(pageFunc(x)), lastPage, addPaginatedFooter); + public static Task SendPaginatedConfirmAsync(this IMessageChannel channel, DiscordSocketClient client, int currentPage, Func pageFunc, int totalElements, int itemsPerPage, bool addPaginatedFooter = true) => + channel.SendPaginatedConfirmAsync(client, currentPage, (x) => Task.FromResult(pageFunc(x)), totalElements, itemsPerPage, addPaginatedFooter); /// /// danny kamisama /// - public static async Task SendPaginatedConfirmAsync(this IMessageChannel channel, DiscordSocketClient client, int currentPage, Func> pageFunc, int? lastPage = null, bool addPaginatedFooter = true) + public static async Task SendPaginatedConfirmAsync(this IMessageChannel channel, DiscordSocketClient client, int currentPage, Func> pageFunc, int totalElements, int itemsPerPage, bool addPaginatedFooter = true) { var embed = await pageFunc(currentPage).ConfigureAwait(false); + var lastPage = (totalElements - 1) / itemsPerPage; + if (addPaginatedFooter) embed.AddPaginatedFooter(currentPage, lastPage); @@ -93,7 +95,7 @@ namespace NadekoBot.Extensions } else if (r.Emote.Name == arrow_right.Name) { - if (lastPage == null || lastPage > currentPage) + if (lastPage > currentPage) { var toSend = await pageFunc(++currentPage).ConfigureAwait(false); if (addPaginatedFooter)