use of org.javacord.core.entity.channel.ServerVoiceChannelImpl in project Javacord by BtoBastian.
the class ChannelUpdateHandler method handleServerVoiceChannel.
/**
* Handles a server voice channel update.
*
* @param jsonChannel The channel data.
*/
private void handleServerVoiceChannel(JsonNode jsonChannel) {
long channelId = jsonChannel.get("id").asLong();
Optional<ServerVoiceChannel> optionalChannel = api.getServerVoiceChannelById(channelId);
if (!optionalChannel.isPresent()) {
LoggerUtil.logMissingChannel(logger, channelId);
return;
}
ServerVoiceChannelImpl channel = (ServerVoiceChannelImpl) optionalChannel.get();
int oldBitrate = channel.getBitrate();
int newBitrate = jsonChannel.get("bitrate").asInt();
if (oldBitrate != newBitrate) {
channel.setBitrate(newBitrate);
ServerVoiceChannelChangeBitrateEvent event = new ServerVoiceChannelChangeBitrateEventImpl(channel, newBitrate, oldBitrate);
api.getEventDispatcher().dispatchServerVoiceChannelChangeBitrateEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
}
int oldUserLimit = channel.getUserLimit().orElse(0);
int newUserLimit = jsonChannel.get("user_limit").asInt();
if (oldUserLimit != newUserLimit) {
channel.setUserLimit(newUserLimit);
ServerVoiceChannelChangeUserLimitEvent event = new ServerVoiceChannelChangeUserLimitEventImpl(channel, newUserLimit, oldUserLimit);
api.getEventDispatcher().dispatchServerVoiceChannelChangeUserLimitEvent((DispatchQueueSelector) channel.getServer(), channel.getServer(), channel, event);
}
}
use of org.javacord.core.entity.channel.ServerVoiceChannelImpl in project Javacord by BtoBastian.
the class ChannelUpdateHandler method handleRegularServerChannel.
private void handleRegularServerChannel(JsonNode jsonChannel) {
long channelId = jsonChannel.get("id").asLong();
Optional<RegularServerChannel> optionalChannel = api.getRegularServerChannelById(channelId);
if (!optionalChannel.isPresent()) {
LoggerUtil.logMissingChannel(logger, channelId);
return;
}
RegularServerChannelImpl channel = (RegularServerChannelImpl) optionalChannel.get();
ServerImpl server = (ServerImpl) channel.getServer();
final AtomicBoolean areYouAffected = new AtomicBoolean(false);
ChannelCategory oldCategory = channel.asCategorizable().flatMap(Categorizable::getCategory).orElse(null);
ChannelCategory newCategory = jsonChannel.hasNonNull("parent_id") ? channel.getServer().getChannelCategoryById(jsonChannel.get("parent_id").asLong(-1)).orElse(null) : null;
final RegularServerChannelImpl regularServerChannel = (RegularServerChannelImpl) channel;
final int oldRawPosition = regularServerChannel.getRawPosition();
final int newRawPosition = jsonChannel.get("position").asInt();
if (oldRawPosition != newRawPosition || !Objects.deepEquals(oldCategory, newCategory)) {
final int oldPosition = regularServerChannel.getPosition();
if (regularServerChannel instanceof ServerTextChannelImpl) {
((ServerTextChannelImpl) regularServerChannel).setParentId(newCategory == null ? -1 : newCategory.getId());
} else if (regularServerChannel instanceof ServerVoiceChannelImpl) {
((ServerVoiceChannelImpl) regularServerChannel).setParentId(newCategory == null ? -1 : newCategory.getId());
}
regularServerChannel.setRawPosition(newRawPosition);
final int newPosition = regularServerChannel.getPosition();
final ServerChannelChangePositionEvent event = new ServerChannelChangePositionEventImpl(regularServerChannel, newPosition, oldPosition, newRawPosition, oldRawPosition, newCategory, oldCategory);
if (server.isReady()) {
api.getEventDispatcher().dispatchServerChannelChangePositionEvent((DispatchQueueSelector) regularServerChannel.getServer(), regularServerChannel.getServer(), regularServerChannel, event);
}
}
Collection<Long> rolesWithOverwrittenPermissions = new HashSet<>();
Collection<Long> usersWithOverwrittenPermissions = new HashSet<>();
if (jsonChannel.has("permission_overwrites") && !jsonChannel.get("permission_overwrites").isNull()) {
for (JsonNode permissionOverwriteJson : jsonChannel.get("permission_overwrites")) {
Permissions oldOverwrittenPermissions;
ConcurrentHashMap<Long, Permissions> overwrittenPermissions;
long entityId = permissionOverwriteJson.get("id").asLong();
Optional<DiscordEntity> entity;
switch(permissionOverwriteJson.get("type").asInt()) {
case 0:
Role role = server.getRoleById(entityId).orElseThrow(() -> new IllegalStateException("Received channel update event with unknown role!"));
entity = Optional.of(role);
oldOverwrittenPermissions = regularServerChannel.getOverwrittenPermissions(role);
overwrittenPermissions = regularServerChannel.getInternalOverwrittenRolePermissions();
rolesWithOverwrittenPermissions.add(entityId);
break;
case 1:
oldOverwrittenPermissions = regularServerChannel.getOverwrittenUserPermissions().getOrDefault(entityId, PermissionsImpl.EMPTY_PERMISSIONS);
entity = api.getCachedUserById(entityId).map(DiscordEntity.class::cast);
overwrittenPermissions = regularServerChannel.getInternalOverwrittenUserPermissions();
usersWithOverwrittenPermissions.add(entityId);
break;
default:
throw new IllegalStateException("Permission overwrite object with unknown type: " + permissionOverwriteJson);
}
long allow = permissionOverwriteJson.get("allow").asLong(0);
long deny = permissionOverwriteJson.get("deny").asLong(0);
Permissions newOverwrittenPermissions = new PermissionsImpl(allow, deny);
if (!newOverwrittenPermissions.equals(oldOverwrittenPermissions)) {
overwrittenPermissions.put(entityId, newOverwrittenPermissions);
if (server.isReady()) {
dispatchServerChannelChangeOverwrittenPermissionsEvent(channel, newOverwrittenPermissions, oldOverwrittenPermissions, entityId, entity.orElse(null));
areYouAffected.compareAndSet(false, entityId == api.getYourself().getId());
entity.filter(e -> e instanceof Role).map(Role.class::cast).ifPresent(role -> areYouAffected.compareAndSet(false, role.getUsers().stream().anyMatch(User::isYourself)));
}
}
}
}
ConcurrentHashMap<Long, Permissions> overwrittenRolePermissions;
ConcurrentHashMap<Long, Permissions> overwrittenUserPermissions;
overwrittenRolePermissions = regularServerChannel.getInternalOverwrittenRolePermissions();
overwrittenUserPermissions = regularServerChannel.getInternalOverwrittenUserPermissions();
Iterator<Map.Entry<Long, Permissions>> userIt = overwrittenUserPermissions.entrySet().iterator();
while (userIt.hasNext()) {
Map.Entry<Long, Permissions> entry = userIt.next();
if (usersWithOverwrittenPermissions.contains(entry.getKey())) {
continue;
}
Permissions oldPermissions = entry.getValue();
userIt.remove();
if (server.isReady()) {
dispatchServerChannelChangeOverwrittenPermissionsEvent(channel, PermissionsImpl.EMPTY_PERMISSIONS, oldPermissions, entry.getKey(), api.getCachedUserById(entry.getKey()).orElse(null));
areYouAffected.compareAndSet(false, entry.getKey() == api.getYourself().getId());
}
}
Iterator<Map.Entry<Long, Permissions>> roleIt = overwrittenRolePermissions.entrySet().iterator();
while (roleIt.hasNext()) {
Map.Entry<Long, Permissions> entry = roleIt.next();
if (rolesWithOverwrittenPermissions.contains(entry.getKey())) {
continue;
}
api.getRoleById(entry.getKey()).ifPresent(role -> {
Permissions oldPermissions = entry.getValue();
roleIt.remove();
if (server.isReady()) {
dispatchServerChannelChangeOverwrittenPermissionsEvent(channel, PermissionsImpl.EMPTY_PERMISSIONS, oldPermissions, role.getId(), role);
areYouAffected.compareAndSet(false, role.getUsers().stream().anyMatch(User::isYourself));
}
});
}
if (areYouAffected.get() && !channel.canYouSee()) {
api.forEachCachedMessageWhere(msg -> msg.getChannel().getId() == channelId, msg -> {
api.removeMessageFromCache(msg.getId());
((MessageCacheImpl) ((TextChannel) channel).getMessageCache()).removeMessage(msg);
});
}
}
use of org.javacord.core.entity.channel.ServerVoiceChannelImpl in project Javacord by BtoBastian.
the class VoiceStateUpdateHandler method handleServerVoiceChannel.
private void handleServerVoiceChannel(JsonNode packet, long userId) {
api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(ServerImpl.class::cast).ifPresent(server -> {
Member member = new MemberImpl(api, server, packet.get("member"), null);
Optional<ServerVoiceChannelImpl> oldChannel = server.getConnectedVoiceChannel(userId).map(ServerVoiceChannelImpl.class::cast);
Optional<ServerVoiceChannelImpl> newChannel;
if (packet.hasNonNull("channel_id")) {
newChannel = server.getVoiceChannelById(packet.get("channel_id").asLong()).map(ServerVoiceChannelImpl.class::cast);
} else {
newChannel = Optional.empty();
}
if (!newChannel.equals(oldChannel)) {
oldChannel.ifPresent(channel -> {
channel.removeConnectedUser(userId);
dispatchServerVoiceChannelMemberLeaveEvent(member, newChannel.orElse(null), channel, server);
});
newChannel.ifPresent(channel -> {
channel.addConnectedUser(userId);
dispatchServerVoiceChannelMemberJoinEvent(member, channel, oldChannel.orElse(null), server);
});
}
if (!packet.hasNonNull("member")) {
logger.warn("Received VOICE_STATE_UPDATE packet without non-null member field: {}", packet);
return;
}
MemberImpl newMember = new MemberImpl(api, server, packet.get("member"), null);
Member oldMember = server.getRealMemberById(packet.get("user_id").asLong()).orElse(null);
boolean newSelfMuted = packet.get("self_mute").asBoolean();
boolean oldSelfMuted = server.isSelfMuted(userId);
if (newSelfMuted != oldSelfMuted) {
UserChangeSelfMutedEventImpl event = new UserChangeSelfMutedEventImpl(newMember, oldMember);
api.getEventDispatcher().dispatchUserChangeSelfMutedEvent(server, server, newMember.getUser(), event);
}
boolean newSelfDeafened = packet.get("self_deaf").asBoolean();
boolean oldSelfDeafened = server.isSelfDeafened(userId);
if (newSelfDeafened != oldSelfDeafened) {
UserChangeSelfDeafenedEventImpl event = new UserChangeSelfDeafenedEventImpl(newMember, oldMember);
api.getEventDispatcher().dispatchUserChangeSelfDeafenedEvent(server, server, newMember.getUser(), event);
}
boolean newMuted = packet.get("mute").asBoolean();
boolean oldMuted = server.isMuted(userId);
if (newMuted != oldMuted) {
server.setMuted(userId, newMuted);
UserChangeMutedEventImpl event = new UserChangeMutedEventImpl(newMember, oldMember);
api.getEventDispatcher().dispatchUserChangeMutedEvent(server, server, newMember.getUser(), event);
}
boolean newDeafened = packet.get("deaf").asBoolean();
boolean oldDeafened = server.isDeafened(userId);
if (newDeafened != oldDeafened) {
server.setDeafened(userId, newDeafened);
UserChangeDeafenedEventImpl event = new UserChangeDeafenedEventImpl(newMember, oldMember);
api.getEventDispatcher().dispatchUserChangeDeafenedEvent(server, server, newMember.getUser(), event);
}
});
}
use of org.javacord.core.entity.channel.ServerVoiceChannelImpl in project Javacord by BtoBastian.
the class ServerImpl method getOrCreateServerVoiceChannel.
/**
* Gets or creates a server voice channel.
*
* @param data The json data of the channel.
* @return The server voice channel.
*/
public ServerVoiceChannel getOrCreateServerVoiceChannel(JsonNode data) {
long id = Long.parseLong(data.get("id").asText());
ChannelType type = ChannelType.fromId(data.get("type").asInt());
synchronized (this) {
if (type == ChannelType.SERVER_VOICE_CHANNEL) {
return getVoiceChannelById(id).orElseGet(() -> new ServerVoiceChannelImpl(api, this, data));
}
}
// Invalid channel type
return null;
}
Aggregations