# Message Components

# ❔ What are components?

Components are interactive elements like buttons or hidden elements like the ActionRow which use is for displaying the visible components. You can add them to a message and interact with users in a very convenient way. Currently, the only interactive components available at the moment are buttons. They differ in style and behaviour(link redirect) seen in the picture below:

# 💡 Sending a message with a component

Sending a component with your message is a simple as that:

TextChannel channel = ...;

new MessageBuilder()
    .setContent("Click on one of these Buttons!")
    .addComponents(
        ActionRow.of(Button.success("success", "Send a message"),
            Button.danger("danger", "Delete this message"),
            Button.secondary("secondary", "Remind me after 5 minutes")))
    .send(channel);

You simply add a High Level component like an ActionRow which is a container for displaying your components. In turn the ActionRow consist of the components you can interact with like Buttons.

This works for Select Menus as well:

TextChannel channel = ...;

new MessageBuilder()
    .setContent("Select an option of this list!")
    .addComponents(
        ActionRow.of(SelectMenu.create("options", "Click here to show the options", 1, 1,
            Arrays.asList(SelectMenuOption.create("Option One", "You selected Option One!", "Click here to select Option One"),
                SelectMenuOption.create("Option Two", "You selected Option Two!", "Click here to select Option Two"),
                SelectMenuOption.create("Option Three", "You selected Option Three!", "Click here to select Option Three")))))
    .send(channel);

# 💬 Responding to component interactions

The following code snipped shows how you can respond to the previously created example:

api.addMessageComponentCreateListener(event -> {
    MessageComponentInteraction messageComponentInteraction = event.getMessageComponentInteraction();
    String customId = messageComponentInteraction.getCustomId();

    switch (customId) {
        case "success":
            messageComponentInteraction.createImmediateResponder()
                    .setContent("You clicked a button!")
                    .respond();
            break;
        case "danger":
            messageComponentInteraction.getMessage().ifPresent(Message::delete);
            break;
        case "secondary":
            messageComponentInteraction.respondLater().thenAccept(interactionOriginalResponseUpdater -> {
                //Code to respond after 5 minutes
            });
            break;
        case "options":
            messageComponentInteraction.createImmediateResponder()
					.setContent("You selected an option in a select menu!")
					.respond();
            break;
    }
});

Note: once you receive a message component interaction you must respond with createImmediateResponder to send a message or respondLater, otherwise the interaction will fail.