Search in sources :

Example 1 with VoiceChannelChangePositionListener

use of de.btobastian.javacord.listener.voicechannel.VoiceChannelChangePositionListener 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);
    }
}
Also used : ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) VoiceChannelChangeNameListener(de.btobastian.javacord.listener.voicechannel.VoiceChannelChangeNameListener) User(de.btobastian.javacord.entities.User) VoiceChannelChangePositionListener(de.btobastian.javacord.listener.voicechannel.VoiceChannelChangePositionListener) ImplPermissions(de.btobastian.javacord.entities.permissions.impl.ImplPermissions) JSONArray(org.json.JSONArray) ImplVoiceChannel(de.btobastian.javacord.entities.impl.ImplVoiceChannel) ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) Role(de.btobastian.javacord.entities.permissions.Role) RoleChangeOverwrittenPermissionsListener(de.btobastian.javacord.listener.role.RoleChangeOverwrittenPermissionsListener) JSONObject(org.json.JSONObject) Permissions(de.btobastian.javacord.entities.permissions.Permissions) ImplPermissions(de.btobastian.javacord.entities.permissions.impl.ImplPermissions) ImplVoiceChannel(de.btobastian.javacord.entities.impl.ImplVoiceChannel) VoiceChannel(de.btobastian.javacord.entities.VoiceChannel) List(java.util.List) UserChangeOverwrittenPermissionsListener(de.btobastian.javacord.listener.user.UserChangeOverwrittenPermissionsListener) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

User (de.btobastian.javacord.entities.User)1 VoiceChannel (de.btobastian.javacord.entities.VoiceChannel)1 ImplVoiceChannel (de.btobastian.javacord.entities.impl.ImplVoiceChannel)1 Permissions (de.btobastian.javacord.entities.permissions.Permissions)1 Role (de.btobastian.javacord.entities.permissions.Role)1 ImplPermissions (de.btobastian.javacord.entities.permissions.impl.ImplPermissions)1 ImplRole (de.btobastian.javacord.entities.permissions.impl.ImplRole)1 RoleChangeOverwrittenPermissionsListener (de.btobastian.javacord.listener.role.RoleChangeOverwrittenPermissionsListener)1 UserChangeOverwrittenPermissionsListener (de.btobastian.javacord.listener.user.UserChangeOverwrittenPermissionsListener)1 VoiceChannelChangeNameListener (de.btobastian.javacord.listener.voicechannel.VoiceChannelChangeNameListener)1 VoiceChannelChangePositionListener (de.btobastian.javacord.listener.voicechannel.VoiceChannelChangePositionListener)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1