Search in sources :

Example 1 with LowLevelComponent

use of org.javacord.api.entity.message.component.LowLevelComponent in project Javacord by BtoBastian.

the class ActionRowImpl method toJsonNode.

/**
 * Gets the ActionRow as a {@link ObjectNode}. This is what is sent to Discord.
 *
 * @param object The object, the data should be added to.
 * @return The button as a ObjectNode.
 * @throws IllegalStateException if the ActionRowBuilder has an ActionRow component
 */
public ObjectNode toJsonNode(ObjectNode object) throws IllegalStateException {
    object.put("type", ComponentType.ACTION_ROW.value());
    if (components.isEmpty()) {
        object.putArray("components");
        return object;
    }
    ArrayNode componentsJson = JsonNodeFactory.instance.objectNode().arrayNode();
    for (LowLevelComponent component : this.components) {
        switch(component.getType()) {
            case ACTION_ROW:
                throw new IllegalStateException("An action row can not contain an action row.");
            case BUTTON:
                ButtonImpl button = (ButtonImpl) component;
                componentsJson.add(button.toJsonNode());
                break;
            case SELECT_MENU:
                SelectMenuImpl selectMenu = (SelectMenuImpl) component;
                componentsJson.add(selectMenu.toJsonNode());
                break;
            default:
                throw new IllegalStateException("An unknown component type was added.");
        }
    }
    object.set("components", componentsJson);
    return object;
}
Also used : LowLevelComponent(org.javacord.api.entity.message.component.LowLevelComponent) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 LowLevelComponent (org.javacord.api.entity.message.component.LowLevelComponent)1