Search in sources :

Example 1 with PaginatorBuilder

use of com.freya02.botcommands.api.pagination.paginator.PaginatorBuilder in project BotCommands by freya022.

the class PaginatorCommand method run.

@JDASlashCommand(name = "paginator")
public void run(GuildSlashEvent event) {
    final List<EmbedBuilder> embedBuilders = new ArrayList<>();
    // Let's suppose you generated embeds like in JDA-U, so you'd have a collection of embeds to present
    for (int i = 0; i < 5; i++) {
        embedBuilders.add(new EmbedBuilder().setTitle("Page #" + (i + 1)));
    }
    final Paginator paginator = new PaginatorBuilder().setConstraints(InteractionConstraints.ofUsers(event.getUser())).useDeleteButton(false).setMaxPages(5).setPaginatorSupplier((instance, messageBuilder, components, page) -> embedBuilders.get(page).build()).build();
    // You must send the paginator as a message
    event.reply(paginator.get()).setEphemeral(true).queue();
}
Also used : List(java.util.List) ApplicationCommand(com.freya02.botcommands.api.application.ApplicationCommand) InteractionConstraints(com.freya02.botcommands.api.components.InteractionConstraints) JDASlashCommand(com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Paginator(com.freya02.botcommands.api.pagination.paginator.Paginator) CommandMarker(com.freya02.botcommands.api.annotations.CommandMarker) GuildSlashEvent(com.freya02.botcommands.api.application.slash.GuildSlashEvent) PaginatorBuilder(com.freya02.botcommands.api.pagination.paginator.PaginatorBuilder) ArrayList(java.util.ArrayList) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ArrayList(java.util.ArrayList) Paginator(com.freya02.botcommands.api.pagination.paginator.Paginator) PaginatorBuilder(com.freya02.botcommands.api.pagination.paginator.PaginatorBuilder) JDASlashCommand(com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand)

Example 2 with PaginatorBuilder

use of com.freya02.botcommands.api.pagination.paginator.PaginatorBuilder in project BotCommands by freya022.

the class SlashPaginator method replyPaginator.

private void replyPaginator(GuildSlashEvent event, BContext context, InteractionConstraints constraints) {
    final PaginatorBuilder builder = new PaginatorBuilder().setConstraints(constraints).useDeleteButton(true).setTimeout(5, TimeUnit.SECONDS, (paginator, message) -> {
        paginator.cleanup(context);
        // Remove components on timeout
        event.getHook().editOriginalComponents().queue();
    // Disable all components on timeout, more expensive
    // event.getHook()
    // .retrieveOriginal()
    // .flatMap(m -> m.editMessageComponents(m.getActionRows().stream().map(ActionRow::asDisabled).toList()))
    // .queue();
    }).setMaxPages(5).setFirstContent(ButtonContent.withString("←")).setPaginatorSupplier((paginator, messageBuilder, components, page) -> {
        components.addComponents(1, Components.primaryButton(btnEvt -> {
            // Pages starts at 0
            paginator.setPage(2);
            btnEvt.editMessage(paginator.get()).queue();
        }).build(ButtonContent.withEmoji("Go to page 3", EmojiUtils.resolveJDAEmoji("page_facing_up"))));
        components.addComponents(1, Components.primaryButton(btnEvt -> {
            // Pages starts at 0
            paginator.setPage(4);
            btnEvt.editMessage(paginator.get()).queue();
        }).build(ButtonContent.withEmoji("Go to page 5", EmojiUtils.resolveJDAEmoji("page_facing_up"))));
        return new EmbedBuilder().setTitle(// Pages starts at 0
        "Page #" + (page + 1)).build();
    });
    final Paginator paginator = builder.build();
    event.reply(paginator.get()).setEphemeral(false).queue();
}
Also used : Components(com.freya02.botcommands.api.components.Components) InteractionConstraints(com.freya02.botcommands.api.components.InteractionConstraints) ButtonContent(com.freya02.botcommands.api.utils.ButtonContent) Permission(net.dv8tion.jda.api.Permission) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Paginator(com.freya02.botcommands.api.pagination.paginator.Paginator) TimeUnit(java.util.concurrent.TimeUnit) AppOption(com.freya02.botcommands.api.application.annotations.AppOption) Role(net.dv8tion.jda.api.entities.Role) ApplicationCommand(com.freya02.botcommands.api.application.ApplicationCommand) JDASlashCommand(com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand) GuildSlashEvent(com.freya02.botcommands.api.application.slash.GuildSlashEvent) BContext(com.freya02.botcommands.api.BContext) PaginatorBuilder(com.freya02.botcommands.api.pagination.paginator.PaginatorBuilder) EmojiUtils(com.freya02.botcommands.api.utils.EmojiUtils) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Paginator(com.freya02.botcommands.api.pagination.paginator.Paginator) PaginatorBuilder(com.freya02.botcommands.api.pagination.paginator.PaginatorBuilder)

Aggregations

ApplicationCommand (com.freya02.botcommands.api.application.ApplicationCommand)2 GuildSlashEvent (com.freya02.botcommands.api.application.slash.GuildSlashEvent)2 JDASlashCommand (com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand)2 InteractionConstraints (com.freya02.botcommands.api.components.InteractionConstraints)2 Paginator (com.freya02.botcommands.api.pagination.paginator.Paginator)2 PaginatorBuilder (com.freya02.botcommands.api.pagination.paginator.PaginatorBuilder)2 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)2 BContext (com.freya02.botcommands.api.BContext)1 CommandMarker (com.freya02.botcommands.api.annotations.CommandMarker)1 AppOption (com.freya02.botcommands.api.application.annotations.AppOption)1 Components (com.freya02.botcommands.api.components.Components)1 ButtonContent (com.freya02.botcommands.api.utils.ButtonContent)1 EmojiUtils (com.freya02.botcommands.api.utils.EmojiUtils)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 Permission (net.dv8tion.jda.api.Permission)1 Role (net.dv8tion.jda.api.entities.Role)1