Search in sources :

Example 1 with User

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

the class ChannelUpdateHandler 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) {
        // no channel with the given id was found
        return;
    }
    String name = packet.getString("name");
    if (!channel.getName().equals(name)) {
        final String oldName = channel.getName();
        ((ImplVoiceChannel) channel).setName(name);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<VoiceChannelChangeNameListener> listeners = api.getListeners(VoiceChannelChangeNameListener.class);
                synchronized (listeners) {
                    for (VoiceChannelChangeNameListener listener : listeners) {
                        try {
                            listener.onVoiceChannelChangeName(api, channel, oldName);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in VoiceChannelChangeNameListener!", t);
                        }
                    }
                }
            }
        });
    }
    int position = packet.getInt("position");
    if (channel.getPosition() != position) {
        final int oldPosition = channel.getPosition();
        ((ImplVoiceChannel) channel).setPosition(position);
        listenerExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                List<VoiceChannelChangePositionListener> listeners = api.getListeners(VoiceChannelChangePositionListener.class);
                synchronized (listeners) {
                    for (VoiceChannelChangePositionListener listener : listeners) {
                        try {
                            listener.onVoiceChannelChangePosition(api, channel, oldPosition);
                        } catch (Throwable t) {
                            logger.warn("Uncaught exception in VoiceChannelChangePositionListener!", 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)) {
                ((ImplVoiceChannel) 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)) {
        ((ImplVoiceChannel) channel).setParentId(parentId);
    }
}
Also used : ImplRole(de.btobastian.javacord.entities.permissions.impl.ImplRole) VoiceChannelChangeNameListener(de.btobastian.javacord.listener.voicechannel.VoiceChannelChangeNameListener) User(de.btobastian.javacord.entities.User) VoiceChannelChangePositionListener(de.btobastian.javacord.listener.voicechannel.VoiceChannelChangePositionListener) ImplPermissions(de.btobastian.javacord.entities.permissions.impl.ImplPermissions) JSONArray(org.json.JSONArray) ImplVoiceChannel(de.btobastian.javacord.entities.impl.ImplVoiceChannel) 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) Permissions(de.btobastian.javacord.entities.permissions.Permissions) ImplPermissions(de.btobastian.javacord.entities.permissions.impl.ImplPermissions) ImplVoiceChannel(de.btobastian.javacord.entities.impl.ImplVoiceChannel) VoiceChannel(de.btobastian.javacord.entities.VoiceChannel) List(java.util.List) UserChangeOverwrittenPermissionsListener(de.btobastian.javacord.listener.user.UserChangeOverwrittenPermissionsListener) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with User

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

the class MessageReactionAddHandler method handle.

@Override
public void handle(JSONObject packet) {
    String userId = packet.getString("user_id");
    String messageId = packet.getString("message_id");
    JSONObject emoji = packet.getJSONObject("emoji");
    boolean isCustomEmoji = !emoji.isNull("id");
    Message message = api.getMessageById(messageId);
    if (message == null) {
        return;
    }
    Reaction reaction = null;
    if (isCustomEmoji) {
        String emojiId = emoji.getString("id");
        if (message.isPrivateMessage()) {
            // Private messages with custom emoji? Maybe with Nitro, but there's no documentation so far.
            return;
        }
        CustomEmoji customEmoji = message.getChannelReceiver().getServer().getCustomEmojiById(emojiId);
        if (customEmoji == null) {
            // We don't know this emoji
            return;
        }
        reaction = ((ImplMessage) message).addCustomEmojiReactionToCache(customEmoji, api.getYourself().getId().equals(userId));
    } else {
        reaction = ((ImplMessage) message).addUnicodeReactionToCache(emoji.getString("name"), api.getYourself().getId().equals(userId));
    }
    if (reaction != null) {
        final User user = api.getCachedUserById(userId);
        if (user != null) {
            final Reaction reactionFinal = reaction;
            listenerExecutorService.submit(new Runnable() {

                @Override
                public void run() {
                    List<ReactionAddListener> listeners = api.getListeners(ReactionAddListener.class);
                    synchronized (listeners) {
                        for (ReactionAddListener listener : listeners) {
                            try {
                                listener.onReactionAdd(api, reactionFinal, user);
                            } catch (Throwable t) {
                                logger.warn("Uncaught exception in ReactionAddListener!", t);
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : CustomEmoji(de.btobastian.javacord.entities.CustomEmoji) User(de.btobastian.javacord.entities.User) JSONObject(org.json.JSONObject) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) ReactionAddListener(de.btobastian.javacord.listener.message.ReactionAddListener) List(java.util.List) Reaction(de.btobastian.javacord.entities.message.Reaction)

Example 3 with User

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

the class MessageReactionRemoveHandler method handle.

@Override
public void handle(JSONObject packet) {
    String userId = packet.getString("user_id");
    String messageId = packet.getString("message_id");
    JSONObject emoji = packet.getJSONObject("emoji");
    boolean isCustomEmoji = !emoji.isNull("id");
    Message message = api.getMessageById(messageId);
    if (message == null) {
        return;
    }
    Reaction reaction = null;
    if (isCustomEmoji) {
        String emojiId = emoji.getString("id");
        if (message.isPrivateMessage()) {
            // Private messages with custom emoji? Maybe with Nitro, but there's no documentation so far.
            return;
        }
        CustomEmoji customEmoji = message.getChannelReceiver().getServer().getCustomEmojiById(emojiId);
        if (customEmoji == null) {
            // We don't know this emoji
            return;
        }
        reaction = ((ImplMessage) message).removeCustomEmojiReactionToCache(customEmoji, api.getYourself().getId().equals(userId));
    } else {
        reaction = ((ImplMessage) message).removeUnicodeReactionToCache(emoji.getString("name"), api.getYourself().getId().equals(userId));
    }
    if (reaction != null) {
        final User user = api.getCachedUserById(userId);
        if (user != null) {
            final Reaction reactionFinal = reaction;
            listenerExecutorService.submit(new Runnable() {

                @Override
                public void run() {
                    List<ReactionRemoveListener> listeners = api.getListeners(ReactionRemoveListener.class);
                    synchronized (listeners) {
                        for (ReactionRemoveListener listener : listeners) {
                            try {
                                listener.onReactionRemove(api, reactionFinal, user);
                            } catch (Throwable t) {
                                logger.warn("Uncaught exception in ReactionRemoveListener!", t);
                            }
                        }
                    }
                }
            });
        }
    }
}
Also used : CustomEmoji(de.btobastian.javacord.entities.CustomEmoji) User(de.btobastian.javacord.entities.User) JSONObject(org.json.JSONObject) ImplMessage(de.btobastian.javacord.entities.message.impl.ImplMessage) Message(de.btobastian.javacord.entities.message.Message) List(java.util.List) Reaction(de.btobastian.javacord.entities.message.Reaction) ReactionRemoveListener(de.btobastian.javacord.listener.message.ReactionRemoveListener)

Example 4 with User

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

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

Aggregations

User (de.btobastian.javacord.entities.User)15 List (java.util.List)12 Server (de.btobastian.javacord.entities.Server)6 ImplServer (de.btobastian.javacord.entities.impl.ImplServer)6 JSONArray (org.json.JSONArray)6 ImplRole (de.btobastian.javacord.entities.permissions.impl.ImplRole)5 JSONObject (org.json.JSONObject)5 ImplUser (de.btobastian.javacord.entities.impl.ImplUser)4 Role (de.btobastian.javacord.entities.permissions.Role)4 ExecutionException (java.util.concurrent.ExecutionException)4 ImplVoiceChannel (de.btobastian.javacord.entities.impl.ImplVoiceChannel)3 Channel (de.btobastian.javacord.entities.Channel)2 CustomEmoji (de.btobastian.javacord.entities.CustomEmoji)2 VoiceChannel (de.btobastian.javacord.entities.VoiceChannel)2 Message (de.btobastian.javacord.entities.message.Message)2 Reaction (de.btobastian.javacord.entities.message.Reaction)2 ImplMessage (de.btobastian.javacord.entities.message.impl.ImplMessage)2 Permissions (de.btobastian.javacord.entities.permissions.Permissions)2 ImplPermissions (de.btobastian.javacord.entities.permissions.impl.ImplPermissions)2 RoleChangeOverwrittenPermissionsListener (de.btobastian.javacord.listener.role.RoleChangeOverwrittenPermissionsListener)2