Search in sources :

Example 1 with TextChannel

use of org.javacord.api.entity.channel.TextChannel in project Javacord by BtoBastian.

the class MessageBuilderBaseDelegateImpl method checkForAttachmentsAndExecuteRequest.

// //////////////////////////////////////////////////////////////////////////////
// Internal MessageBuilder utility methods
// //////////////////////////////////////////////////////////////////////////////
private CompletableFuture<Message> checkForAttachmentsAndExecuteRequest(TextChannel channel, ObjectNode body, RestRequest<Message> request, boolean clearAttachmentsIfAppropriate) {
    if (attachments.isEmpty() && embeds.stream().noneMatch(EmbedBuilder::requiresAttachments)) {
        if (clearAttachmentsIfAppropriate) {
            body.set("attachments", JsonNodeFactory.instance.objectNode().arrayNode());
        }
        return executeRequestWithoutAttachments(channel, body, request);
    }
    CompletableFuture<Message> future = new CompletableFuture<>();
    // We access files etc. so this should be async
    channel.getApi().getThreadPool().getExecutorService().submit(() -> {
        try {
            List<FileContainer> tempAttachments = new ArrayList<>(attachments);
            // Add the attachments required for the embeds
            for (EmbedBuilder embed : embeds) {
                tempAttachments.addAll(((EmbedBuilderDelegateImpl) embed.getDelegate()).getRequiredAttachments());
            }
            addMultipartBodyToRequest(request, body, tempAttachments, channel.getApi());
            request.execute(result -> ((DiscordApiImpl) channel.getApi()).getOrCreateMessage(channel, result.getJsonBody())).whenComplete((newMessage, throwable) -> {
                if (throwable != null) {
                    future.completeExceptionally(throwable);
                } else {
                    future.complete(newMessage);
                }
            });
        } catch (Throwable t) {
            future.completeExceptionally(t);
        }
    });
    return future;
}
Also used : MessageAttachment(org.javacord.api.entity.message.MessageAttachment) Arrays(java.util.Arrays) Member(org.javacord.core.entity.user.Member) URL(java.net.URL) RestMethod(org.javacord.core.util.rest.RestMethod) FileContainer(org.javacord.core.util.FileContainer) HighLevelComponent(org.javacord.api.entity.message.component.HighLevelComponent) RestRequest(org.javacord.core.util.rest.RestRequest) EmbedBuilderDelegateImpl(org.javacord.core.entity.message.embed.EmbedBuilderDelegateImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ComponentType(org.javacord.api.entity.message.component.ComponentType) MediaType(okhttp3.MediaType) ActionRowBuilder(org.javacord.api.entity.message.component.ActionRowBuilder) MessageBuilderBaseDelegate(org.javacord.api.entity.message.internal.MessageBuilderBaseDelegate) BufferedImage(java.awt.image.BufferedImage) Messageable(org.javacord.api.entity.message.Messageable) Collection(java.util.Collection) Set(java.util.Set) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) AllowedMentions(org.javacord.api.entity.message.mention.AllowedMentions) Message(org.javacord.api.entity.message.Message) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RestEndpoint(org.javacord.core.util.rest.RestEndpoint) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MultipartBody(okhttp3.MultipartBody) Mentionable(org.javacord.api.entity.Mentionable) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) AllowedMentionsImpl(org.javacord.core.entity.message.mention.AllowedMentionsImpl) CompletableFuture(java.util.concurrent.CompletableFuture) ComponentImpl(org.javacord.core.entity.message.component.ComponentImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) IncomingWebhook(org.javacord.api.entity.webhook.IncomingWebhook) RequestBody(okhttp3.RequestBody) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MessageDecoration(org.javacord.api.entity.message.MessageDecoration) URLConnection(java.net.URLConnection) Icon(org.javacord.api.entity.Icon) TextChannel(org.javacord.api.entity.channel.TextChannel) DiscordApiImpl(org.javacord.core.DiscordApiImpl) ActionRow(org.javacord.api.entity.message.component.ActionRow) LowLevelComponent(org.javacord.api.entity.message.component.LowLevelComponent) File(java.io.File) LoggerUtil(org.javacord.core.util.logging.LoggerUtil) User(org.javacord.api.entity.user.User) DiscordApi(org.javacord.api.DiscordApi) Collections(java.util.Collections) InputStream(java.io.InputStream) CompletableFuture(java.util.concurrent.CompletableFuture) EmbedBuilder(org.javacord.api.entity.message.embed.EmbedBuilder) Message(org.javacord.api.entity.message.Message) DiscordApiImpl(org.javacord.core.DiscordApiImpl) ArrayList(java.util.ArrayList) FileContainer(org.javacord.core.util.FileContainer)

Example 2 with TextChannel

use of org.javacord.api.entity.channel.TextChannel in project Javacord by BtoBastian.

the class MessageReactionAddHandler method handle.

@Override
public void handle(JsonNode packet) {
    long channelId = packet.get("channel_id").asLong();
    long messageId = packet.get("message_id").asLong();
    long userId = packet.get("user_id").asLong();
    String serverId = packet.hasNonNull("guild_id") ? packet.get("guild_id").asText() : null;
    TextChannel channel;
    if (serverId == null) {
        // if private channel:
        channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
    } else {
        channel = api.getTextChannelById(channelId).orElse(null);
    }
    if (channel == null) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    Optional<Server> server = api.getServerById(serverId);
    Member member = null;
    if (packet.hasNonNull("member") && server.isPresent()) {
        member = new MemberImpl(api, (ServerImpl) server.get(), packet.get("member"), null);
    }
    Optional<Message> message = api.getCachedMessageById(messageId);
    Emoji emoji;
    JsonNode emojiJson = packet.get("emoji");
    if (!emojiJson.has("id") || emojiJson.get("id").isNull()) {
        emoji = UnicodeEmojiImpl.fromString(emojiJson.get("name").asText());
    } else {
        emoji = api.getKnownCustomEmojiOrCreateCustomEmoji(emojiJson);
    }
    message.ifPresent(msg -> ((MessageImpl) msg).addReaction(emoji, userId == api.getYourself().getId()));
    ReactionAddEvent event = new ReactionAddEventImpl(api, messageId, channel, emoji, userId, member);
    api.getEventDispatcher().dispatchReactionAddEvent(server.map(DispatchQueueSelector.class::cast).orElse(api), messageId, server.orElse(null), channel, userId, event);
}
Also used : ReactionAddEventImpl(org.javacord.core.event.message.reaction.ReactionAddEventImpl) Server(org.javacord.api.entity.server.Server) Message(org.javacord.api.entity.message.Message) MemberImpl(org.javacord.core.entity.user.MemberImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ReactionAddEvent(org.javacord.api.event.message.reaction.ReactionAddEvent) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) TextChannel(org.javacord.api.entity.channel.TextChannel) ServerImpl(org.javacord.core.entity.server.ServerImpl) Emoji(org.javacord.api.entity.emoji.Emoji) Member(org.javacord.core.entity.user.Member)

Example 3 with TextChannel

use of org.javacord.api.entity.channel.TextChannel in project Javacord by BtoBastian.

the class MessageReactionRemoveHandler method handle.

@Override
public void handle(JsonNode packet) {
    long messageId = packet.get("message_id").asLong();
    long userId = packet.get("user_id").asLong();
    Optional<Message> message = api.getCachedMessageById(messageId);
    long channelId = packet.get("channel_id").asLong();
    TextChannel channel;
    if (packet.hasNonNull("guild_id")) {
        channel = api.getTextChannelById(channelId).orElse(null);
    } else {
        // if private channel:
        channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
    }
    if (channel == null) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    Emoji emoji;
    JsonNode emojiJson = packet.get("emoji");
    if (!emojiJson.has("id") || emojiJson.get("id").isNull()) {
        emoji = UnicodeEmojiImpl.fromString(emojiJson.get("name").asText());
    } else {
        emoji = api.getKnownCustomEmojiOrCreateCustomEmoji(emojiJson);
    }
    message.ifPresent(msg -> ((MessageImpl) msg).removeReaction(emoji, userId == api.getYourself().getId()));
    ReactionRemoveEvent event = new ReactionRemoveEventImpl(api, messageId, channel, emoji, userId);
    Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
    api.getEventDispatcher().dispatchReactionRemoveEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, userId, event);
}
Also used : ReactionRemoveEvent(org.javacord.api.event.message.reaction.ReactionRemoveEvent) TextChannel(org.javacord.api.entity.channel.TextChannel) Message(org.javacord.api.entity.message.Message) Server(org.javacord.api.entity.server.Server) ReactionRemoveEventImpl(org.javacord.core.event.message.reaction.ReactionRemoveEventImpl) Emoji(org.javacord.api.entity.emoji.Emoji) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerChannel(org.javacord.api.entity.channel.ServerChannel) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Example 4 with TextChannel

use of org.javacord.api.entity.channel.TextChannel 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)

Example 5 with TextChannel

use of org.javacord.api.entity.channel.TextChannel in project Javacord by BtoBastian.

the class MessageDeleteBulkHandler method handle.

@Override
public void handle(JsonNode packet) {
    long channelId = Long.parseLong(packet.get("channel_id").asText());
    Optional<TextChannel> optionalChannel = api.getTextChannelById(channelId);
    if (optionalChannel.isPresent()) {
        TextChannel channel = optionalChannel.get();
        for (JsonNode messageIdJson : packet.get("ids")) {
            long messageId = messageIdJson.asLong();
            MessageDeleteEvent event = new MessageDeleteEventImpl(api, messageId, channel);
            api.getCachedMessageById(messageId).ifPresent(((MessageCacheImpl) channel.getMessageCache())::removeMessage);
            api.removeMessageFromCache(messageId);
            Optional<Server> optionalServer = channel.asServerChannel().map(ServerChannel::getServer);
            api.getEventDispatcher().dispatchMessageDeleteEvent(optionalServer.map(DispatchQueueSelector.class::cast).orElse(api), messageId, optionalServer.orElse(null), channel, event);
        }
    } else {
        LoggerUtil.logMissingChannel(logger, channelId);
    }
}
Also used : TextChannel(org.javacord.api.entity.channel.TextChannel) Server(org.javacord.api.entity.server.Server) JsonNode(com.fasterxml.jackson.databind.JsonNode) MessageDeleteEvent(org.javacord.api.event.message.MessageDeleteEvent) ServerChannel(org.javacord.api.entity.channel.ServerChannel) MessageDeleteEventImpl(org.javacord.core.event.message.MessageDeleteEventImpl) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector)

Aggregations

TextChannel (org.javacord.api.entity.channel.TextChannel)12 Server (org.javacord.api.entity.server.Server)7 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ServerChannel (org.javacord.api.entity.channel.ServerChannel)6 Message (org.javacord.api.entity.message.Message)5 ServerImpl (org.javacord.core.entity.server.ServerImpl)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Emoji (org.javacord.api.entity.emoji.Emoji)2 ComponentType (org.javacord.api.entity.message.component.ComponentType)2 DiscordApiImpl (org.javacord.core.DiscordApiImpl)2 Member (org.javacord.core.entity.user.Member)2 MemberImpl (org.javacord.core.entity.user.MemberImpl)2 RestEndpoint (org.javacord.core.util.rest.RestEndpoint)2 RestMethod (org.javacord.core.util.rest.RestMethod)2