Search in sources :

Example 11 with ImplServer

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

Example 12 with ImplServer

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

the class ReadyHandler method handle.

@Override
public void handle(JSONObject packet) {
    // guild = server
    JSONArray guilds = packet.getJSONArray("guilds");
    for (int i = 0; i < guilds.length(); i++) {
        JSONObject guild = guilds.getJSONObject(i);
        if (guild.has("unavailable") && guild.getBoolean("unavailable")) {
            // add guild to the list of unavailable servers
            api.getUnavailableServers().add(guild.getString("id"));
            continue;
        }
        new ImplServer(guild, api);
    }
    JSONArray privateChannels = packet.getJSONArray("private_channels");
    for (int i = 0; i < privateChannels.length(); i++) {
        JSONObject privateChannel = privateChannels.getJSONObject(i);
        String id = privateChannel.getString("id");
        if (privateChannel.has("recipient")) {
            User user = api.getOrCreateUser(privateChannel.getJSONObject("recipient"));
            if (user != null) {
                ((ImplUser) user).setUserChannelId(id);
            }
        }
    }
    api.setYourself(api.getOrCreateUser(packet.getJSONObject("user")));
}
Also used : ImplServer(de.btobastian.javacord.entities.impl.ImplServer) ImplUser(de.btobastian.javacord.entities.impl.ImplUser) User(de.btobastian.javacord.entities.User) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ImplUser(de.btobastian.javacord.entities.impl.ImplUser)

Aggregations

ImplServer (de.btobastian.javacord.entities.impl.ImplServer)12 List (java.util.List)11 Server (de.btobastian.javacord.entities.Server)7 User (de.btobastian.javacord.entities.User)6 Role (de.btobastian.javacord.entities.permissions.Role)3 ImplRole (de.btobastian.javacord.entities.permissions.impl.ImplRole)3 JSONArray (org.json.JSONArray)3 VoiceChannel (de.btobastian.javacord.entities.VoiceChannel)2 ImplUser (de.btobastian.javacord.entities.impl.ImplUser)2 VoiceChannelDeleteListener (de.btobastian.javacord.listener.voicechannel.VoiceChannelDeleteListener)2 JSONObject (org.json.JSONObject)2 Channel (de.btobastian.javacord.entities.Channel)1 Region (de.btobastian.javacord.entities.Region)1 UserStatus (de.btobastian.javacord.entities.UserStatus)1 ChannelDeleteListener (de.btobastian.javacord.listener.channel.ChannelDeleteListener)1 RoleCreateListener (de.btobastian.javacord.listener.role.RoleCreateListener)1 RoleDeleteListener (de.btobastian.javacord.listener.role.RoleDeleteListener)1 ServerChangeIconListener (de.btobastian.javacord.listener.server.ServerChangeIconListener)1 ServerChangeNameListener (de.btobastian.javacord.listener.server.ServerChangeNameListener)1 ServerChangeOwnerListener (de.btobastian.javacord.listener.server.ServerChangeOwnerListener)1