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;
}
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();
}
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);
});
});
}
}
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;
}
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;
}
Aggregations