Package dev.sympho.bot_utils.component
Class ButtonManager
java.lang.Object
dev.sympho.bot_utils.component.ComponentManager<ButtonInteractionEvent,ButtonManager.ButtonContext,ButtonManager.HandlerFunction,ButtonManager.Handler,ButtonManager.HandlerEntry>
dev.sympho.bot_utils.component.ButtonManager
public class ButtonManager
extends ComponentManager<ButtonInteractionEvent,ButtonManager.ButtonContext,ButtonManager.HandlerFunction,ButtonManager.Handler,ButtonManager.HandlerEntry>
Centralized manager for button interaction handling.
- Since:
- 1.0
- Version:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classThe execution context of a button being pressed.static final recordSpecification for the handling of a button.static final recordSpecification for a handler to be registered.static interfaceA function used to handle a button press event.Nested classes/interfaces inherited from class dev.sympho.bot_utils.component.ComponentManager
ComponentManager.ComponentContext<E extends @NonNull ComponentInteractionEvent> -
Field Summary
Fields inherited from class dev.sympho.bot_utils.component.ComponentManager
logger -
Constructor Summary
ConstructorsConstructorDescriptionButtonManager(GatewayDiscordClient client, AccessManager accessManager) Creates a new manager that receives interactions from the given client. -
Method Summary
Modifier and TypeMethodDescriptionprotected Class<ButtonInteractionEvent>Retrieves the interaction event type.protected ButtonManager.ButtonContextmakeContext(ButtonInteractionEvent event, AccessManager accessManager) Creates the interaction context.static StringCreates a custom ID for a button with arguments in a format that is compatible with this manager.mutex(ButtonManager.HandlerFunction handler) Converts a handler into a mutually-exclusive handler, where concurrent interactions to buttons in the same message (not necessarily the same button, but any button in the message whose handler is also a mutex in this manager) will only allow one execution (namely the first) to proceeed while the others fail, until the executing handler finishes.voidregister(ButtonManager.HandlerEntry handler) Registers an interaction handler.validateInteraction(ButtonManager.ButtonContext context, ButtonManager.Handler handler) Validates that the interaction is allowed.Methods inherited from class dev.sympho.bot_utils.component.ComponentManager
register, registerAll, start, stop, unregister
-
Constructor Details
-
ButtonManager
Creates a new manager that receives interactions from the given client.- Parameters:
client- The client to receive interactions from.accessManager- The access manager to use.
-
-
Method Details
-
makeId
Creates a custom ID for a button with arguments in a format that is compatible with this manager.- Parameters:
id- The button handler ID.args- The interaction arguments.- Returns:
- The assembled custom ID.
-
register
Description copied from class:ComponentManagerRegisters an interaction handler.- Overrides:
registerin classComponentManager<ButtonInteractionEvent,ButtonManager.ButtonContext, ButtonManager.HandlerFunction, ButtonManager.Handler, ButtonManager.HandlerEntry> - Parameters:
handler- The handler to use.
-
getEventType
Description copied from class:ComponentManagerRetrieves the interaction event type.- Specified by:
getEventTypein classComponentManager<ButtonInteractionEvent,ButtonManager.ButtonContext, ButtonManager.HandlerFunction, ButtonManager.Handler, ButtonManager.HandlerEntry> - Returns:
- The interaction event type.
-
makeContext
protected ButtonManager.ButtonContext makeContext(ButtonInteractionEvent event, AccessManager accessManager) Description copied from class:ComponentManagerCreates the interaction context.- Specified by:
makeContextin classComponentManager<ButtonInteractionEvent,ButtonManager.ButtonContext, ButtonManager.HandlerFunction, ButtonManager.Handler, ButtonManager.HandlerEntry> - Parameters:
event- The interaction event.accessManager- The access manager.- Returns:
- The created context.
-
validateInteraction
protected Mono<String> validateInteraction(ButtonManager.ButtonContext context, ButtonManager.Handler handler) Description copied from class:ComponentManagerValidates that the interaction is allowed.- Specified by:
validateInteractionin classComponentManager<ButtonInteractionEvent,ButtonManager.ButtonContext, ButtonManager.HandlerFunction, ButtonManager.Handler, ButtonManager.HandlerEntry> - Parameters:
context- The interaction context.handler- The invoked handler.- Returns:
- A mono that completes empty if the interaction is allowed, otherwise issuing an error message to be sent to the user.
-
mutex
Converts a handler into a mutually-exclusive handler, where concurrent interactions to buttons in the same message (not necessarily the same button, but any button in the message whose handler is also a mutex in this manager) will only allow one execution (namely the first) to proceeed while the others fail, until the executing handler finishes.This method should be used to protect button-based features where only one execution should be allowed at once. Preferrably it should be used in conjunction with disabling the buttons, which by itself would not be sufficient due to the lag between the start of handling and the edit being processed.
- Parameters:
handler- The handler to wrap.- Returns:
- The wrapped mutex handler.
-