Search in sources :

Example 1 with EntityBuilder

use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.

the class TypingStartHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    GuildImpl guild = null;
    if (!content.isNull("guild_id")) {
        long guildId = content.getUnsignedLong("guild_id");
        guild = (GuildImpl) getJDA().getGuildById(guildId);
        if (getJDA().getGuildSetupController().isLocked(guildId))
            return guildId;
        else if (guild == null)
            // Don't cache typing events
            return null;
    }
    final long channelId = content.getLong("channel_id");
    // TODO-v5-unified-channel-cache
    MessageChannel channel = getJDA().getTextChannelsView().get(channelId);
    if (channel == null)
        channel = getJDA().getNewsChannelView().get(channelId);
    if (channel == null)
        channel = getJDA().getPrivateChannelsView().get(channelId);
    if (channel == null)
        // We don't have the channel cached yet. We chose not to cache this event
        return null;
    // because that happen very often and could easily fill up the EventCache if
    // we, for some reason, never get the channel. Especially in an active channel.
    final long userId = content.getLong("user_id");
    User user;
    MemberImpl member = null;
    if (channel instanceof PrivateChannel)
        user = ((PrivateChannel) channel).getUser();
    else
        user = getJDA().getUsersView().get(userId);
    if (!content.isNull("member")) {
        // Try to load member for the typing event
        EntityBuilder entityBuilder = getJDA().getEntityBuilder();
        member = entityBuilder.createMember(guild, content.getObject("member"));
        entityBuilder.updateMemberCache(member);
        user = member.getUser();
    }
    if (user == null)
        // Just like in the comment above, if for some reason we don't have the user
        return null;
    // then we will just throw the event away.
    OffsetDateTime timestamp = Instant.ofEpochSecond(content.getInt("timestamp")).atOffset(ZoneOffset.UTC);
    getJDA().handleEvent(new UserTypingEvent(getJDA(), responseNumber, user, channel, timestamp, member));
    return null;
}
Also used : PrivateChannel(net.dv8tion.jda.api.entities.PrivateChannel) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) UserTypingEvent(net.dv8tion.jda.api.events.user.UserTypingEvent) User(net.dv8tion.jda.api.entities.User) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) OffsetDateTime(java.time.OffsetDateTime) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder)

Example 2 with EntityBuilder

use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.

the class CommandInteractionPayloadImpl method parseResolved.

private void parseResolved(JDAImpl jda, DataObject resolveJson) {
    EntityBuilder entityBuilder = jda.getEntityBuilder();
    resolveJson.optObject("users").ifPresent(users -> users.keys().forEach(userId -> {
        DataObject userJson = users.getObject(userId);
        UserImpl userArg = entityBuilder.createUser(userJson);
        resolved.put(userArg.getIdLong(), userArg);
    }));
    resolveJson.optObject("attachments").ifPresent(attachments -> attachments.keys().forEach(id -> {
        DataObject json = attachments.getObject(id);
        Message.Attachment file = entityBuilder.createMessageAttachment(json);
        resolved.put(file.getIdLong(), file);
    }));
    if (// Technically these can function in DMs too ...
    guild != null) {
        resolveJson.optObject("members").ifPresent(members -> members.keys().forEach(memberId -> {
            DataObject userJson = resolveJson.getObject("users").getObject(memberId);
            DataObject memberJson = members.getObject(memberId);
            memberJson.put("user", userJson);
            MemberImpl optionMember = entityBuilder.createMember((GuildImpl) guild, memberJson);
            entityBuilder.updateMemberCache(optionMember);
            // This basically upgrades user to member
            resolved.put(optionMember.getIdLong(), optionMember);
        }));
        resolveJson.optObject("roles").ifPresent(roles -> roles.keys().stream().map(guild::getRoleById).filter(Objects::nonNull).forEach(role -> resolved.put(role.getIdLong(), role)));
        resolveJson.optObject("channels").ifPresent(channels -> {
            channels.keys().forEach(id -> {
                ISnowflake channelObj = jda.getGuildChannelById(id);
                if (channelObj != null)
                    resolved.put(channelObj.getIdLong(), channelObj);
            });
        });
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) DataArray(net.dv8tion.jda.api.utils.data.DataArray) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) UserImpl(net.dv8tion.jda.internal.entities.UserImpl) ISnowflake(net.dv8tion.jda.api.entities.ISnowflake) InteractionImpl(net.dv8tion.jda.internal.interactions.InteractionImpl) Command(net.dv8tion.jda.api.interactions.commands.Command) ArrayList(java.util.ArrayList) CommandInteractionPayload(net.dv8tion.jda.api.interactions.commands.CommandInteractionPayload) Objects(java.util.Objects) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) List(java.util.List) OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) JDAImpl(net.dv8tion.jda.internal.JDAImpl) TLongObjectMap(gnu.trove.map.TLongObjectMap) DataObject(net.dv8tion.jda.api.utils.data.DataObject) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) Nonnull(javax.annotation.Nonnull) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) DataObject(net.dv8tion.jda.api.utils.data.DataObject) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) UserImpl(net.dv8tion.jda.internal.entities.UserImpl) Objects(java.util.Objects) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) ISnowflake(net.dv8tion.jda.api.entities.ISnowflake)

Example 3 with EntityBuilder

use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.

the class ThreadListSyncHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    long guildId = content.getLong("guild_id");
    if (api.getGuildSetupController().isLocked(guildId))
        return guildId;
    EntityBuilder entityBuilder = api.getEntityBuilder();
    DataArray threadsArrayJson = content.getArray("threads");
    for (int i = 0; i < threadsArrayJson.length(); i++) {
        DataObject threadJson = threadsArrayJson.getObject(i);
        ThreadChannel thread = entityBuilder.createThreadChannel(threadJson, guildId);
        api.handleEvent(new ThreadRevealedEvent(api, responseNumber, thread));
    }
    return null;
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) ThreadRevealedEvent(net.dv8tion.jda.api.events.thread.ThreadRevealedEvent) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) ThreadChannel(net.dv8tion.jda.api.entities.ThreadChannel) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Example 4 with EntityBuilder

use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.

the class ThreadMembersUpdateHandler method handleAddedThreadMembers.

private void handleAddedThreadMembers(ThreadChannelImpl thread, DataArray addedMembersJson) {
    EntityBuilder entityBuilder = api.getEntityBuilder();
    CacheView.SimpleCacheView<ThreadMember> view = thread.getThreadMemberView();
    List<ThreadMember> addedThreadMembers = new ArrayList<>();
    for (int i = 0; i < addedMembersJson.length(); i++) {
        DataObject threadMemberJson = addedMembersJson.getObject(i);
        ThreadMember threadMember = entityBuilder.createThreadMember((GuildImpl) thread.getGuild(), thread, threadMemberJson);
        addedThreadMembers.add(threadMember);
    }
    // want to cache ThreadMembers if the Members they're based on aren't being cached.
    try (UnlockHook lock = view.writeLock()) {
        for (ThreadMember threadMember : addedThreadMembers) {
            view.getMap().put(threadMember.getIdLong(), threadMember);
        }
    }
    // Emit the events from outside the writeLock
    for (ThreadMember threadMember : addedThreadMembers) {
        api.handleEvent(new ThreadMemberJoinEvent(api, responseNumber, thread, threadMember));
    }
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) CacheView(net.dv8tion.jda.api.utils.cache.CacheView) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook) ArrayList(java.util.ArrayList) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) ThreadMember(net.dv8tion.jda.api.entities.ThreadMember) ThreadMemberJoinEvent(net.dv8tion.jda.api.events.thread.member.ThreadMemberJoinEvent)

Example 5 with EntityBuilder

use of net.dv8tion.jda.internal.entities.EntityBuilder in project JDA by DV8FromTheWorld.

the class BaseGuildMessageChannelMixin method retrieveWebhooks.

@Nonnull
@Override
default RestAction<List<Webhook>> retrieveWebhooks() {
    checkPermission(Permission.MANAGE_WEBHOOKS);
    Route.CompiledRoute route = Route.Channels.GET_WEBHOOKS.compile(getId());
    JDAImpl jda = (JDAImpl) getJDA();
    return new RestActionImpl<>(jda, route, (response, request) -> {
        DataArray array = response.getArray();
        List<Webhook> webhooks = new ArrayList<>(array.length());
        EntityBuilder builder = jda.getEntityBuilder();
        for (int i = 0; i < array.length(); i++) {
            try {
                webhooks.add(builder.createWebhook(array.getObject(i)));
            } catch (UncheckedIOException | NullPointerException e) {
                JDAImpl.LOG.error("Error while creating websocket from json", e);
            }
        }
        return Collections.unmodifiableList(webhooks);
    });
}
Also used : ArrayList(java.util.ArrayList) JDAImpl(net.dv8tion.jda.internal.JDAImpl) UncheckedIOException(java.io.UncheckedIOException) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) AuditableRestActionImpl(net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl) Webhook(net.dv8tion.jda.api.entities.Webhook) Route(net.dv8tion.jda.internal.requests.Route) Nonnull(javax.annotation.Nonnull)

Aggregations

EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)19 DataArray (net.dv8tion.jda.api.utils.data.DataArray)15 DataObject (net.dv8tion.jda.api.utils.data.DataObject)9 ArrayList (java.util.ArrayList)7 Nonnull (javax.annotation.Nonnull)7 JDAImpl (net.dv8tion.jda.internal.JDAImpl)7 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)5 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)5 Route (net.dv8tion.jda.internal.requests.Route)5 ParsingException (net.dv8tion.jda.api.exceptions.ParsingException)4 CheckReturnValue (javax.annotation.CheckReturnValue)3 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)3 MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)3 TLongObjectMap (gnu.trove.map.TLongObjectMap)2 LinkedList (java.util.LinkedList)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 ThreadChannel (net.dv8tion.jda.api.entities.ThreadChannel)2 User (net.dv8tion.jda.api.entities.User)2 InsufficientPermissionException (net.dv8tion.jda.api.exceptions.InsufficientPermissionException)2 MissingAccessException (net.dv8tion.jda.api.exceptions.MissingAccessException)2