Search in sources :

Example 1 with MessageChannelMixin

use of net.dv8tion.jda.internal.entities.mixin.channel.middleman.MessageChannelMixin in project JDA by DV8FromTheWorld.

the class MessageCreateHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    MessageType type = MessageType.fromId(content.getInt("type"));
    if (type == MessageType.UNKNOWN) {
        WebSocketClient.LOG.debug("JDA received a message of unknown type. Type: {}  JSON: {}", type, content);
        return null;
    }
    // Drop ephemeral messages since they are broken due to missing guild_id
    if ((content.getInt("flags", 0) & 64) != 0)
        return null;
    JDAImpl jda = getJDA();
    Guild guild = null;
    if (!content.isNull("guild_id")) {
        long guildId = content.getLong("guild_id");
        if (jda.getGuildSetupController().isLocked(guildId))
            return guildId;
        guild = api.getGuildById(guildId);
        if (guild == null) {
            api.getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
            EventCache.LOG.debug("Received message for a guild that JDA does not currently have cached");
            return null;
        }
    }
    Message message;
    try {
        message = jda.getEntityBuilder().createMessageWithLookup(content, guild, true);
    } catch (IllegalArgumentException e) {
        switch(e.getMessage()) {
            case EntityBuilder.MISSING_CHANNEL:
                {
                    final long channelId = content.getLong("channel_id");
                    jda.getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle);
                    EventCache.LOG.debug("Received a message for a channel that JDA does not currently have cached");
                    return null;
                }
            case EntityBuilder.MISSING_USER:
                {
                    final long authorId = content.getObject("author").getLong("id");
                    jda.getEventCache().cache(EventCache.Type.USER, authorId, responseNumber, allContent, this::handle);
                    EventCache.LOG.debug("Received a message for a user that JDA does not currently have cached");
                    return null;
                }
            case EntityBuilder.UNKNOWN_MESSAGE_TYPE:
                {
                    WebSocketClient.LOG.debug("Ignoring message with unknown type: {}", content);
                    return null;
                }
            default:
                throw e;
        }
    }
    MessageChannel channel = message.getChannel();
    ChannelType channelType = channel.getType();
    // Update the variable that tracks the latest message received in the channel
    ((MessageChannelMixin<?>) channel).setLatestMessageIdLong(message.getIdLong());
    if (channelType.isGuild()) {
        if (channelType.isThread()) {
            ThreadChannelImpl gThread = (ThreadChannelImpl) channel;
            // Discord will only ever allow this property to show up to 50,
            // so we don't want to update it to be over 50 because we don't want users to use it incorrectly.
            int newMessageCount = Math.min(gThread.getMessageCount() + 1, 50);
            gThread.setMessageCount(newMessageCount);
        }
    } else {
        api.usedPrivateChannel(channel.getIdLong());
    }
    jda.handleEvent(new MessageReceivedEvent(jda, responseNumber, message));
    return null;
}
Also used : MessageChannelMixin(net.dv8tion.jda.internal.entities.mixin.channel.middleman.MessageChannelMixin) ThreadChannelImpl(net.dv8tion.jda.internal.entities.ThreadChannelImpl) JDAImpl(net.dv8tion.jda.internal.JDAImpl) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent)

Aggregations

MessageReceivedEvent (net.dv8tion.jda.api.events.message.MessageReceivedEvent)1 JDAImpl (net.dv8tion.jda.internal.JDAImpl)1 ThreadChannelImpl (net.dv8tion.jda.internal.entities.ThreadChannelImpl)1 MessageChannelMixin (net.dv8tion.jda.internal.entities.mixin.channel.middleman.MessageChannelMixin)1