Class InteractiveMenu


  • public class InteractiveMenu
    extends Object
    Utility to create interactive menus in Discord. An interactive menu first sends a message as a prompt and waits for a interaction from the user. The said interaction can be either a message or a reaction.
    • Method Detail

      • create

        public static InteractiveMenu create​(Consumer<discord4j.core.spec.MessageCreateSpec> spec)
        Creates a new empty InteractiveMenu with a given message that will serve as menu prompt.
        Parameters:
        spec - the spec to build the menu message
        Returns:
        a new InteractiveMenu
      • create

        public static InteractiveMenu create​(String message)
        Creates a new empty InteractiveMenu with a given message that will serve as menu prompt.
        Parameters:
        message - the menu message
        Returns:
        a new InteractiveMenu
      • create

        public static InteractiveMenu create​(reactor.core.publisher.Mono<Consumer<discord4j.core.spec.MessageCreateSpec>> specMono)
        Creates a new empty InteractiveMenu with a given message that will serve as menu prompt. The menu message may be supplied from an asynchronous source.
        Parameters:
        specMono - the Mono emitting the spec to build the menu message
        Returns:
        a new InteractiveMenu
      • createPaginated

        public static InteractiveMenu createPaginated​(@Nullable
                                                      AtomicInteger currentPageRef,
                                                      PaginationControls controls,
                                                      IntFunction<MessageSpecTemplate> paginator)
        Creates a new InteractiveMenu prefilled with menu items useful for pagination.
        Parameters:
        currentPageRef - an AtomicInteger that stores the current page number, may be null if you don't need it
        controls - the emojis to use for reaction-based navigation controls
        paginator - a Function that generates the message to display according to the current page number. If the page number is out of range, the function may throw a PageNumberOutOfRangeException which is handled by default to cover cases where the user inputs an invalid page number. Note that if the paginator function throws PageNumberOutOfRangeException with min/max values that aren't the same depending on the current page number, the behavior of the InteractiveMenu will be undefined.
        Returns:
        a new InteractiveMenu prefilled with menu items useful for pagination.
      • createAsyncPaginated

        public static InteractiveMenu createAsyncPaginated​(@Nullable
                                                           AtomicInteger currentPageRef,
                                                           PaginationControls controls,
                                                           IntFunction<reactor.core.publisher.Mono<MessageSpecTemplate>> asyncPaginator)
        Creates a new InteractiveMenu prefilled with menu items useful for pagination. Unlike createPaginated(AtomicInteger, PaginationControls, IntFunction), this method support asynchronous paginator functions.
        Parameters:
        currentPageRef - an AtomicInteger that stores the current page number, may be null if you don't need it
        controls - the emojis to use for reaction-based navigation controls
        asyncPaginator - a Function that asynchronously generates the message to display according to the current page number. If the page number is out of range, the Mono returned by this function may emit a PageNumberOutOfRangeException which is handled by default to cover cases where the user inputs an invalid page number. Note that if PageNumberOutOfRangeException is emitted with min/max values that aren't the same depending on the current page number, the behavior of the InteractiveMenu will be undefined.
        Returns:
        a new InteractiveMenu prefilled with menu items useful for pagination.
      • addMessageItem

        public InteractiveMenu addMessageItem​(String message,
                                              Function<MessageMenuInteraction,​reactor.core.publisher.Mono<Void>> action)
        Adds an item to this menu that is triggered when replying with a specific message.
        Parameters:
        message - the text the message must start with in order to trigger this item
        action - the action associated to this item
        Returns:
        this menu
      • addReactionItem

        public InteractiveMenu addReactionItem​(String emojiName,
                                               Function<ReactionMenuInteraction,​reactor.core.publisher.Mono<Void>> action)
        Adds an item to this menu that is triggered when adding or removing a reaction to the menu message.
        Parameters:
        emojiName - the name of the reaction emoji identifying this item. It can be a Unicode emoji character, or the name of one of the emojis in the emoji servers configured on the bot
        action - the action associated to this item
        Returns:
        this menu
      • deleteMenuOnClose

        public InteractiveMenu deleteMenuOnClose​(boolean deleteMenuOnClose)
        Sets whether to delete the menu message when the menu is closed by user.
        Parameters:
        deleteMenuOnClose - a boolean
        Returns:
        this menu
      • deleteMenuOnTimeout

        public InteractiveMenu deleteMenuOnTimeout​(boolean deleteMenuOnTimeout)
        Sets whether to delete the menu message when the menu is closed automatically by timeout
        Parameters:
        deleteMenuOnTimeout - a boolean
        Returns:
        this menu
      • closeAfterMessage

        public InteractiveMenu closeAfterMessage​(boolean closeAfterMessage)
        Sets whether to close this menu after a message item is triggered.
        Parameters:
        closeAfterMessage - a boolean
        Returns:
        this menu
      • closeAfterReaction

        public InteractiveMenu closeAfterReaction​(boolean closeAfterReaction)
        Sets whether to close this menu after a reaction item is triggered.
        Parameters:
        closeAfterReaction - a boolean
        Returns:
        this menu
      • withTimeout

        public InteractiveMenu withTimeout​(Duration timeout)
        Sets a timeou after which the menu automatically closes. The timeout starts when the menu opens, and is not reset by user interaction.
        Parameters:
        timeout - the timeout
        Returns:
        this menu
      • open

        public reactor.core.publisher.Mono<Void> open​(Context ctx)
        Opens the interactive menu, that is, sends the menu message over Discord and starts listening for user's interaction. The returned Mono completes once the menu closes or timeouts. If the menu was created using the factory method create(Mono) and the supplied Mono completes empty or with an error, the respective signals will be forwarded through the returning Mono.
        Parameters:
        ctx - the context of the command invoking this menu
        Returns:
        a Mono completing when the menu closes or timeouts. Any error happening while the menu is open will be forwarded through the Mono