Search in sources :

Example 1 with UserJoinVoiceChannelListener

use of de.btobastian.javacord.listener.voice.UserJoinVoiceChannelListener 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);
                        }
                    }
                }
            }
        });
    }
}
Also used : UserLeaveVoiceChannelListener(de.btobastian.javacord.listener.voice.UserLeaveVoiceChannelListener) ImplUser(de.btobastian.javacord.entities.impl.ImplUser) User(de.btobastian.javacord.entities.User) JSONException(org.json.JSONException) ImplVoiceChannel(de.btobastian.javacord.entities.impl.ImplVoiceChannel) List(java.util.List) UserJoinVoiceChannelListener(de.btobastian.javacord.listener.voice.UserJoinVoiceChannelListener) ExecutionException(java.util.concurrent.ExecutionException) ImplUser(de.btobastian.javacord.entities.impl.ImplUser)

Aggregations

User (de.btobastian.javacord.entities.User)1 ImplUser (de.btobastian.javacord.entities.impl.ImplUser)1 ImplVoiceChannel (de.btobastian.javacord.entities.impl.ImplVoiceChannel)1 UserJoinVoiceChannelListener (de.btobastian.javacord.listener.voice.UserJoinVoiceChannelListener)1 UserLeaveVoiceChannelListener (de.btobastian.javacord.listener.voice.UserLeaveVoiceChannelListener)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 JSONException (org.json.JSONException)1