Search in sources :

Example 1 with ImplServer

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

the class GuildBanRemoveHandler method handle.

@Override
public void handle(JSONObject packet) {
    final Server server = api.getServerById(packet.getString("guild_id"));
    final User user = api.getOrCreateUser(packet.getJSONObject("user"));
    if (server != null) {
        ((ImplServer) server).removeMember(user);
        listenerExecutorService.submit(new Runnable() {

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

Example 2 with ImplServer

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

the class GuildCreateHandler method handle.

@Override
public void handle(JSONObject packet) {
    if (packet.has("unavailable") && packet.getBoolean("unavailable")) {
        return;
    }
    String id = packet.getString("id");
    if (api.getUnavailableServers().contains(id)) {
        api.getUnavailableServers().remove(id);
        new ImplServer(packet, api);
        return;
    }
    if (api.getServerById(id) != null) {
        // TODO update information
        return;
    }
    final Server server = new ImplServer(packet, api);
    listenerExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            List<ServerJoinListener> listeners = api.getListeners(ServerJoinListener.class);
            synchronized (listeners) {
                for (ServerJoinListener listener : listeners) {
                    try {
                        listener.onServerJoin(api, server);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in ServerJoinListener!", t);
                    }
                }
            }
        }
    });
    api.getThreadPool().getExecutorService().submit(new Runnable() {

        @Override
        public void run() {
            // can cause a deadlock if someone blocks a listener
            // with the #createServer or #acceptInvite method + #get
            api.getInternalServerJoinListener().onServerJoin(api, server);
        }
    });
}
Also used : ServerJoinListener(de.btobastian.javacord.listener.server.ServerJoinListener) ImplServer(de.btobastian.javacord.entities.impl.ImplServer) Server(de.btobastian.javacord.entities.Server) ImplServer(de.btobastian.javacord.entities.impl.ImplServer) List(java.util.List)

Example 3 with ImplServer

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

the class GuildMemberAddHandler method handle.

@Override
public void handle(JSONObject packet) {
    final Server server = api.getServerById(packet.getString("guild_id"));
    final User user = api.getOrCreateUser(packet.getJSONObject("user"));
    if (server != null) {
        ((ImplServer) server).addMember(user);
        ((ImplServer) server).incrementMemberCount();
        listenerExecutorService.submit(new Runnable() {

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

Example 4 with ImplServer

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

the class GuildMemberRemoveHandler method handle.

@Override
public void handle(JSONObject packet) {
    final Server server = api.getServerById(packet.getString("guild_id"));
    final User user = api.getOrCreateUser(packet.getJSONObject("user"));
    if (server != null) {
        ((ImplServer) server).removeMember(user);
        ((ImplServer) server).decrementMemberCount();
        listenerExecutorService.submit(new Runnable() {

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

Example 5 with ImplServer

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

the class GuildUpdateHandler method handle.

@Override
public void handle(JSONObject packet) {
    if (packet.has("unavailable") && packet.getBoolean("unavailable")) {
        return;
    }
    final ImplServer server = (ImplServer) api.getServerById(packet.getString("id"));
    String name = packet.getString("name");
    if (!server.getName().equals(name)) {
        final String oldName = server.getName();
        server.setName(name);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<ServerChangeNameListener> listeners = api.getListeners(ServerChangeNameListener.class);
                synchronized (listeners) {
                    for (ServerChangeNameListener listener : listeners) {
                        try {
                            listener.onServerChangeName(api, server, oldName);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in ServerChangeNameListener!", t);
                        }
                    }
                }
            }
        });
    }
    Region region = Region.getRegionByKey(packet.getString("region"));
    if (server.getRegion() != region) {
        final Region oldRegion = server.getRegion();
        server.setRegion(region);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<ServerChangeRegionListener> listeners = api.getListeners(ServerChangeRegionListener.class);
                synchronized (listeners) {
                    for (ServerChangeRegionListener listener : listeners) {
                        try {
                            listener.onServerChangeRegion(api, server, oldRegion);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in ServerChangeRegionListener!", t);
                        }
                    }
                }
            }
        });
    }
    String ownerId = packet.getString("owner_id");
    if (!server.getOwnerId().equals(ownerId)) {
        final String oldOwnerId = server.getOwnerId();
        server.setOwnerId(ownerId);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<ServerChangeOwnerListener> listeners = api.getListeners(ServerChangeOwnerListener.class);
                synchronized (listeners) {
                    for (ServerChangeOwnerListener listener : listeners) {
                        try {
                            listener.onServerChangeOwner(api, server, oldOwnerId);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in ServerChangeOwnerListener!", t);
                        }
                    }
                }
            }
        });
    }
    String icon = packet.isNull("icon") ? null : packet.getString("icon");
    if (server.getIconHash() != null && !server.getIconHash().equals(icon)) {
        final String oldIcon = server.getIconHash();
        server.setIconHash(icon);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<ServerChangeIconListener> listeners = api.getListeners(ServerChangeIconListener.class);
                synchronized (listeners) {
                    for (ServerChangeIconListener listener : listeners) {
                        try {
                            listener.onServerChangeIcon(api, server, oldIcon);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in ServerChangeIconListener!", t);
                        }
                    }
                }
            }
        });
    } else if (server.getIconHash() == null && server.getIconHash() != icon) {
        final String oldIcon = server.getIconHash();
        server.setIconHash(icon);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<ServerChangeIconListener> listeners = api.getListeners(ServerChangeIconListener.class);
                synchronized (listeners) {
                    for (ServerChangeIconListener listener : listeners) {
                        try {
                            listener.onServerChangeIcon(api, server, oldIcon);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in ServerChangeIconListener!", t);
                        }
                    }
                }
            }
        });
    }
}
Also used : ImplServer(de.btobastian.javacord.entities.impl.ImplServer) ServerChangeIconListener(de.btobastian.javacord.listener.server.ServerChangeIconListener) Region(de.btobastian.javacord.entities.Region) List(java.util.List) ServerChangeNameListener(de.btobastian.javacord.listener.server.ServerChangeNameListener) ServerChangeRegionListener(de.btobastian.javacord.listener.server.ServerChangeRegionListener) ServerChangeOwnerListener(de.btobastian.javacord.listener.server.ServerChangeOwnerListener)

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