Search in sources :

Example 6 with ImplServer

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

the class GuildRoleDeleteHandler method handle.

@Override
public void handle(JSONObject packet) {
    String guildId = packet.getString("guild_id");
    String roleId = packet.getString("role_id");
    Server server = api.getServerById(guildId);
    final Role role = server.getRoleById(roleId);
    if (role == null) {
        return;
    }
    ((ImplServer) server).removeRole(role);
    listenerExecutorService.submit(new Runnable() {

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

Example 7 with ImplServer

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

the class ChannelDeleteHandler 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) {
        return;
    }
    ((ImplServer) server).removeVoiceChannel(channel);
    listenerExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            List<VoiceChannelDeleteListener> listeners = api.getListeners(VoiceChannelDeleteListener.class);
            synchronized (listeners) {
                for (VoiceChannelDeleteListener listener : listeners) {
                    try {
                        listener.onVoiceChannelDelete(api, channel);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in VoiceChannelDeleteListener!", t);
                    }
                }
            }
        }
    });
}
Also used : ImplServer(de.btobastian.javacord.entities.impl.ImplServer) VoiceChannel(de.btobastian.javacord.entities.VoiceChannel) List(java.util.List) VoiceChannelDeleteListener(de.btobastian.javacord.listener.voicechannel.VoiceChannelDeleteListener)

Example 8 with ImplServer

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

the class ChannelDeleteHandler 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) {
        return;
    }
    ((ImplServer) server).removeChannel(channel);
    listenerExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            List<ChannelDeleteListener> listeners = api.getListeners(ChannelDeleteListener.class);
            synchronized (listeners) {
                for (ChannelDeleteListener listener : listeners) {
                    try {
                        listener.onChannelDelete(api, channel);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in ChannelDeleteListener!", t);
                    }
                }
            }
        }
    });
}
Also used : ImplServer(de.btobastian.javacord.entities.impl.ImplServer) Channel(de.btobastian.javacord.entities.Channel) VoiceChannel(de.btobastian.javacord.entities.VoiceChannel) VoiceChannelDeleteListener(de.btobastian.javacord.listener.voicechannel.VoiceChannelDeleteListener) ChannelDeleteListener(de.btobastian.javacord.listener.channel.ChannelDeleteListener) List(java.util.List)

Example 9 with ImplServer

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

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

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