Search in sources :

Example 1 with ButtonClickEvent

use of org.javacord.api.event.interaction.ButtonClickEvent in project Javacord by BtoBastian.

the class InteractionCreateHandler method handle.

@Override
public void handle(JsonNode packet) {
    TextChannel channel = null;
    if (packet.hasNonNull("channel_id")) {
        long channelId = packet.get("channel_id").asLong();
        // Check if this interaction comes from a guild or a DM
        if (packet.hasNonNull("guild_id")) {
            channel = api.getTextChannelById(channelId).orElse(null);
        } else {
            UserImpl user = new UserImpl(api, packet.get("user"), (MemberImpl) null, null);
            channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, user.getId(), user);
        }
    }
    int typeId = packet.get("type").asInt();
    final InteractionType interactionType = InteractionType.fromValue(typeId);
    ComponentType componentType = null;
    InteractionImpl interaction;
    switch(interactionType) {
        case APPLICATION_COMMAND:
            int applicationCommandTypeId = packet.get("data").get("type").asInt();
            ApplicationCommandType applicationCommandType = ApplicationCommandType.fromValue(applicationCommandTypeId);
            switch(applicationCommandType) {
                case SLASH:
                    interaction = new SlashCommandInteractionImpl(api, channel, packet);
                    break;
                case USER:
                    interaction = new UserContextMenuInteractionImpl(api, channel, packet);
                    break;
                case MESSAGE:
                    interaction = new MessageContextMenuInteractionImpl(api, channel, packet);
                    break;
                default:
                    logger.info("Got application command interaction of unknown type <{}>. " + "Please contact the developer!", applicationCommandTypeId);
                    return;
            }
            break;
        case MESSAGE_COMPONENT:
            int componentTypeId = packet.get("data").get("component_type").asInt();
            componentType = ComponentType.fromId(componentTypeId);
            switch(componentType) {
                case BUTTON:
                    interaction = new ButtonInteractionImpl(api, channel, packet);
                    break;
                case ACTION_ROW:
                    logger.warn("Received a message component interaction of type ACTION_ROW. This should not" + " be possible.");
                    return;
                case SELECT_MENU:
                    interaction = new SelectMenuInteractionImpl(api, channel, packet);
                    break;
                default:
                    logger.warn("Received message component interaction of unknown type <{}>. " + "Please contact the developer!", componentTypeId);
                    return;
            }
            break;
        case APPLICATION_COMMAND_AUTOCOMPLETE:
            interaction = new AutocompleteInteractionImpl(api, channel, packet);
            break;
        default:
            logger.warn("Received interaction of unknown type <{}>. " + "Please contact the developer!", typeId);
            return;
    }
    InteractionCreateEvent event = new InteractionCreateEventImpl(interaction);
    ServerImpl server = (ServerImpl) interaction.getServer().orElse(null);
    api.getEventDispatcher().dispatchInteractionCreateEvent(server == null ? api : server, server, interaction.getChannel().orElse(null), interaction.getUser(), event);
    switch(interactionType) {
        case APPLICATION_COMMAND:
            int applicationCommandTypeId = packet.get("data").get("type").asInt();
            ApplicationCommandType applicationCommandType = ApplicationCommandType.fromValue(applicationCommandTypeId);
            switch(applicationCommandType) {
                case SLASH:
                    SlashCommandCreateEvent slashCommandCreateEvent = new SlashCommandCreateEventImpl(interaction);
                    api.getEventDispatcher().dispatchSlashCommandCreateEvent(server == null ? api : server, server, interaction.getChannel().orElse(null), interaction.getUser(), slashCommandCreateEvent);
                    break;
                case USER:
                    UserContextMenuCommandEvent userContextMenuCommandEvent = new UserContextMenuCommandEventImpl(interaction);
                    api.getEventDispatcher().dispatchUserContextMenuCommandEvent(server, server, interaction.getChannel().orElse(null), interaction.getUser(), userContextMenuCommandEvent);
                    break;
                case MESSAGE:
                    MessageContextMenuCommandEvent messageContextMenuCommandEvent = new MessageContextMenuCommandEventImpl(interaction);
                    api.getEventDispatcher().dispatchMessageContextMenuCommandEvent(server, interaction.asMessageContextMenuInteraction().orElseThrow(AssertionError::new).getTarget().getId(), server, interaction.getChannel().orElse(null), interaction.getUser(), messageContextMenuCommandEvent);
                    break;
                default:
                    logger.info("Got application command interaction of unknown type <{}>. " + "Please contact the developer!", applicationCommandTypeId);
                    return;
            }
            break;
        case MESSAGE_COMPONENT:
            MessageComponentCreateEvent messageComponentCreateEvent = new MessageComponentCreateEventImpl(interaction);
            long messageId = messageComponentCreateEvent.getMessageComponentInteraction().getMessage().getId();
            api.getEventDispatcher().dispatchMessageComponentCreateEvent(server == null ? api : server, messageId, server, interaction.getChannel().orElse(null), interaction.getUser(), messageComponentCreateEvent);
            switch(componentType) {
                case BUTTON:
                    ButtonClickEvent buttonClickEvent = new ButtonClickEventImpl(interaction);
                    api.getEventDispatcher().dispatchButtonClickEvent(server == null ? api : server, messageId, server, interaction.getChannel().orElse(null), interaction.getUser(), buttonClickEvent);
                    break;
                case SELECT_MENU:
                    SelectMenuChooseEvent selectMenuChooseEvent = new SelectMenuChooseEventImpl(interaction);
                    api.getEventDispatcher().dispatchSelectMenuChooseEvent(server == null ? api : server, messageId, server, interaction.getChannel().orElse(null), interaction.getUser(), selectMenuChooseEvent);
                    break;
                default:
                    break;
            }
            break;
        case APPLICATION_COMMAND_AUTOCOMPLETE:
            AutocompleteCreateEvent autocompleteCreateEvent = new AutocompleteCreateEventImpl(interaction);
            api.getEventDispatcher().dispatchAutocompleteCreateEvent(server == null ? api : server, server, interaction.getChannel().orElse(null), interaction.getUser(), autocompleteCreateEvent);
            break;
        default:
            break;
    }
}
Also used : InteractionType(org.javacord.api.interaction.InteractionType) MessageComponentCreateEventImpl(org.javacord.core.event.interaction.MessageComponentCreateEventImpl) AutocompleteCreateEvent(org.javacord.api.event.interaction.AutocompleteCreateEvent) ButtonClickEvent(org.javacord.api.event.interaction.ButtonClickEvent) UserContextMenuInteractionImpl(org.javacord.core.interaction.UserContextMenuInteractionImpl) SelectMenuChooseEvent(org.javacord.api.event.interaction.SelectMenuChooseEvent) UserContextMenuCommandEvent(org.javacord.api.event.interaction.UserContextMenuCommandEvent) AutocompleteCreateEventImpl(org.javacord.core.event.interaction.AutocompleteCreateEventImpl) MessageContextMenuCommandEventImpl(org.javacord.core.event.interaction.MessageContextMenuCommandEventImpl) SlashCommandCreateEventImpl(org.javacord.core.event.interaction.SlashCommandCreateEventImpl) MessageComponentCreateEvent(org.javacord.api.event.interaction.MessageComponentCreateEvent) TextChannel(org.javacord.api.entity.channel.TextChannel) InteractionCreateEvent(org.javacord.api.event.interaction.InteractionCreateEvent) ServerImpl(org.javacord.core.entity.server.ServerImpl) UserImpl(org.javacord.core.entity.user.UserImpl) SelectMenuInteractionImpl(org.javacord.core.interaction.SelectMenuInteractionImpl) ApplicationCommandType(org.javacord.api.interaction.ApplicationCommandType) ComponentType(org.javacord.api.entity.message.component.ComponentType) MessageContextMenuInteractionImpl(org.javacord.core.interaction.MessageContextMenuInteractionImpl) InteractionCreateEventImpl(org.javacord.core.event.interaction.InteractionCreateEventImpl) AutocompleteInteractionImpl(org.javacord.core.interaction.AutocompleteInteractionImpl) MessageContextMenuCommandEvent(org.javacord.api.event.interaction.MessageContextMenuCommandEvent) SlashCommandInteractionImpl(org.javacord.core.interaction.SlashCommandInteractionImpl) SelectMenuChooseEventImpl(org.javacord.core.event.interaction.SelectMenuChooseEventImpl) ButtonInteractionImpl(org.javacord.core.interaction.ButtonInteractionImpl) UserContextMenuCommandEventImpl(org.javacord.core.event.interaction.UserContextMenuCommandEventImpl) MessageContextMenuInteractionImpl(org.javacord.core.interaction.MessageContextMenuInteractionImpl) UserContextMenuInteractionImpl(org.javacord.core.interaction.UserContextMenuInteractionImpl) SlashCommandInteractionImpl(org.javacord.core.interaction.SlashCommandInteractionImpl) InteractionImpl(org.javacord.core.interaction.InteractionImpl) SelectMenuInteractionImpl(org.javacord.core.interaction.SelectMenuInteractionImpl) ButtonInteractionImpl(org.javacord.core.interaction.ButtonInteractionImpl) AutocompleteInteractionImpl(org.javacord.core.interaction.AutocompleteInteractionImpl) ButtonClickEventImpl(org.javacord.core.event.interaction.ButtonClickEventImpl) SlashCommandCreateEvent(org.javacord.api.event.interaction.SlashCommandCreateEvent)

Aggregations

TextChannel (org.javacord.api.entity.channel.TextChannel)1 ComponentType (org.javacord.api.entity.message.component.ComponentType)1 AutocompleteCreateEvent (org.javacord.api.event.interaction.AutocompleteCreateEvent)1 ButtonClickEvent (org.javacord.api.event.interaction.ButtonClickEvent)1 InteractionCreateEvent (org.javacord.api.event.interaction.InteractionCreateEvent)1 MessageComponentCreateEvent (org.javacord.api.event.interaction.MessageComponentCreateEvent)1 MessageContextMenuCommandEvent (org.javacord.api.event.interaction.MessageContextMenuCommandEvent)1 SelectMenuChooseEvent (org.javacord.api.event.interaction.SelectMenuChooseEvent)1 SlashCommandCreateEvent (org.javacord.api.event.interaction.SlashCommandCreateEvent)1 UserContextMenuCommandEvent (org.javacord.api.event.interaction.UserContextMenuCommandEvent)1 ApplicationCommandType (org.javacord.api.interaction.ApplicationCommandType)1 InteractionType (org.javacord.api.interaction.InteractionType)1 ServerImpl (org.javacord.core.entity.server.ServerImpl)1 UserImpl (org.javacord.core.entity.user.UserImpl)1 AutocompleteCreateEventImpl (org.javacord.core.event.interaction.AutocompleteCreateEventImpl)1 ButtonClickEventImpl (org.javacord.core.event.interaction.ButtonClickEventImpl)1 InteractionCreateEventImpl (org.javacord.core.event.interaction.InteractionCreateEventImpl)1 MessageComponentCreateEventImpl (org.javacord.core.event.interaction.MessageComponentCreateEventImpl)1 MessageContextMenuCommandEventImpl (org.javacord.core.event.interaction.MessageContextMenuCommandEventImpl)1 SelectMenuChooseEventImpl (org.javacord.core.event.interaction.SelectMenuChooseEventImpl)1