Search in sources :

Example 6 with ImplRole

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

the class PresenceUpdateHandler method handle.

@Override
public void handle(JSONObject packet) {
    final User user = api.getOrCreateUser(packet.getJSONObject("user"));
    if (user == null) {
        return;
    }
    Server server = null;
    if (packet.has("guild_id")) {
        server = api.getServerById(packet.getString("guild_id"));
    }
    if (server != null) {
        // add user to server
        ((ImplServer) server).addMember(user);
    }
    if (server != null && packet.has("roles")) {
        JSONArray roleIds = packet.getJSONArray("roles");
        for (int i = 0; i < roleIds.length(); i++) {
            // add user to the role
            ((ImplRole) server.getRoleById(roleIds.getString(i))).addUserNoUpdate(user);
        }
    }
    // check status
    if (packet.has("status")) {
        UserStatus status = UserStatus.fromString(packet.getString("status"));
        final UserStatus oldStatus = user.getStatus();
        ((ImplUser) user).setStatus(status);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<UserChangeStatusListener> listeners = api.getListeners(UserChangeStatusListener.class);
                synchronized (listeners) {
                    for (UserChangeStatusListener listener : listeners) {
                        try {
                            listener.onUserChangeStatus(api, user, oldStatus);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in UserChangeStatusListener!", t);
                        }
                    }
                }
            }
        });
    }
    // check username
    if (packet.getJSONObject("user").has("username")) {
        String name = packet.getJSONObject("user").getString("username");
        if (!user.getName().equals(name)) {
            final String oldName = user.getName();
            ((ImplUser) user).setName(name);
            listenerExecutorService.submit(new Runnable() {

                @Override
                public void run() {
                    List<UserChangeNameListener> listeners = api.getListeners(UserChangeNameListener.class);
                    synchronized (listeners) {
                        for (UserChangeNameListener listener : listeners) {
                            try {
                                listener.onUserChangeName(api, user, oldName);
                            } catch (Throwable t) {
                                logger.warn("Uncaught exception in UserChangeNameListener!", t);
                            }
                        }
                    }
                }
            });
        }
    }
    // check game
    if (packet.has("game")) {
        String game;
        if (!packet.isNull("game") && packet.getJSONObject("game").has("name") && !packet.getJSONObject("game").isNull("name")) {
            game = packet.getJSONObject("game").get("name").toString();
        } else {
            game = null;
        }
        String oldGame = user.getGame();
        if ((game == null && oldGame != null) || (game != null && oldGame == null) || (game != null && !game.equals(oldGame))) {
            ((ImplUser) user).setGame(game);
            List<UserChangeGameListener> listeners = api.getListeners(UserChangeGameListener.class);
            synchronized (listeners) {
                for (UserChangeGameListener listener : listeners) {
                    try {
                        listener.onUserChangeGame(api, user, oldGame);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in UserChangeGameListener!", t);
                    }
                }
            }
        }
    }
}
Also used : ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) ImplUser(de.btobastian.javacord.entities.impl.ImplUser) User(de.btobastian.javacord.entities.User) ImplServer(de.btobastian.javacord.entities.impl.ImplServer) UserChangeStatusListener(de.btobastian.javacord.listener.user.UserChangeStatusListener) Server(de.btobastian.javacord.entities.Server) ImplServer(de.btobastian.javacord.entities.impl.ImplServer) JSONArray(org.json.JSONArray) UserChangeGameListener(de.btobastian.javacord.listener.user.UserChangeGameListener) UserStatus(de.btobastian.javacord.entities.UserStatus) List(java.util.List) ImplUser(de.btobastian.javacord.entities.impl.ImplUser) UserChangeNameListener(de.btobastian.javacord.listener.user.UserChangeNameListener)

Example 7 with ImplRole

use of de.btobastian.javacord.entities.permissions.impl.ImplRole 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

ImplRole (de.btobastian.javacord.entities.permissions.impl.ImplRole)7 Role (de.btobastian.javacord.entities.permissions.Role)6 List (java.util.List)6 User (de.btobastian.javacord.entities.User)5 JSONArray (org.json.JSONArray)5 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 UserStatus (de.btobastian.javacord.entities.UserStatus)1 ImplChannel (de.btobastian.javacord.entities.impl.ImplChannel)1