fix: xplb and xpglb pagination fixed, closes #430

fix: Page number when there is an unknown number of items while paginating is now correct
fix: .stm and .stma fixed and can now mention everyone as long as the user executing the command also can
dev: Cleaned up/improved some code
This commit is contained in:
Kwoth
2024-05-15 13:44:20 +00:00
parent 803fe5db2f
commit a52a246982
13 changed files with 183 additions and 134 deletions

View File

@@ -58,7 +58,7 @@ public partial class ResponseBuilder
cb.WithButton(new ButtonBuilder()
.WithStyle(ButtonStyle.Primary)
.WithCustomId(BUTTON_RIGHT)
.WithDisabled(lastPage == 0 || currentPage >= lastPage)
.WithDisabled(lastPage is not null && (lastPage == 0 || currentPage >= lastPage))
.WithEmote(InteractionHelpers.ArrowRight));
return cb;

View File

@@ -1,5 +1,6 @@
using NadekoBot.Common.Configs;
using NadekoBot.Db.Models;
using System.Collections.ObjectModel;
namespace NadekoBot.Extensions;
@@ -357,10 +358,9 @@ public sealed partial class ResponseBuilder
fileName = name;
return this;
}
public PaginatedResponseBuilder Paginated()
=> new(this);
}
public class PaginatedResponseBuilder
@@ -376,7 +376,7 @@ public class PaginatedResponseBuilder
=> new SourcedPaginatedResponseBuilder<T>(_builder)
.Items(items);
public SourcedPaginatedResponseBuilder<T> PageItems<T>(Func<int, Task<IEnumerable<T>>> items)
public SourcedPaginatedResponseBuilder<T> PageItems<T>(Func<int, Task<IReadOnlyCollection<T>>> items)
=> new SourcedPaginatedResponseBuilder<T>(_builder)
.PageItems(items);
}
@@ -390,14 +390,14 @@ public sealed class SourcedPaginatedResponseBuilder<T> : PaginatedResponseBuilde
return Task.FromResult<EmbedBuilder>(new());
};
public Func<int, Task<IEnumerable<T>>> ItemsFunc { get; set; } = static delegate
public Func<int, Task<IReadOnlyCollection<T>>> ItemsFunc { get; set; } = static delegate
{
return Task.FromResult(Enumerable.Empty<T>());
return Task.FromResult<IReadOnlyCollection<T>>(ReadOnlyCollection<T>.Empty);
};
public Func<int, Task<SimpleInteractionBase>>? InteractionFunc { get; private set; }
public int Elems { get; private set; } = 1;
public int? Elems { get; private set; } = 1;
public int ItemsPerPage { get; private set; } = 9;
public bool AddPaginatedFooter { get; private set; } = true;
public bool IsEphemeral { get; private set; }
@@ -413,18 +413,19 @@ public sealed class SourcedPaginatedResponseBuilder<T> : PaginatedResponseBuilde
{
items = col;
Elems = col.Count;
ItemsFunc = (i) => Task.FromResult(items.Skip(i * ItemsPerPage).Take(ItemsPerPage));
ItemsFunc = (i) => Task.FromResult(items.Skip(i * ItemsPerPage).Take(ItemsPerPage).ToArray() as IReadOnlyCollection<T>);
return this;
}
public SourcedPaginatedResponseBuilder<T> TotalElements(int i)
{
Elems = i;
return this;
}
public SourcedPaginatedResponseBuilder<T> PageItems(Func<int, Task<IEnumerable<T>>> func)
public SourcedPaginatedResponseBuilder<T> PageItems(Func<int, Task<IReadOnlyCollection<T>>> func)
{
Elems = null;
ItemsFunc = func;
return this;
}

View File

@@ -167,7 +167,8 @@ public static class Extensions
{
if (lastPage is not null)
return embed.WithFooter($"{curPage + 1} / {lastPage + 1}");
return embed.WithFooter(curPage.ToString());
return embed.WithFooter((curPage + 1).ToString());
}
// public static EmbedBuilder WithOkColor(this EmbedBuilder eb)