Search in sources :

Example 6 with Components

use of com.freya02.botcommands.api.components.Components in project BotCommands by freya022.

the class CommandsBuilderImpl method buildClasses.

private void buildClasses() throws Exception {
    classes.removeIf(c -> {
        try {
            return !ReflectionUtils.isInstantiable(c);
        } catch (IllegalAccessException | InvocationTargetException e) {
            LOGGER.error("An error occurred while trying to find if a class is instantiable", e);
            throw new RuntimeException("An error occurred while trying to find if a class is instantiable", e);
        }
    });
    for (Class<?> aClass : classes) {
        processClass(aClass);
    }
    if (!context.isHelpDisabled()) {
        processClass(HelpCommand.class);
        final TextCommandInfo helpInfo = context.findFirstCommand(CommandPath.of("help"));
        if (helpInfo == null)
            throw new IllegalStateException("HelpCommand did not build properly");
        final HelpCommand help = (HelpCommand) helpInfo.getInstance();
        help.generate();
    }
    prefixedCommandsBuilder.postProcess();
    if (context.getComponentManager() != null) {
        // Load button listeners
        for (Class<?> aClass : classes) {
            componentsBuilder.processClass(aClass);
        }
    } else {
        LOGGER.info("ComponentManager is not set, the Components API, paginators and menus won't be usable");
    }
    applicationCommandsBuilder.postProcess();
    if (context.getComponentManager() != null) {
        componentsBuilder.postProcess();
    }
    eventListenersBuilder.postProcess();
    autocompletionHandlersBuilder.postProcess();
    context.getRegistrationListeners().forEach(RegistrationListener::onBuildComplete);
    LOGGER.info("Finished registering all commands");
}
Also used : RegistrationListener(com.freya02.botcommands.api.RegistrationListener) HelpCommand(com.freya02.botcommands.internal.prefixed.HelpCommand) TextCommandInfo(com.freya02.botcommands.internal.prefixed.TextCommandInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 7 with Components

use of com.freya02.botcommands.api.components.Components in project BotCommands by freya022.

the class MessageComponents method execute.

@JDATextCommand(name = "components")
public void execute(CommandEvent event) {
    final ActionRow firstRow = ActionRow.of(Components.selectionMenu(SELECTION_HANDLER_NAME).addOption("Option 1", "Value 1").addOption("Option 2", "Value 2").addOption("Option 3", "Value 3").setPlaceholder("Select a value").build());
    final ActionRow secondRow = ActionRow.of(Components.primaryButton(BUTTON_HANDLER_NAME).build("Primary button (persistent, no group)"));
    event.getMessage().reply("Components !").setActionRows(firstRow, secondRow).queue();
}
Also used : ActionRow(net.dv8tion.jda.api.interactions.components.ActionRow) JDATextCommand(com.freya02.botcommands.api.prefixed.annotations.JDATextCommand)

Example 8 with Components

use of com.freya02.botcommands.api.components.Components in project BotCommands by freya022.

the class ExtensionsBotMain method main.

public static void main(String[] args) {
    try {
        final CommonMain.CommonStuff commonStuff = CommonMain.start();
        final JDA jda = commonStuff.getJDA();
        // Build the command framework:
        // Prefix: !
        // Owner: User with the ID 222046562543468545
        // Commands package: com.freya02.bot.extensionsbot.commands
        CommandsBuilder.newBuilder(222046562543468545L).textCommandBuilder(textCommandsBuilder -> textCommandsBuilder.addPrefix("!")).extensionsBuilder(extensionsBuilder -> extensionsBuilder.registerParameterResolver(// Support JDA's Timestamp in application commands and components
        new TimestampResolver())).setSettingsProvider(// Add our settings provider for the guilds
        new MySettingsProvider()).build(jda, // Registering listeners is taken care of by the lib
        "com.freya02.bot.extensionsbot.commands");
    } catch (Exception e) {
        LOGGER.error("Unable to start the bot", e);
        System.exit(-1);
    }
}
Also used : CommandsBuilder(com.freya02.botcommands.api.CommandsBuilder) JDA(net.dv8tion.jda.api.JDA) Logger(org.slf4j.Logger) CommonMain(com.freya02.bot.CommonMain) Logging(com.freya02.botcommands.api.Logging) JDA(net.dv8tion.jda.api.JDA) CommonMain(com.freya02.bot.CommonMain)

Example 9 with Components

use of com.freya02.botcommands.api.components.Components in project BotCommands by freya022.

the class SlashInteractiveMenu method interactiveMenu.

@JDASlashCommand(name = "interactive")
public void interactiveMenu(GuildSlashEvent event) {
    final InteractiveMenu menu = new InteractiveMenuBuilder().addMenu(SelectContent.of("Joy", "This sparks joy", Emoji.fromMarkdown("\uD83D\uDE02")), (interactiveMenu, messageBuilder, components) -> {
        components.addComponents(1, Components.dangerButton(buttonEvent -> {
            event.getHook().deleteOriginal().queue();
            interactiveMenu.cancelTimeout();
            interactiveMenu.cleanup(event.getContext());
        }).build("Delete"), Components.secondaryButton(buttonEvent -> {
            interactiveMenu.setSelectedItem("Grin");
            buttonEvent.editMessage(interactiveMenu.get()).queue();
        }).build("Go to 'Grin'"));
        return new EmbedBuilder().setTitle("This sparks joy").build();
    }).addMenu(SelectContent.of("Grin", "This does not spark joy", Emoji.fromMarkdown("\uD83D\uDE00")), (interactiveMenu, messageBuilder, components) -> {
        components.addComponents(1, Components.dangerButton(buttonEvent -> {
            event.getHook().deleteOriginal().queue();
            interactiveMenu.cancelTimeout();
            interactiveMenu.cleanup(event.getContext());
        }).build("Delete"), Components.secondaryButton(buttonEvent -> {
            interactiveMenu.setSelectedItem(0);
            buttonEvent.editMessage(interactiveMenu.get()).queue();
        }).build("Go to 'Joy'"));
        return new EmbedBuilder().setTitle("This does not spark joy").build();
    }).setConstraints(InteractionConstraints.ofUsers(event.getUser())).setTimeout(5, TimeUnit.SECONDS, (interactiveMenu, msg) -> {
        System.out.println("bru");
        interactiveMenu.cleanup(event.getContext());
    }).build();
    event.reply(menu.get()).setEphemeral(false).queue();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) InteractiveMenuBuilder(com.freya02.botcommands.api.pagination.interactive.InteractiveMenuBuilder) ApplicationCommand(com.freya02.botcommands.api.application.ApplicationCommand) Components(com.freya02.botcommands.api.components.Components) InteractionConstraints(com.freya02.botcommands.api.components.InteractionConstraints) JDASlashCommand(com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) InteractiveMenu(com.freya02.botcommands.api.pagination.interactive.InteractiveMenu) Emoji(net.dv8tion.jda.api.entities.Emoji) GuildSlashEvent(com.freya02.botcommands.api.application.slash.GuildSlashEvent) SelectContent(com.freya02.botcommands.api.pagination.interactive.SelectContent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) InteractiveMenu(com.freya02.botcommands.api.pagination.interactive.InteractiveMenu) InteractiveMenuBuilder(com.freya02.botcommands.api.pagination.interactive.InteractiveMenuBuilder) JDASlashCommand(com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand)

Example 10 with Components

use of com.freya02.botcommands.api.components.Components 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)3 GuildSlashEvent (com.freya02.botcommands.api.application.slash.GuildSlashEvent)3 JDASlashCommand (com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand)3 InteractionConstraints (com.freya02.botcommands.api.components.InteractionConstraints)3 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)3 JDA (net.dv8tion.jda.api.JDA)3 CommonMain (com.freya02.bot.CommonMain)2 Logging (com.freya02.botcommands.api.Logging)2 CommandPath (com.freya02.botcommands.api.application.CommandPath)2 Components (com.freya02.botcommands.api.components.Components)2 DefaultComponentManager (com.freya02.botcommands.api.components.DefaultComponentManager)2 Paginator (com.freya02.botcommands.api.pagination.paginator.Paginator)2 PaginatorBuilder (com.freya02.botcommands.api.pagination.paginator.PaginatorBuilder)2 IOException (java.io.IOException)2 TimeUnit (java.util.concurrent.TimeUnit)2 Guild (net.dv8tion.jda.api.entities.Guild)2 NotNull (org.jetbrains.annotations.NotNull)2 ComponentsDB (com.freya02.bot.ComponentsDB)1 BContext (com.freya02.botcommands.api.BContext)1 CommandsBuilder (com.freya02.botcommands.api.CommandsBuilder)1