Search in sources :

Example 1 with MemberImpl

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

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

the class VoiceStateUpdateHandler method handleGuildVoiceState.

private void handleGuildVoiceState(DataObject content) {
    final long userId = content.getLong("user_id");
    final long guildId = content.getLong("guild_id");
    final Long channelId = !content.isNull("channel_id") ? content.getLong("channel_id") : null;
    final String sessionId = !content.isNull("session_id") ? content.getString("session_id") : null;
    boolean selfMuted = content.getBoolean("self_mute");
    boolean selfDeafened = content.getBoolean("self_deaf");
    boolean guildMuted = content.getBoolean("mute");
    boolean guildDeafened = content.getBoolean("deaf");
    boolean suppressed = content.getBoolean("suppress");
    boolean stream = content.getBoolean("self_stream");
    boolean video = content.getBoolean("self_video", false);
    String requestToSpeak = content.getString("request_to_speak_timestamp", null);
    OffsetDateTime requestToSpeakTime = null;
    long requestToSpeakTimestamp = 0L;
    if (requestToSpeak != null) {
        requestToSpeakTime = OffsetDateTime.parse(requestToSpeak);
        requestToSpeakTimestamp = requestToSpeakTime.toInstant().toEpochMilli();
    }
    Guild guild = getJDA().getGuildById(guildId);
    if (guild == null) {
        getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received a VOICE_STATE_UPDATE for a Guild that has yet to be cached. JSON: {}", content);
        return;
    }
    AudioChannel channel = null;
    if (channelId != null) {
        channel = (AudioChannel) guild.getGuildChannelById(channelId);
    }
    if (channel == null && (channelId != null)) {
        getJDA().getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Received VOICE_STATE_UPDATE for an AudioChannel that has yet to be cached. JSON: {}", content);
        return;
    }
    DataObject memberJson = content.getObject("member");
    MemberImpl member = getJDA().getEntityBuilder().createMember((GuildImpl) guild, memberJson);
    if (member == null)
        return;
    GuildVoiceStateImpl vState = (GuildVoiceStateImpl) member.getVoiceState();
    if (vState == null)
        return;
    // Cant really see a reason for an event for this
    vState.setSessionId(sessionId);
    VoiceDispatchInterceptor voiceInterceptor = getJDA().getVoiceInterceptor();
    boolean isSelf = guild.getSelfMember().equals(member);
    boolean wasMute = vState.isMuted();
    boolean wasDeaf = vState.isDeafened();
    if (selfMuted != vState.isSelfMuted()) {
        vState.setSelfMuted(selfMuted);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceSelfMuteEvent(getJDA(), responseNumber, member));
    }
    if (selfDeafened != vState.isSelfDeafened()) {
        vState.setSelfDeafened(selfDeafened);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceSelfDeafenEvent(getJDA(), responseNumber, member));
    }
    if (guildMuted != vState.isGuildMuted()) {
        vState.setGuildMuted(guildMuted);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceGuildMuteEvent(getJDA(), responseNumber, member));
    }
    if (guildDeafened != vState.isGuildDeafened()) {
        vState.setGuildDeafened(guildDeafened);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceGuildDeafenEvent(getJDA(), responseNumber, member));
    }
    if (suppressed != vState.isSuppressed()) {
        vState.setSuppressed(suppressed);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceSuppressEvent(getJDA(), responseNumber, member));
    }
    if (stream != vState.isStream()) {
        vState.setStream(stream);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceStreamEvent(getJDA(), responseNumber, member, stream));
    }
    if (video != vState.isSendingVideo()) {
        vState.setVideo(video);
        getJDA().getEntityBuilder().updateMemberCache(member);
        getJDA().handleEvent(new GuildVoiceVideoEvent(getJDA(), responseNumber, member, video));
    }
    if (wasMute != vState.isMuted())
        getJDA().handleEvent(new GuildVoiceMuteEvent(getJDA(), responseNumber, member));
    if (wasDeaf != vState.isDeafened())
        getJDA().handleEvent(new GuildVoiceDeafenEvent(getJDA(), responseNumber, member));
    if (requestToSpeakTimestamp != vState.getRequestToSpeak()) {
        OffsetDateTime oldRequestToSpeak = vState.getRequestToSpeakTimestamp();
        vState.setRequestToSpeak(requestToSpeakTime);
        getJDA().handleEvent(new GuildVoiceRequestToSpeakEvent(getJDA(), responseNumber, member, oldRequestToSpeak, requestToSpeakTime));
    }
    if (!Objects.equals(channel, vState.getChannel())) {
        AudioChannel oldChannel = vState.getChannel();
        vState.setConnectedChannel(channel);
        if (oldChannel == null) {
            ((AudioChannelMixin<?>) channel).getConnectedMembersMap().put(userId, member);
            getJDA().getEntityBuilder().updateMemberCache(member);
            getJDA().handleEvent(new GuildVoiceJoinEvent(getJDA(), responseNumber, member));
        } else if (channel == null) {
            ((AudioChannelMixin<?>) oldChannel).getConnectedMembersMap().remove(userId);
            if (isSelf)
                getJDA().getDirectAudioController().update(guild, null);
            getJDA().getEntityBuilder().updateMemberCache(member, memberJson.isNull("joined_at"));
            getJDA().handleEvent(new GuildVoiceLeaveEvent(getJDA(), responseNumber, member, oldChannel));
        } else {
            AudioManagerImpl mng = (AudioManagerImpl) getJDA().getAudioManagersView().get(guildId);
            // If the currently connected account is the one that is being moved
            if (isSelf && mng != null && voiceInterceptor == null) {
                // then change the channel we expect to be connected to.
                if (mng.isConnected())
                    mng.setConnectedChannel(channel);
                // (handled by updateAudioConnection)
                if (mng.isConnected())
                    getJDA().getDirectAudioController().update(guild, channel);
            // If we are not already connected this will be removed by VOICE_SERVER_UPDATE
            }
            ((AudioChannelMixin<?>) channel).getConnectedMembersMap().put(userId, member);
            ((AudioChannelMixin<?>) oldChannel).getConnectedMembersMap().remove(userId);
            getJDA().getEntityBuilder().updateMemberCache(member);
            getJDA().handleEvent(new GuildVoiceMoveEvent(getJDA(), responseNumber, member, oldChannel));
        }
    }
    if (isSelf && voiceInterceptor != null) {
        if (voiceInterceptor.onVoiceStateUpdate(new VoiceDispatchInterceptor.VoiceStateUpdate(channel, vState, allContent)))
            getJDA().getDirectAudioController().update(guild, channel);
    }
    ((GuildImpl) guild).updateRequestToSpeak();
}
Also used : Guild(net.dv8tion.jda.api.entities.Guild) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) AudioManagerImpl(net.dv8tion.jda.internal.managers.AudioManagerImpl) AudioChannel(net.dv8tion.jda.api.entities.AudioChannel) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl) AudioChannelMixin(net.dv8tion.jda.internal.entities.mixin.channel.middleman.AudioChannelMixin) VoiceDispatchInterceptor(net.dv8tion.jda.api.hooks.VoiceDispatchInterceptor) DataObject(net.dv8tion.jda.api.utils.data.DataObject) OffsetDateTime(java.time.OffsetDateTime) GuildVoiceStateImpl(net.dv8tion.jda.internal.entities.GuildVoiceStateImpl)

Example 3 with MemberImpl

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

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

the class GuildMemberAddHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long id = content.getLong("guild_id");
    boolean setup = getJDA().getGuildSetupController().onAddMember(id, content);
    if (setup)
        return null;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(id);
    if (guild == null) {
        getJDA().getEventCache().cache(EventCache.Type.GUILD, id, responseNumber, allContent, this::handle);
        EventCache.LOG.debug("Caching member for guild that is not yet cached. Guild ID: {} JSON: {}", id, content);
        return null;
    }
    // Update memberCount
    guild.onMemberAdd();
    MemberImpl member = getJDA().getEntityBuilder().createMember(guild, content);
    getJDA().getEntityBuilder().updateMemberCache(member);
    getJDA().handleEvent(new GuildMemberJoinEvent(getJDA(), responseNumber, member));
    return null;
}
Also used : GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) GuildMemberJoinEvent(net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent) MemberImpl(net.dv8tion.jda.internal.entities.MemberImpl)

Example 5 with MemberImpl

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

the class GuildMembersChunkHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    final long guildId = content.getLong("guild_id");
    DataArray members = content.getArray("members");
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
    if (guild != null) {
        if (api.getClient().getChunkManager().handleChunk(guildId, content))
            return null;
        WebSocketClient.LOG.debug("Received member chunk for guild that is already in cache. GuildId: {} Count: {} Index: {}/{}", guildId, members.length(), content.getInt("chunk_index"), content.getInt("chunk_count"));
        // Chunk handling
        EntityBuilder builder = getJDA().getEntityBuilder();
        TLongObjectMap<DataObject> presences = content.optArray("presences").map(it -> builder.convertToUserMap(o -> o.getObject("user").getUnsignedLong("id"), it)).orElseGet(TLongObjectHashMap::new);
        for (int i = 0; i < members.length(); i++) {
            DataObject object = members.getObject(i);
            long userId = object.getObject("user").getUnsignedLong("id");
            DataObject presence = presences.get(userId);
            MemberImpl member = builder.createMember(guild, object, null, presence);
            builder.updateMemberCache(member);
        }
        return null;
    }
    getJDA().getGuildSetupController().onMemberChunk(guildId, content);
    return null;
}
Also used : TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) DataArray(net.dv8tion.jda.api.utils.data.DataArray) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) WebSocketClient(net.dv8tion.jda.internal.requests.WebSocketClient) 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) 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) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) EntityBuilder(net.dv8tion.jda.internal.entities.EntityBuilder) DataArray(net.dv8tion.jda.api.utils.data.DataArray)

Aggregations

MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)9 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)8 DataObject (net.dv8tion.jda.api.utils.data.DataObject)6 DataArray (net.dv8tion.jda.api.utils.data.DataArray)3 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)3 TLongObjectMap (gnu.trove.map.TLongObjectMap)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 OffsetDateTime (java.time.OffsetDateTime)2 ArrayList (java.util.ArrayList)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 Role (net.dv8tion.jda.api.entities.Role)2 JDAImpl (net.dv8tion.jda.internal.JDAImpl)2 EmoteImpl (net.dv8tion.jda.internal.entities.EmoteImpl)2 List (java.util.List)1 Objects (java.util.Objects)1 Nonnull (javax.annotation.Nonnull)1 OnlineStatus (net.dv8tion.jda.api.OnlineStatus)1 Activity (net.dv8tion.jda.api.entities.Activity)1 AudioChannel (net.dv8tion.jda.api.entities.AudioChannel)1 Emote (net.dv8tion.jda.api.entities.Emote)1