use of net.dv8tion.jda.internal.entities.GuildVoiceStateImpl 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();
}
Aggregations