Search in sources :

Example 1 with UserImpl

use of net.dv8tion.jda.internal.entities.UserImpl 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 2 with UserImpl

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

the class JDAImpl method openPrivateChannelById.

@Nonnull
@Override
public RestAction<PrivateChannel> openPrivateChannelById(long userId) {
    if (selfUser != null && userId == selfUser.getIdLong())
        throw new UnsupportedOperationException("Cannot open private channel with yourself!");
    return new DeferredRestAction<>(this, PrivateChannel.class, () -> {
        User user = getUserById(userId);
        if (user instanceof UserImpl)
            return ((UserImpl) user).getPrivateChannel();
        return null;
    }, () -> {
        Route.CompiledRoute route = Route.Self.CREATE_PRIVATE_CHANNEL.compile();
        DataObject body = DataObject.empty().put("recipient_id", userId);
        return new RestActionImpl<>(this, route, body, (response, request) -> getEntityBuilder().createPrivateChannel(response.getObject()));
    });
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) UserImpl(net.dv8tion.jda.internal.entities.UserImpl) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)2 DataObject (net.dv8tion.jda.api.utils.data.DataObject)2 UserImpl (net.dv8tion.jda.internal.entities.UserImpl)2 TLongObjectMap (gnu.trove.map.TLongObjectMap)1 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 ISnowflake (net.dv8tion.jda.api.entities.ISnowflake)1 Message (net.dv8tion.jda.api.entities.Message)1 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)1 Command (net.dv8tion.jda.api.interactions.commands.Command)1 CommandInteractionPayload (net.dv8tion.jda.api.interactions.commands.CommandInteractionPayload)1 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)1 OptionType (net.dv8tion.jda.api.interactions.commands.OptionType)1 DataArray (net.dv8tion.jda.api.utils.data.DataArray)1 JDAImpl (net.dv8tion.jda.internal.JDAImpl)1 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)1 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)1 MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)1