use of de.btobastian.javacord.entities.impl.ImplVoiceChannel in project Javacord by BtoBastian.
the class ChannelUpdateHandler method handleServerVoiceChannel.
/**
* Handles the server voice channels.
*
* @param packet The packet (the "d"-object).
* @param server The server of the channel.
*/
private void handleServerVoiceChannel(JSONObject packet, Server server) {
final VoiceChannel channel = server.getVoiceChannelById(packet.getString("id"));
if (channel == null) {
// no channel with the given id was found
return;
}
String name = packet.getString("name");
if (!channel.getName().equals(name)) {
final String oldName = channel.getName();
((ImplVoiceChannel) channel).setName(name);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<VoiceChannelChangeNameListener> listeners = api.getListeners(VoiceChannelChangeNameListener.class);
synchronized (listeners) {
for (VoiceChannelChangeNameListener listener : listeners) {
try {
listener.onVoiceChannelChangeName(api, channel, oldName);
} catch (Throwable t) {
logger.warn("Uncaught exception in VoiceChannelChangeNameListener!", t);
}
}
}
}
});
}
int position = packet.getInt("position");
if (channel.getPosition() != position) {
final int oldPosition = channel.getPosition();
((ImplVoiceChannel) channel).setPosition(position);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<VoiceChannelChangePositionListener> listeners = api.getListeners(VoiceChannelChangePositionListener.class);
synchronized (listeners) {
for (VoiceChannelChangePositionListener listener : listeners) {
try {
listener.onVoiceChannelChangePosition(api, channel, oldPosition);
} catch (Throwable t) {
logger.warn("Uncaught exception in VoiceChannelChangePositionListener!", t);
}
}
}
}
});
}
JSONArray permissionOverwrites = packet.getJSONArray("permission_overwrites");
for (int i = 0; i < permissionOverwrites.length(); i++) {
JSONObject permissionOverwrite = permissionOverwrites.getJSONObject(i);
int allow = permissionOverwrite.getInt("allow");
int deny = permissionOverwrite.getInt("deny");
String id = permissionOverwrite.getString("id");
String type = permissionOverwrite.getString("type");
// permissions overwritten by users
if (type.equals("member")) {
final User user;
try {
user = api.getUserById(id).get();
} catch (InterruptedException | ExecutionException e) {
continue;
}
ImplPermissions permissions = new ImplPermissions(allow, deny);
final Permissions oldPermissions = channel.getOverwrittenPermissions(user);
if (!oldPermissions.equals(permissions)) {
((ImplVoiceChannel) channel).setOverwrittenPermissions(user, permissions);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<UserChangeOverwrittenPermissionsListener> listeners = api.getListeners(UserChangeOverwrittenPermissionsListener.class);
synchronized (listeners) {
for (UserChangeOverwrittenPermissionsListener listener : listeners) {
try {
listener.onUserChangeOverwrittenPermissions(api, user, channel, oldPermissions);
} catch (Throwable t) {
logger.warn("Uncaught exception in UserChangeOverwrittenPermissionsListener!", t);
}
}
}
}
});
}
}
// permissions overwritten by roles
if (type.equals("role")) {
final Role role = channel.getServer().getRoleById(id);
ImplPermissions permissions = new ImplPermissions(allow, deny);
final Permissions oldPermissions = role.getOverwrittenPermissions(channel);
if (!permissions.equals(oldPermissions)) {
((ImplRole) role).setOverwrittenPermissions(channel, permissions);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<RoleChangeOverwrittenPermissionsListener> listeners = api.getListeners(RoleChangeOverwrittenPermissionsListener.class);
synchronized (listeners) {
for (RoleChangeOverwrittenPermissionsListener listener : listeners) {
try {
listener.onRoleChangeOverwrittenPermissions(api, role, channel, oldPermissions);
} catch (Throwable t) {
logger.warn("Uncaught exception in RoleChangeOverwrittenPermissionsListener!", t);
}
}
}
}
});
}
}
}
String parentId = packet.isNull("parent_id") ? null : packet.getString("parent_id");
if (!Objects.deepEquals(channel.getParentId(), parentId)) {
((ImplVoiceChannel) channel).setParentId(parentId);
}
}
use of de.btobastian.javacord.entities.impl.ImplVoiceChannel in project Javacord by BtoBastian.
the class ChannelCreateHandler method handleServerVoiceChannel.
/**
* Handles the server voice channels.
*
* @param packet The packet (the "d"-object).
* @param server The server of the channel.
*/
private void handleServerVoiceChannel(JSONObject packet, Server server) {
if (server.getVoiceChannelById(packet.getString("id")) != null) {
return;
}
final VoiceChannel channel = new ImplVoiceChannel(packet, (ImplServer) server, api);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<VoiceChannelCreateListener> listeners = api.getListeners(VoiceChannelCreateListener.class);
synchronized (listeners) {
for (VoiceChannelCreateListener listener : listeners) {
try {
listener.onVoiceChannelCreate(api, channel);
} catch (Throwable t) {
logger.warn("Uncaught exception in VoiceChannelCreateListener!", t);
}
}
}
}
});
}
use of de.btobastian.javacord.entities.impl.ImplVoiceChannel in project Javacord by BtoBastian.
the class VoiceStateUpdateHandler method handle.
@Override
public void handle(JSONObject packet) {
ImplUser user = null;
try {
user = (ImplUser) api.getUserById(packet.getString("user_id")).get();
} catch (JSONException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
String channelId = null;
try {
channelId = packet.getString("channel_id");
} catch (JSONException ignored) {
}
final User userPassed = user;
if (channelId != null) {
if (user.getVoiceChannel() != null) {
if (channelId.equals(user.getVoiceChannel().getId())) {
// Probably a mute/unmute event; Ignore for now
return;
}
((ImplVoiceChannel) user.getVoiceChannel()).removeConnectedUser(user);
}
final ImplVoiceChannel channel = (ImplVoiceChannel) api.getVoiceChannelById(channelId);
if (channel == null) {
return;
}
channel.addConnectedUser(user);
user.setVoiceChannel(channel);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<UserJoinVoiceChannelListener> listeners = api.getListeners(UserJoinVoiceChannelListener.class);
synchronized (listeners) {
for (UserJoinVoiceChannelListener listener : listeners) {
try {
listener.onUserJoinVoiceChannel(api, userPassed, channel);
} catch (Throwable t) {
logger.warn("Uncaught exception in UserJoinVoiceChannelListener!", t);
}
}
}
}
});
} else {
if (user.getVoiceChannel() != null) {
((ImplVoiceChannel) user.getVoiceChannel()).removeConnectedUser(user);
}
user.setVoiceChannel(null);
listenerExecutorService.submit(new Runnable() {
@Override
public void run() {
List<UserLeaveVoiceChannelListener> listeners = api.getListeners(UserLeaveVoiceChannelListener.class);
synchronized (listeners) {
for (UserLeaveVoiceChannelListener listener : listeners) {
try {
listener.onUserLeaveVoiceChannel(api, userPassed);
} catch (Throwable t) {
logger.warn("Uncaught exception in UserLeaveVoiceChannelListener!", t);
}
}
}
}
});
}
}
Aggregations