Search in sources :

Example 1 with Channel

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

the class TypingStartHandler method handle.

@Override
public void handle(JSONObject packet) {
    Channel channelTemp = null;
    String channelId = packet.getString("channel_id");
    Iterator<Server> serverIterator = api.getServers().iterator();
    while (serverIterator.hasNext()) {
        channelTemp = serverIterator.next().getChannelById(channelId);
        if (channelTemp != null) {
            break;
        }
    }
    final Channel channel = channelTemp;
    String userId = packet.getString("user_id");
    final User user;
    try {
        user = api.getUserById(userId).get();
    } catch (InterruptedException | ExecutionException e) {
        return;
    }
    listenerExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            List<TypingStartListener> listeners = api.getListeners(TypingStartListener.class);
            synchronized (listeners) {
                for (TypingStartListener listener : listeners) {
                    try {
                        listener.onTypingStart(api, user, channel);
                    } catch (Throwable t) {
                        logger.warn("Uncaught exception in TypingStartListener!", t);
                    }
                }
            }
        }
    });
}
Also used : User(de.btobastian.javacord.entities.User) Server(de.btobastian.javacord.entities.Server) Channel(de.btobastian.javacord.entities.Channel) TypingStartListener(de.btobastian.javacord.listener.message.TypingStartListener) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with Channel

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

the class ChannelCreateHandler 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) {
    if (server.getChannelById(packet.getString("id")) != null) {
        return;
    }
    final Channel channel = new ImplChannel(packet, (ImplServer) server, api);
    listenerExecutorService.submit(new Runnable() {

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

Example 3 with Channel

use of de.btobastian.javacord.entities.Channel 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 4 with Channel

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

Aggregations

Channel (de.btobastian.javacord.entities.Channel)4 List (java.util.List)4 VoiceChannel (de.btobastian.javacord.entities.VoiceChannel)3 User (de.btobastian.javacord.entities.User)2 ImplChannel (de.btobastian.javacord.entities.impl.ImplChannel)2 ImplVoiceChannel (de.btobastian.javacord.entities.impl.ImplVoiceChannel)2 ExecutionException (java.util.concurrent.ExecutionException)2 Server (de.btobastian.javacord.entities.Server)1 ImplServer (de.btobastian.javacord.entities.impl.ImplServer)1 Permissions (de.btobastian.javacord.entities.permissions.Permissions)1 Role (de.btobastian.javacord.entities.permissions.Role)1 ImplPermissions (de.btobastian.javacord.entities.permissions.impl.ImplPermissions)1 ImplRole (de.btobastian.javacord.entities.permissions.impl.ImplRole)1 ChannelChangeNameListener (de.btobastian.javacord.listener.channel.ChannelChangeNameListener)1 ChannelChangePositionListener (de.btobastian.javacord.listener.channel.ChannelChangePositionListener)1 ChannelChangeTopicListener (de.btobastian.javacord.listener.channel.ChannelChangeTopicListener)1 ChannelCreateListener (de.btobastian.javacord.listener.channel.ChannelCreateListener)1 ChannelDeleteListener (de.btobastian.javacord.listener.channel.ChannelDeleteListener)1 TypingStartListener (de.btobastian.javacord.listener.message.TypingStartListener)1 RoleChangeOverwrittenPermissionsListener (de.btobastian.javacord.listener.role.RoleChangeOverwrittenPermissionsListener)1