Search in sources :

Example 1 with Member

use of org.javacord.core.entity.user.Member in project Javacord by BtoBastian.

the class ServerImpl method addAndGetMembers.

/**
 * Adds members to the server and returns the added members.
 *
 * @param membersJson An array of guild member objects.
 * @return The added members.
 */
public List<Member> addAndGetMembers(JsonNode membersJson) {
    List<Member> members = new ArrayList<>();
    for (JsonNode memberJson : membersJson) {
        Member member = addMember(memberJson);
        members.add(member);
    }
    return members;
}
Also used : ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Member(org.javacord.core.entity.user.Member)

Example 2 with Member

use of org.javacord.core.entity.user.Member in project Javacord by BtoBastian.

the class MessageReactionAddHandler method handle.

@Override
public void handle(JsonNode packet) {
    long channelId = packet.get("channel_id").asLong();
    long messageId = packet.get("message_id").asLong();
    long userId = packet.get("user_id").asLong();
    String serverId = packet.hasNonNull("guild_id") ? packet.get("guild_id").asText() : null;
    TextChannel channel;
    if (serverId == null) {
        // if private channel:
        channel = PrivateChannelImpl.getOrCreatePrivateChannel(api, channelId, userId, null);
    } else {
        channel = api.getTextChannelById(channelId).orElse(null);
    }
    if (channel == null) {
        LoggerUtil.logMissingChannel(logger, channelId);
        return;
    }
    Optional<Server> server = api.getServerById(serverId);
    Member member = null;
    if (packet.hasNonNull("member") && server.isPresent()) {
        member = new MemberImpl(api, (ServerImpl) server.get(), packet.get("member"), null);
    }
    Optional<Message> message = api.getCachedMessageById(messageId);
    Emoji emoji;
    JsonNode emojiJson = packet.get("emoji");
    if (!emojiJson.has("id") || emojiJson.get("id").isNull()) {
        emoji = UnicodeEmojiImpl.fromString(emojiJson.get("name").asText());
    } else {
        emoji = api.getKnownCustomEmojiOrCreateCustomEmoji(emojiJson);
    }
    message.ifPresent(msg -> ((MessageImpl) msg).addReaction(emoji, userId == api.getYourself().getId()));
    ReactionAddEvent event = new ReactionAddEventImpl(api, messageId, channel, emoji, userId, member);
    api.getEventDispatcher().dispatchReactionAddEvent(server.map(DispatchQueueSelector.class::cast).orElse(api), messageId, server.orElse(null), channel, userId, event);
}
Also used : ReactionAddEventImpl(org.javacord.core.event.message.reaction.ReactionAddEventImpl) Server(org.javacord.api.entity.server.Server) Message(org.javacord.api.entity.message.Message) MemberImpl(org.javacord.core.entity.user.MemberImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ReactionAddEvent(org.javacord.api.event.message.reaction.ReactionAddEvent) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) TextChannel(org.javacord.api.entity.channel.TextChannel) ServerImpl(org.javacord.core.entity.server.ServerImpl) Emoji(org.javacord.api.entity.emoji.Emoji) Member(org.javacord.core.entity.user.Member)

Example 3 with Member

use of org.javacord.core.entity.user.Member in project Javacord by BtoBastian.

the class GuildMemberUpdateHandler method handle.

@Override
public void handle(JsonNode packet) {
    api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(server -> (ServerImpl) server).ifPresent(server -> {
        MemberImpl newMember = new MemberImpl(api, server, packet, null);
        Member oldMember = server.getRealMemberById(newMember.getId()).orElse(null);
        api.addMemberToCacheOrReplaceExisting(newMember);
        if (oldMember == null) {
            // Should only happen shortly after startup and is unproblematic
            return;
        }
        if (!newMember.getNickname().equals(oldMember.getNickname())) {
            UserChangeNicknameEvent event = new UserChangeNicknameEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeNicknameEvent(server, server, newMember.getUser(), event);
        }
        if (!newMember.getServerAvatarHash().equals(oldMember.getServerAvatarHash())) {
            UserChangeServerAvatarEvent event = new UserChangeServerAvatarEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeServerAvatarEvent(server, server, newMember.getUser(), event);
        }
        if (newMember.isPending() != oldMember.isPending()) {
            UserChangePendingEvent event = new UserChangePendingEventImpl(oldMember, newMember);
            api.getEventDispatcher().dispatchUserChangePendingEvent(server, server, newMember.getUser(), event);
        }
        if (packet.has("roles")) {
            JsonNode jsonRoles = packet.get("roles");
            Collection<Role> newRoles = new HashSet<>();
            Collection<Role> oldRoles = oldMember.getRoles();
            Collection<Role> intersection = new HashSet<>();
            for (JsonNode roleIdJson : jsonRoles) {
                api.getRoleById(roleIdJson.asText()).map(role -> {
                    newRoles.add(role);
                    return role;
                }).filter(oldRoles::contains).ifPresent(intersection::add);
            }
            // Added roles
            Collection<Role> addedRoles = new ArrayList<>(newRoles);
            addedRoles.removeAll(intersection);
            for (Role role : addedRoles) {
                if (role.isEveryoneRole()) {
                    continue;
                }
                UserRoleAddEvent event = new UserRoleAddEventImpl(role, newMember);
                api.getEventDispatcher().dispatchUserRoleAddEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), newMember.getId(), event);
            }
            // Removed roles
            Collection<Role> removedRoles = new ArrayList<>(oldRoles);
            removedRoles.removeAll(intersection);
            for (Role role : removedRoles) {
                if (role.isEveryoneRole()) {
                    continue;
                }
                UserRoleRemoveEvent event = new UserRoleRemoveEventImpl(role, newMember);
                api.getEventDispatcher().dispatchUserRoleRemoveEvent((DispatchQueueSelector) role.getServer(), role, role.getServer(), newMember.getId(), event);
            }
        }
        if (newMember.getUser().isYourself()) {
            Set<Long> unreadableChannels = server.getTextChannels().stream().filter(((Predicate<ServerTextChannel>) ServerTextChannel::canYouSee).negate()).map(ServerTextChannel::getId).collect(Collectors.toSet());
            api.forEachCachedMessageWhere(msg -> unreadableChannels.contains(msg.getChannel().getId()), msg -> {
                api.removeMessageFromCache(msg.getId());
                ((MessageCacheImpl) msg.getChannel().getMessageCache()).removeMessage(msg);
            });
        }
        // https://github.com/discord/discord-api-docs/pull/1307#issuecomment-581561519
        if (oldMember.getUser() != null) {
            UserImpl oldUser = (UserImpl) oldMember.getUser();
            boolean userChanged = false;
            UserImpl updatedUser = oldUser.replacePartialUserData(packet.get("user"));
            if (packet.get("user").has("username")) {
                String newName = packet.get("user").get("username").asText();
                String oldName = oldUser.getName();
                if (!oldName.equals(newName)) {
                    dispatchUserChangeNameEvent(updatedUser, newName, oldName);
                    userChanged = true;
                }
            }
            if (packet.get("user").has("discriminator")) {
                String newDiscriminator = packet.get("user").get("discriminator").asText();
                String oldDiscriminator = oldUser.getDiscriminator();
                if (!oldDiscriminator.equals(newDiscriminator)) {
                    dispatchUserChangeDiscriminatorEvent(updatedUser, newDiscriminator, oldDiscriminator);
                    userChanged = true;
                }
            }
            if (packet.get("user").has("avatar")) {
                String newAvatarHash = packet.get("user").get("avatar").asText(null);
                String oldAvatarHash = oldUser.getAvatarHash().orElse(null);
                if (!Objects.deepEquals(newAvatarHash, oldAvatarHash)) {
                    dispatchUserChangeAvatarEvent(updatedUser, newAvatarHash, oldAvatarHash);
                    userChanged = true;
                }
            }
            if (userChanged) {
                api.updateUserOfAllMembers(updatedUser);
            }
        }
    });
}
Also used : UserChangeNameEventImpl(org.javacord.core.event.user.UserChangeNameEventImpl) Member(org.javacord.core.entity.user.Member) UserChangeServerAvatarEventImpl(org.javacord.core.event.user.UserChangeServerAvatarEventImpl) UserChangePendingEvent(org.javacord.api.event.user.UserChangePendingEvent) UserRoleRemoveEventImpl(org.javacord.core.event.server.role.UserRoleRemoveEventImpl) UserChangeAvatarEvent(org.javacord.api.event.user.UserChangeAvatarEvent) UserChangeAvatarEventImpl(org.javacord.core.event.user.UserChangeAvatarEventImpl) UserChangeNicknameEvent(org.javacord.api.event.user.UserChangeNicknameEvent) UserChangeServerAvatarEvent(org.javacord.api.event.user.UserChangeServerAvatarEvent) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl) UserRoleRemoveEvent(org.javacord.api.event.server.role.UserRoleRemoveEvent) UserChangePendingEventImpl(org.javacord.core.event.user.UserChangePendingEventImpl) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) JsonNode(com.fasterxml.jackson.databind.JsonNode) UserChangeDiscriminatorEventImpl(org.javacord.core.event.user.UserChangeDiscriminatorEventImpl) ServerImpl(org.javacord.core.entity.server.ServerImpl) Predicate(java.util.function.Predicate) UserImpl(org.javacord.core.entity.user.UserImpl) Collection(java.util.Collection) MemberImpl(org.javacord.core.entity.user.MemberImpl) Set(java.util.Set) Collectors(java.util.stream.Collectors) UserRoleAddEvent(org.javacord.api.event.server.role.UserRoleAddEvent) UserChangeNameEvent(org.javacord.api.event.user.UserChangeNameEvent) Objects(java.util.Objects) LoggerUtil(org.javacord.core.util.logging.LoggerUtil) Logger(org.apache.logging.log4j.Logger) User(org.javacord.api.entity.user.User) UserRoleAddEventImpl(org.javacord.core.event.server.role.UserRoleAddEventImpl) Role(org.javacord.api.entity.permission.Role) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) UserChangeNicknameEventImpl(org.javacord.core.event.user.UserChangeNicknameEventImpl) DispatchQueueSelector(org.javacord.core.util.event.DispatchQueueSelector) Collections(java.util.Collections) UserChangeDiscriminatorEvent(org.javacord.api.event.user.UserChangeDiscriminatorEvent) UserChangePendingEvent(org.javacord.api.event.user.UserChangePendingEvent) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) UserChangeNicknameEventImpl(org.javacord.core.event.user.UserChangeNicknameEventImpl) UserChangePendingEventImpl(org.javacord.core.event.user.UserChangePendingEventImpl) ServerImpl(org.javacord.core.entity.server.ServerImpl) UserRoleRemoveEvent(org.javacord.api.event.server.role.UserRoleRemoveEvent) UserImpl(org.javacord.core.entity.user.UserImpl) Member(org.javacord.core.entity.user.Member) HashSet(java.util.HashSet) UserRoleAddEventImpl(org.javacord.core.event.server.role.UserRoleAddEventImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) UserChangeServerAvatarEventImpl(org.javacord.core.event.user.UserChangeServerAvatarEventImpl) Role(org.javacord.api.entity.permission.Role) UserRoleAddEvent(org.javacord.api.event.server.role.UserRoleAddEvent) ServerTextChannel(org.javacord.api.entity.channel.ServerTextChannel) MessageCacheImpl(org.javacord.core.util.cache.MessageCacheImpl) UserChangeServerAvatarEvent(org.javacord.api.event.user.UserChangeServerAvatarEvent) UserChangeNicknameEvent(org.javacord.api.event.user.UserChangeNicknameEvent) UserRoleRemoveEventImpl(org.javacord.core.event.server.role.UserRoleRemoveEventImpl)

Example 4 with Member

use of org.javacord.core.entity.user.Member in project Javacord by BtoBastian.

the class GuildMembersChunkHandler method handle.

@Override
public void handle(JsonNode packet) {
    api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(ServerImpl.class::cast).ifPresent(server -> {
        List<Member> members = server.addAndGetMembers(packet.get("members"));
        ServerMembersChunkEventImpl event = new ServerMembersChunkEventImpl(server, members.stream().map(Member::getUser).collect(Collectors.toList()));
        api.getEventDispatcher().dispatchServerMembersChunkEvent(server, server, event);
    });
}
Also used : ServerMembersChunkEventImpl(org.javacord.core.event.server.member.ServerMembersChunkEventImpl) Member(org.javacord.core.entity.user.Member)

Example 5 with Member

use of org.javacord.core.entity.user.Member in project Javacord by BtoBastian.

the class VoiceStateUpdateHandler method handleServerVoiceChannel.

private void handleServerVoiceChannel(JsonNode packet, long userId) {
    api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(ServerImpl.class::cast).ifPresent(server -> {
        Member member = new MemberImpl(api, server, packet.get("member"), null);
        Optional<ServerVoiceChannelImpl> oldChannel = server.getConnectedVoiceChannel(userId).map(ServerVoiceChannelImpl.class::cast);
        Optional<ServerVoiceChannelImpl> newChannel;
        if (packet.hasNonNull("channel_id")) {
            newChannel = server.getVoiceChannelById(packet.get("channel_id").asLong()).map(ServerVoiceChannelImpl.class::cast);
        } else {
            newChannel = Optional.empty();
        }
        if (!newChannel.equals(oldChannel)) {
            oldChannel.ifPresent(channel -> {
                channel.removeConnectedUser(userId);
                dispatchServerVoiceChannelMemberLeaveEvent(member, newChannel.orElse(null), channel, server);
            });
            newChannel.ifPresent(channel -> {
                channel.addConnectedUser(userId);
                dispatchServerVoiceChannelMemberJoinEvent(member, channel, oldChannel.orElse(null), server);
            });
        }
        if (!packet.hasNonNull("member")) {
            logger.warn("Received VOICE_STATE_UPDATE packet without non-null member field: {}", packet);
            return;
        }
        MemberImpl newMember = new MemberImpl(api, server, packet.get("member"), null);
        Member oldMember = server.getRealMemberById(packet.get("user_id").asLong()).orElse(null);
        boolean newSelfMuted = packet.get("self_mute").asBoolean();
        boolean oldSelfMuted = server.isSelfMuted(userId);
        if (newSelfMuted != oldSelfMuted) {
            UserChangeSelfMutedEventImpl event = new UserChangeSelfMutedEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeSelfMutedEvent(server, server, newMember.getUser(), event);
        }
        boolean newSelfDeafened = packet.get("self_deaf").asBoolean();
        boolean oldSelfDeafened = server.isSelfDeafened(userId);
        if (newSelfDeafened != oldSelfDeafened) {
            UserChangeSelfDeafenedEventImpl event = new UserChangeSelfDeafenedEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeSelfDeafenedEvent(server, server, newMember.getUser(), event);
        }
        boolean newMuted = packet.get("mute").asBoolean();
        boolean oldMuted = server.isMuted(userId);
        if (newMuted != oldMuted) {
            server.setMuted(userId, newMuted);
            UserChangeMutedEventImpl event = new UserChangeMutedEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeMutedEvent(server, server, newMember.getUser(), event);
        }
        boolean newDeafened = packet.get("deaf").asBoolean();
        boolean oldDeafened = server.isDeafened(userId);
        if (newDeafened != oldDeafened) {
            server.setDeafened(userId, newDeafened);
            UserChangeDeafenedEventImpl event = new UserChangeDeafenedEventImpl(newMember, oldMember);
            api.getEventDispatcher().dispatchUserChangeDeafenedEvent(server, server, newMember.getUser(), event);
        }
    });
}
Also used : UserChangeSelfDeafenedEventImpl(org.javacord.core.event.user.UserChangeSelfDeafenedEventImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) UserChangeSelfMutedEventImpl(org.javacord.core.event.user.UserChangeSelfMutedEventImpl) UserChangeMutedEventImpl(org.javacord.core.event.user.UserChangeMutedEventImpl) Member(org.javacord.core.entity.user.Member) ServerVoiceChannelImpl(org.javacord.core.entity.channel.ServerVoiceChannelImpl) UserChangeDeafenedEventImpl(org.javacord.core.event.user.UserChangeDeafenedEventImpl)

Aggregations

Member (org.javacord.core.entity.user.Member)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 MemberImpl (org.javacord.core.entity.user.MemberImpl)3 ArrayList (java.util.ArrayList)2 ServerImpl (org.javacord.core.entity.server.ServerImpl)2 DispatchQueueSelector (org.javacord.core.util.event.DispatchQueueSelector)2 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Logger (org.apache.logging.log4j.Logger)1 DiscordApi (org.javacord.api.DiscordApi)1 ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)1 TextChannel (org.javacord.api.entity.channel.TextChannel)1 Emoji (org.javacord.api.entity.emoji.Emoji)1 Message (org.javacord.api.entity.message.Message)1 Role (org.javacord.api.entity.permission.Role)1