Search in sources :

Example 1 with GuildImpl

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

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

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

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

the class WebSocketClient method updateAudioManagerReferences.

protected void updateAudioManagerReferences() {
    AbstractCacheView<AudioManager> managerView = api.getAudioManagersView();
    try (UnlockHook hook = managerView.writeLock()) {
        final TLongObjectMap<AudioManager> managerMap = managerView.getMap();
        if (managerMap.size() > 0)
            LOG.trace("Updating AudioManager references");
        for (TLongObjectIterator<AudioManager> it = managerMap.iterator(); it.hasNext(); ) {
            it.advance();
            final long guildId = it.key();
            final AudioManagerImpl mng = (AudioManagerImpl) it.value();
            GuildImpl guild = (GuildImpl) api.getGuildById(guildId);
            if (guild == null) {
                // We no longer have access to the guild that this audio manager was for. Set the value to null.
                queuedAudioConnections.remove(guildId);
                mng.closeAudioConnection(ConnectionStatus.DISCONNECTED_REMOVED_DURING_RECONNECT);
                it.remove();
            }
        }
    }
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) AudioManagerImpl(net.dv8tion.jda.internal.managers.AudioManagerImpl) UnlockHook(net.dv8tion.jda.internal.utils.UnlockHook)

Example 5 with GuildImpl

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

the class StageInstanceDeleteHandler method handleInternally.

@Override
protected Long handleInternally(DataObject content) {
    long guildId = content.getUnsignedLong("guild_id", 0L);
    if (getJDA().getGuildSetupController().isLocked(guildId))
        return guildId;
    GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId);
    if (guild == null) {
        EventCache.LOG.debug("Caching STAGE_INSTANCE_DELETE for uncached guild with id {}", guildId);
        getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle);
        return null;
    }
    long channelId = content.getUnsignedLong("channel_id", 0L);
    StageChannelImpl channel = (StageChannelImpl) guild.getStageChannelById(channelId);
    if (channel == null)
        return null;
    StageInstance instance = channel.getStageInstance();
    channel.setStageInstance(null);
    if (instance != null)
        getJDA().handleEvent(new StageInstanceDeleteEvent(getJDA(), responseNumber, instance));
    return null;
}
Also used : GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) StageInstanceDeleteEvent(net.dv8tion.jda.api.events.stage.StageInstanceDeleteEvent) StageInstance(net.dv8tion.jda.api.entities.StageInstance) StageChannelImpl(net.dv8tion.jda.internal.entities.StageChannelImpl)

Aggregations

GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)24 DataObject (net.dv8tion.jda.api.utils.data.DataObject)9 MemberImpl (net.dv8tion.jda.internal.entities.MemberImpl)8 DataArray (net.dv8tion.jda.api.utils.data.DataArray)6 Role (net.dv8tion.jda.api.entities.Role)5 UnlockHook (net.dv8tion.jda.internal.utils.UnlockHook)4 Emote (net.dv8tion.jda.api.entities.Emote)3 StageInstance (net.dv8tion.jda.api.entities.StageInstance)3 EntityBuilder (net.dv8tion.jda.internal.entities.EntityBuilder)3 AudioManagerImpl (net.dv8tion.jda.internal.managers.AudioManagerImpl)3 TLongObjectMap (gnu.trove.map.TLongObjectMap)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 OffsetDateTime (java.time.OffsetDateTime)2 ArrayList (java.util.ArrayList)2 Guild (net.dv8tion.jda.api.entities.Guild)2 ISnowflake (net.dv8tion.jda.api.entities.ISnowflake)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 User (net.dv8tion.jda.api.entities.User)2 ChannelDeleteEvent (net.dv8tion.jda.api.events.channel.ChannelDeleteEvent)2 AudioManager (net.dv8tion.jda.api.managers.AudioManager)2