Search in sources :

Example 6 with Role

use of de.btobastian.javacord.entities.permissions.Role in project Javacord by BtoBastian.

the class ChannelUpdateHandler method handleServerTextChannel.

/**
 * Handles the server text channels.
 *
 * @param packet The packet (the "d"-object).
 * @param server The server of the channel.
 */
private void handleServerTextChannel(JSONObject packet, Server server) {
    final Channel channel = server.getChannelById(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();
        ((ImplChannel) channel).setName(name);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<ChannelChangeNameListener> listeners = api.getListeners(ChannelChangeNameListener.class);
                synchronized (listeners) {
                    for (ChannelChangeNameListener listener : listeners) {
                        try {
                            listener.onChannelChangeName(api, channel, oldName);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in ChannelChangeNameListener!", t);
                        }
                    }
                }
            }
        });
    }
    String topic = packet.get("topic") == null ? null : packet.get("topic").toString();
    if ((channel.getTopic() != null && topic == null) || (channel.getTopic() == null && topic != null) || (channel.getTopic() != null && !channel.getTopic().equals(topic))) {
        final String oldTopic = channel.getTopic();
        ((ImplChannel) channel).setTopic(topic);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<ChannelChangeTopicListener> listeners = api.getListeners(ChannelChangeTopicListener.class);
                synchronized (listeners) {
                    for (ChannelChangeTopicListener listener : listeners) {
                        try {
                            listener.onChannelChangeTopic(api, channel, oldTopic);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in ChannelChangeTopicListener!", t);
                        }
                    }
                }
            }
        });
    }
    int position = packet.getInt("position");
    if (channel.getPosition() != position) {
        final int oldPosition = channel.getPosition();
        ((ImplChannel) channel).setPosition(position);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<ChannelChangePositionListener> listeners = api.getListeners(ChannelChangePositionListener.class);
                synchronized (listeners) {
                    for (ChannelChangePositionListener listener : listeners) {
                        try {
                            listener.onChannelChangePosition(api, channel, oldPosition);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in ChannelChangePositionListener!", 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)) {
                ((ImplChannel) 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)) {
        ((ImplChannel) channel).setParentId(parentId);
    }
}
Also used : ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) User(de.btobastian.javacord.entities.User) ChannelChangePositionListener(de.btobastian.javacord.listener.channel.ChannelChangePositionListener) VoiceChannelChangePositionListener(de.btobastian.javacord.listener.voicechannel.VoiceChannelChangePositionListener) ImplPermissions(de.btobastian.javacord.entities.permissions.impl.ImplPermissions) Channel(de.btobastian.javacord.entities.Channel) ImplVoiceChannel(de.btobastian.javacord.entities.impl.ImplVoiceChannel) ImplChannel(de.btobastian.javacord.entities.impl.ImplChannel) VoiceChannel(de.btobastian.javacord.entities.VoiceChannel) JSONArray(org.json.JSONArray) ImplChannel(de.btobastian.javacord.entities.impl.ImplChannel) 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) VoiceChannelChangeNameListener(de.btobastian.javacord.listener.voicechannel.VoiceChannelChangeNameListener) ChannelChangeNameListener(de.btobastian.javacord.listener.channel.ChannelChangeNameListener) Permissions(de.btobastian.javacord.entities.permissions.Permissions) ImplPermissions(de.btobastian.javacord.entities.permissions.impl.ImplPermissions) List(java.util.List) ChannelChangeTopicListener(de.btobastian.javacord.listener.channel.ChannelChangeTopicListener) UserChangeOverwrittenPermissionsListener(de.btobastian.javacord.listener.user.UserChangeOverwrittenPermissionsListener) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with Role

use of de.btobastian.javacord.entities.permissions.Role in project Javacord by BtoBastian.

the class GuildRoleCreateHandler method handle.

@Override
public void handle(JSONObject packet) {
    String guildId = packet.getString("guild_id");
    JSONObject roleJson = packet.getJSONObject("role");
    Server server = api.getServerById(guildId);
    final Role role = new ImplRole(roleJson, (ImplServer) server, api);
    listenerExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            List<RoleCreateListener> listeners = api.getListeners(RoleCreateListener.class);
            synchronized (listeners) {
                for (RoleCreateListener listener : listeners) {
                    try {
                        listener.onRoleCreate(api, role);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in RoleCreateListener!", t);
                    }
                }
            }
        }
    });
}
Also used : ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) Role(de.btobastian.javacord.entities.permissions.Role) ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) JSONObject(org.json.JSONObject) Server(de.btobastian.javacord.entities.Server) ImplServer(de.btobastian.javacord.entities.impl.ImplServer) List(java.util.List) RoleCreateListener(de.btobastian.javacord.listener.role.RoleCreateListener)

Example 8 with Role

use of de.btobastian.javacord.entities.permissions.Role in project Javacord by BtoBastian.

the class GuildMemberUpdateHandler method handle.

@Override
public void handle(JSONObject packet) {
    final ImplServer server = (ImplServer) api.getServerById(packet.getString("guild_id"));
    final User user = api.getOrCreateUser(packet.getJSONObject("user"));
    if (server != null) {
        // update nickname
        if (packet.has("nick")) {
            String newNick = packet.isNull("nick") ? null : packet.getString("nick");
            final String oldNick = server.getNickname(user);
            if (newNick != null && !newNick.equals(oldNick) || ((oldNick != null) && !oldNick.equals(newNick))) {
                server.setNickname(user, newNick);
                listenerExecutorService.submit(new Runnable() {

                    @Override
                    public void run() {
                        List<UserChangeNicknameListener> listeners = api.getListeners(UserChangeNicknameListener.class);
                        synchronized (listeners) {
                            for (UserChangeNicknameListener listener : listeners) {
                                try {
                                    listener.onUserChangeNickname(api, server, user, oldNick);
                                } catch (Throwable t) {
                                    logger.warn("Uncaught exception in UserChangeNicknameListener!", t);
                                }
                            }
                        }
                    }
                });
            }
        }
        // get array with all roles
        JSONArray jsonRoles = packet.getJSONArray("roles");
        Role[] roles = new Role[jsonRoles.length()];
        for (int i = 0; i < jsonRoles.length(); i++) {
            roles[i] = server.getRoleById(jsonRoles.getString(i));
        }
        // iterate throw all current roles and remove roles which aren't in the roles array
        for (final Role role : user.getRoles(server)) {
            boolean contains = false;
            for (Role r : roles) {
                if (role == r) {
                    contains = true;
                    break;
                }
            }
            if (!contains) {
                ((ImplRole) role).removeUserNoUpdate(user);
                api.getThreadPool().getSingleThreadExecutorService("listeners").submit(new Runnable() {

                    @Override
                    public void run() {
                        List<UserRoleRemoveListener> listeners = api.getListeners(UserRoleRemoveListener.class);
                        synchronized (listeners) {
                            for (UserRoleRemoveListener listener : listeners) {
                                try {
                                    listener.onUserRoleRemove(api, user, role);
                                } catch (Throwable t) {
                                    logger.warn("Uncaught exception in UserRoleRemoveListenerListener!", t);
                                }
                            }
                        }
                    }
                });
            }
        }
        // iterate throw all roles of the roles array and remove add roles which aren't in the current roles list
        for (final Role role : roles) {
            if (!user.getRoles(server).contains(role)) {
                ((ImplRole) role).addUserNoUpdate(user);
                api.getThreadPool().getSingleThreadExecutorService("listeners").submit(new Runnable() {

                    @Override
                    public void run() {
                        List<UserRoleAddListener> listeners = api.getListeners(UserRoleAddListener.class);
                        synchronized (listeners) {
                            for (UserRoleAddListener listener : listeners) {
                                try {
                                    listener.onUserRoleAdd(api, user, role);
                                } catch (Throwable t) {
                                    logger.warn("Uncaught exception in UserRoleAddListener!", t);
                                }
                            }
                        }
                    }
                });
            }
        }
    }
}
Also used : ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) ImplServer(de.btobastian.javacord.entities.impl.ImplServer) User(de.btobastian.javacord.entities.User) UserRoleAddListener(de.btobastian.javacord.listener.user.UserRoleAddListener) JSONArray(org.json.JSONArray) Role(de.btobastian.javacord.entities.permissions.Role) ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) UserRoleRemoveListener(de.btobastian.javacord.listener.user.UserRoleRemoveListener) List(java.util.List) UserChangeNicknameListener(de.btobastian.javacord.listener.user.UserChangeNicknameListener)

Aggregations

Role (de.btobastian.javacord.entities.permissions.Role)8 ImplRole (de.btobastian.javacord.entities.permissions.impl.ImplRole)6 List (java.util.List)6 User (de.btobastian.javacord.entities.User)4 JSONArray (org.json.JSONArray)4 JSONObject (org.json.JSONObject)4 Server (de.btobastian.javacord.entities.Server)3 ImplServer (de.btobastian.javacord.entities.impl.ImplServer)3 Permissions (de.btobastian.javacord.entities.permissions.Permissions)3 ImplPermissions (de.btobastian.javacord.entities.permissions.impl.ImplPermissions)3 VoiceChannel (de.btobastian.javacord.entities.VoiceChannel)2 ImplVoiceChannel (de.btobastian.javacord.entities.impl.ImplVoiceChannel)2 RoleChangeOverwrittenPermissionsListener (de.btobastian.javacord.listener.role.RoleChangeOverwrittenPermissionsListener)2 UserChangeOverwrittenPermissionsListener (de.btobastian.javacord.listener.user.UserChangeOverwrittenPermissionsListener)2 VoiceChannelChangeNameListener (de.btobastian.javacord.listener.voicechannel.VoiceChannelChangeNameListener)2 VoiceChannelChangePositionListener (de.btobastian.javacord.listener.voicechannel.VoiceChannelChangePositionListener)2 ExecutionException (java.util.concurrent.ExecutionException)2 Channel (de.btobastian.javacord.entities.Channel)1 ImplChannel (de.btobastian.javacord.entities.impl.ImplChannel)1 ChannelChangeNameListener (de.btobastian.javacord.listener.channel.ChannelChangeNameListener)1