Search in sources :

Example 6 with User

use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.

the class GuildMemberRemoveHandler method handle.

@Override
public void handle(JsonNode packet) {
    api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(server -> (ServerImpl) server).ifPresent(server -> {
        User user = new UserImpl(api, packet.get("user"), (MemberImpl) null, server);
        server.removeMember(user.getId());
        server.decrementMemberCount();
        ServerMemberLeaveEvent event = new ServerMemberLeaveEventImpl(server, user);
        api.getEventDispatcher().dispatchServerMemberLeaveEvent(server, server, user, event);
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) User(org.javacord.api.entity.user.User) ServerMemberLeaveEvent(org.javacord.api.event.server.member.ServerMemberLeaveEvent) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) UserImpl(org.javacord.core.entity.user.UserImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) ServerMemberLeaveEventImpl(org.javacord.core.event.server.member.ServerMemberLeaveEventImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) User(org.javacord.api.entity.user.User) ServerMemberLeaveEvent(org.javacord.api.event.server.member.ServerMemberLeaveEvent) ServerImpl(org.javacord.core.entity.server.ServerImpl) UserImpl(org.javacord.core.entity.user.UserImpl) ServerMemberLeaveEventImpl(org.javacord.core.event.server.member.ServerMemberLeaveEventImpl)

Example 7 with User

use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.

the class DiscordApi method makeMentionsReadable.

/**
 * Gets the readable content of the string, which replaces all mentions etc. with the actual name.
 * The replacement happens as following:
 * <ul>
 * <li><b>User mentions</b>:
 * <code>@nickname</code> if the user has a nickname, <code>@name</code> if the user has no nickname, unchanged if
 * the user is not in the cache.
 * <li><b>Role mentions</b>:
 * <code>@name</code> if the role exists in the server, otherwise <code>#deleted-role</code>
 * <li><b>Channel mentions</b>:
 * <code>#name</code> if the text channel exists in the server, otherwise <code>#deleted-channel</code>
 * <li><b>Custom emoji</b>:
 * <code>:name:</code>. If the emoji is known, the real name is used, otherwise the name from the mention tag.
 * </ul>
 *
 * @param content The string to strip the mentions away.
 * @param server The server to get the display name of users from.
 *
 * @return The readable content of the string.
 */
default String makeMentionsReadable(String content, Server server) {
    Matcher userMention = DiscordRegexPattern.USER_MENTION.matcher(content);
    while (userMention.find()) {
        String userId = userMention.group("id");
        Optional<User> userOptional = getCachedUserById(userId);
        if (userOptional.isPresent()) {
            User user = userOptional.get();
            String userName = server == null ? user.getName() : server.getDisplayName(user);
            content = userMention.replaceFirst(Matcher.quoteReplacement("@" + userName));
            userMention.reset(content);
        }
    }
    Matcher roleMention = DiscordRegexPattern.ROLE_MENTION.matcher(content);
    while (roleMention.find()) {
        String roleName = getRoleById(roleMention.group("id")).map(Role::getName).orElse("deleted-role");
        content = roleMention.replaceFirst(Matcher.quoteReplacement("@" + roleName));
        roleMention.reset(content);
    }
    Matcher channelMention = DiscordRegexPattern.CHANNEL_MENTION.matcher(content);
    while (channelMention.find()) {
        String channelId = channelMention.group("id");
        String channelName = getServerChannelById(channelId).map(ServerChannel::getName).orElse("deleted-channel");
        content = channelMention.replaceFirst("#" + channelName);
        channelMention.reset(content);
    }
    Matcher customEmoji = DiscordRegexPattern.CUSTOM_EMOJI.matcher(content);
    while (customEmoji.find()) {
        String emojiId = customEmoji.group("id");
        String name = getCustomEmojiById(emojiId).map(CustomEmoji::getName).orElseGet(() -> customEmoji.group("name"));
        content = customEmoji.replaceFirst(":" + name + ":");
        customEmoji.reset(content);
    }
    return ESCAPED_CHARACTER.matcher(content).replaceAll("${char}");
}
Also used : User(org.javacord.api.entity.user.User) Matcher(java.util.regex.Matcher)

Example 8 with User

use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.

the class DiscordWebSocketAdapter method sendVoiceStateUpdate.

/**
 * Sends the voice state update packet.
 *
 * @param server The server to send the voice state update for. Can be {@code null} if {@code channel} is given.
 * @param channel The channel to connect to or {@code null} to disconnect from voice.
 * @param selfMuted Whether to self-mute on the given server. If {@code null}, current state remains unchanged.
 * @param selfDeafened Whether to self-deafen on the given server. If {@code null}, current state remains unchanged.
 */
public void sendVoiceStateUpdate(Server server, ServerVoiceChannel channel, Boolean selfMuted, Boolean selfDeafened) {
    ObjectNode updateVoiceStatePacket = JsonNodeFactory.instance.objectNode().put("op", GatewayOpcode.VOICE_STATE_UPDATE.getCode());
    if (server == null) {
        if (channel == null) {
            throw new IllegalArgumentException("Either server or channel must be given");
        }
        server = channel.getServer();
    }
    User yourself = api.getYourself();
    updateVoiceStatePacket.putObject("d").put("guild_id", server.getIdAsString()).put("channel_id", (channel == null) ? null : channel.getIdAsString()).put("self_mute", (selfMuted == null) ? server.isSelfMuted(yourself) : selfMuted).put("self_deaf", (selfDeafened == null) ? server.isSelfDeafened(yourself) : selfDeafened);
    logger.debug("Sending VOICE_STATE_UPDATE packet for {} on {}", channel, server);
    sendTextFrame(updateVoiceStatePacket.toString());
}
Also used : User(org.javacord.api.entity.user.User) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode)

Example 9 with User

use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.

the class ChannelUpdateHandler method dispatchServerChannelChangeOverwrittenPermissionsEvent.

/**
 * Dispatches a ServerChannelChangeOverwrittenPermissionsEvent.
 *
 * @param channel        The channel of the event.
 * @param newPermissions The new overwritten permissions.
 * @param oldPermissions The old overwritten permissions.
 * @param entityId       The id of the entity.
 * @param entity         The entity of the event.
 */
private void dispatchServerChannelChangeOverwrittenPermissionsEvent(ServerChannel channel, Permissions newPermissions, Permissions oldPermissions, long entityId, DiscordEntity entity) {
    if (newPermissions.equals(oldPermissions)) {
        // any of its values. We don't need to dispatch an event for this.
        return;
    }
    ServerChannelChangeOverwrittenPermissionsEvent event = new ServerChannelChangeOverwrittenPermissionsEventImpl(channel, newPermissions, oldPermissions, entityId, entity);
    api.getEventDispatcher().dispatchServerChannelChangeOverwrittenPermissionsEvent((DispatchQueueSelector) channel.getServer(), (entity instanceof Role) ? (Role) entity : null, channel.getServer(), channel, (entity instanceof User) ? (User) entity : null, event);
}
Also used : Role(org.javacord.api.entity.permission.Role) User(org.javacord.api.entity.user.User) ServerChannelChangeOverwrittenPermissionsEventImpl(org.javacord.core.event.channel.server.ServerChannelChangeOverwrittenPermissionsEventImpl) ServerChannelChangeOverwrittenPermissionsEvent(org.javacord.api.event.channel.server.ServerChannelChangeOverwrittenPermissionsEvent)

Example 10 with User

use of org.javacord.api.entity.user.User in project Javacord by BtoBastian.

the class GuildBanRemoveHandler method handle.

@Override
public void handle(JsonNode packet) {
    api.getPossiblyUnreadyServerById(packet.get("guild_id").asLong()).map(server -> (ServerImpl) server).ifPresent(server -> {
        User user = new UserImpl(api, packet.get("user"), (MemberImpl) null, server);
        ServerMemberUnbanEvent event = new ServerMemberUnbanEventImpl(server, user);
        api.getEventDispatcher().dispatchServerMemberUnbanEvent(server, server, user, event);
    });
}
Also used : ServerImpl(org.javacord.core.entity.server.ServerImpl) ServerMemberUnbanEventImpl(org.javacord.core.event.server.member.ServerMemberUnbanEventImpl) User(org.javacord.api.entity.user.User) PacketHandler(org.javacord.core.util.gateway.PacketHandler) DiscordApi(org.javacord.api.DiscordApi) UserImpl(org.javacord.core.entity.user.UserImpl) MemberImpl(org.javacord.core.entity.user.MemberImpl) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServerMemberUnbanEvent(org.javacord.api.event.server.member.ServerMemberUnbanEvent) ServerMemberUnbanEventImpl(org.javacord.core.event.server.member.ServerMemberUnbanEventImpl) User(org.javacord.api.entity.user.User) ServerImpl(org.javacord.core.entity.server.ServerImpl) UserImpl(org.javacord.core.entity.user.UserImpl) ServerMemberUnbanEvent(org.javacord.api.event.server.member.ServerMemberUnbanEvent)

Aggregations

User (org.javacord.api.entity.user.User)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 DiscordApi (org.javacord.api.DiscordApi)6 ServerImpl (org.javacord.core.entity.server.ServerImpl)6 PacketHandler (org.javacord.core.util.gateway.PacketHandler)6 MemberImpl (org.javacord.core.entity.user.MemberImpl)5 UserImpl (org.javacord.core.entity.user.UserImpl)5 Role (org.javacord.api.entity.permission.Role)4 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 DiscordEntity (org.javacord.api.entity.DiscordEntity)3 ServerTextChannel (org.javacord.api.entity.channel.ServerTextChannel)3 Permissions (org.javacord.api.entity.permission.Permissions)3 MessageCacheImpl (org.javacord.core.util.cache.MessageCacheImpl)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2