use of de.btobastian.javacord.listener.voice.UserLeaveVoiceChannelListener 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