Example usage for java.lang Long parseUnsignedLong

List of usage examples for java.lang Long parseUnsignedLong

Introduction

In this page you can find the example usage for java.lang Long parseUnsignedLong.

Prototype

public static long parseUnsignedLong(String s) throws NumberFormatException 

Source Link

Document

Parses the string argument as an unsigned decimal long .

Usage

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Gets the attachments on a message.//from  ww  w.  j av a  2s.  c  o  m
 *
 * @param json The json response to use.
 * @return The attachments.
 */
public static List<IMessage.Attachment> getAttachmentsFromJSON(MessageObject json) {
    List<IMessage.Attachment> attachments = new ArrayList<>();
    if (json.attachments != null)
        for (MessageObject.AttachmentObject response : json.attachments) {
            attachments.add(new IMessage.Attachment(response.filename, response.size,
                    Long.parseUnsignedLong(response.id), response.url));
        }

    return attachments;
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Converts a json {@link GuildObject} to a {@link IGuild}. This method first checks the internal guild cache and returns
 * that object with updated information if it exists. Otherwise, it constructs a new guild.
 *
 * @param shard The shard the guild belongs to.
 * @param json The json object representing the guild.
 * @return The converted guild object.//w w  w. ja  va2  s . c  o m
 */
public static IGuild getGuildFromJSON(IShard shard, GuildObject json) {
    Guild guild;

    long guildId = Long.parseUnsignedLong(json.id);
    long systemChannelId = json.system_channel_id == null ? 0L : Long.parseUnsignedLong(json.system_channel_id);

    if ((guild = (Guild) shard.getGuildByID(guildId)) != null) {
        guild.setIcon(json.icon);
        guild.setName(json.name);
        guild.setOwnerID(Long.parseUnsignedLong(json.owner_id));
        guild.setAFKChannel(json.afk_channel_id == null ? 0 : Long.parseUnsignedLong(json.afk_channel_id));
        guild.setAfkTimeout(json.afk_timeout);
        guild.setRegionID(json.region);
        guild.setVerificationLevel(json.verification_level);
        guild.setTotalMemberCount(json.member_count);
        guild.setSystemChannelId(systemChannelId);

        List<IRole> newRoles = new ArrayList<>();
        for (RoleObject roleResponse : json.roles) {
            newRoles.add(getRoleFromJSON(guild, roleResponse));
        }
        guild.roles.clear();
        guild.roles.putAll(newRoles);

        for (IUser user : guild.getUsers()) { //Removes all deprecated roles
            for (IRole role : user.getRolesForGuild(guild)) {
                if (guild.getRoleByID(role.getLongID()) == null) {
                    user.getRolesForGuild(guild).remove(role);
                }
            }
        }
    } else {
        guild = new Guild(shard, json.name, guildId, json.icon, Long.parseUnsignedLong(json.owner_id),
                json.afk_channel_id == null ? 0 : Long.parseUnsignedLong(json.afk_channel_id), json.afk_timeout,
                json.region, json.verification_level, systemChannelId);

        if (json.roles != null)
            for (RoleObject roleResponse : json.roles) {
                getRoleFromJSON(guild, roleResponse); //Implicitly adds the role to the guild.
            }

        guild.setTotalMemberCount(json.member_count);
        if (json.members != null) {
            for (MemberObject member : json.members) {
                IUser user = getUserFromGuildMemberResponse(guild, member);
                guild.users.put(user);
            }
        }

        if (json.presences != null)
            for (PresenceObject presence : json.presences) {
                User user = (User) guild.getUserByID(Long.parseUnsignedLong(presence.user.id));
                if (user != null) {
                    user.setPresence(DiscordUtils.getPresenceFromJSON(presence));
                }
            }

        if (json.channels != null)
            for (ChannelObject channelJSON : json.channels) {
                IChannel channel = getChannelFromJSON(shard, guild, channelJSON);
                if (channelJSON.type == ChannelObject.Type.GUILD_TEXT) {
                    guild.channels.put(channel);
                } else if (channelJSON.type == ChannelObject.Type.GUILD_VOICE) {
                    guild.voiceChannels.put((IVoiceChannel) channel);
                } else if (channelJSON.type == ChannelObject.Type.GUILD_CATEGORY) {
                    guild.categories.put(DiscordUtils.getCategoryFromJSON(shard, guild, channelJSON));
                }
            }

        if (json.voice_states != null) {
            for (VoiceStateObject voiceState : json.voice_states) {
                final AtomicReference<IUser> user = new AtomicReference<>(
                        guild.getUserByID(Long.parseUnsignedLong(voiceState.user_id)));
                if (user.get() == null) {
                    new RequestBuilder(shard.getClient()).shouldBufferRequests(true).doAction(() -> {
                        if (user.get() == null)
                            user.set(shard.fetchUser(Long.parseUnsignedLong(voiceState.user_id)));
                        return true;
                    }).execute();
                }
                if (user.get() != null)
                    ((User) user.get()).voiceStates.put(DiscordUtils.getVoiceStateFromJson(guild, voiceState));
            }
        }
    }

    // emoji are always updated
    guild.emojis.clear();
    for (EmojiObject obj : json.emojis) {
        guild.emojis.put(DiscordUtils.getEmojiFromJSON(guild, obj));
    }

    return guild;
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Converts a json {@link MemberObject} to a {@link IUser}. This method uses {@link #getUserFromJSON(IShard, UserObject)}
 * to get or create a {@link IUser} and then updates the guild's appropriate member caches for that user.
 *
 * @param guild The guild the member belongs to.
 * @param json The json object representing the member.
 * @return The converted user object.//w w  w .ja va 2s  .co  m
 */
public static IUser getUserFromGuildMemberResponse(IGuild guild, MemberObject json) {
    User user = getUserFromJSON(guild.getShard(), json.user);
    for (String role : json.roles) {
        Role roleObj = (Role) guild.getRoleByID(Long.parseUnsignedLong(role));
        if (roleObj != null && !user.getRolesForGuild(guild).contains(roleObj))
            user.addRole(guild.getLongID(), roleObj);
    }
    user.addRole(guild.getLongID(), guild.getRoleByID(guild.getLongID())); //@everyone role
    user.addNick(guild.getLongID(), json.nick);

    VoiceState voiceState = (VoiceState) user.getVoiceStateForGuild(guild);
    voiceState.setDeafened(json.deaf);
    voiceState.setMuted(json.mute);

    ((Guild) guild).joinTimes
            .put(new Guild.TimeStampHolder(user.getLongID(), convertFromTimestamp(json.joined_at)));
    return user;
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Converts a json {@link MessageObject} to a {@link IMessage}. This method first checks the internal message cache
 * and returns that object with updated information if it exists. Otherwise, it constructs a new message.
 *
 * @param channel The channel the message belongs to.
 * @param json The json object representing the message.
 * @return The converted message object.
 *//*from w  ww .j  a  v a 2  s. c o m*/
public static IMessage getMessageFromJSON(Channel channel, MessageObject json) {
    if (json == null)
        return null;

    if (channel.messages.containsKey(json.id)) {
        Message message = (Message) channel.getMessageByID(Long.parseUnsignedLong(json.id));
        message.setAttachments(getAttachmentsFromJSON(json));
        message.setEmbeds(getEmbedsFromJSON(json));
        message.setContent(json.content);
        message.setMentionsEveryone(json.mention_everyone);
        message.setMentions(getMentionsFromJSON(json), getRoleMentionsFromJSON(json));
        message.setTimestamp(convertFromTimestamp(json.timestamp));
        message.setEditedTimestamp(
                json.edited_timestamp == null ? null : convertFromTimestamp(json.edited_timestamp));
        message.setPinned(Boolean.TRUE.equals(json.pinned));
        message.setChannelMentions();

        return message;
    } else {
        long authorId = Long.parseUnsignedLong(json.author.id);
        IGuild guild = channel.isPrivate() ? null : channel.getGuild();
        IUser author = guild == null ? getUserFromJSON(channel.getShard(), json.author)
                : guild.getUsers().stream().filter(it -> it.getLongID() == authorId).findAny()
                        .orElseGet(() -> getUserFromJSON(channel.getShard(), json.author));

        IMessage.Type type = Arrays.stream(IMessage.Type.values()).filter(t -> t.getValue() == json.type)
                .findFirst().orElse(IMessage.Type.UNKNOWN);

        Message message = new Message(channel.getClient(), Long.parseUnsignedLong(json.id), json.content,
                author, channel, convertFromTimestamp(json.timestamp),
                json.edited_timestamp == null ? null : convertFromTimestamp(json.edited_timestamp),
                json.mention_everyone, getMentionsFromJSON(json), getRoleMentionsFromJSON(json),
                getAttachmentsFromJSON(json), Boolean.TRUE.equals(json.pinned), getEmbedsFromJSON(json),
                json.webhook_id != null ? Long.parseUnsignedLong(json.webhook_id) : 0, type);
        message.setReactions(getReactionsFromJSON(message, json.reactions));

        return message;
    }
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Updates a {@link IMessage} object with the non-null or non-empty contents of a json {@link MessageObject}.
 *
 * @param client The client this message belongs to.
 * @param toUpdate The message to update.
 * @param json The json object representing the message.
 * @return The updated message object./*from   w  w  w  .  j  a  v  a 2s  .  co m*/
 */
public static IMessage getUpdatedMessageFromJSON(IDiscordClient client, IMessage toUpdate, MessageObject json) {
    if (toUpdate == null) {
        Channel channel = (Channel) client.getChannelByID(Long.parseUnsignedLong(json.channel_id));
        return channel == null ? null : channel.getMessageByID(Long.parseUnsignedLong(json.id));
    }

    Message message = (Message) toUpdate;
    List<IMessage.Attachment> attachments = getAttachmentsFromJSON(json);
    List<Embed> embeds = getEmbedsFromJSON(json);
    if (!attachments.isEmpty())
        message.setAttachments(attachments);
    if (!embeds.isEmpty())
        message.setEmbeds(embeds);
    if (json.content != null) {
        message.setContent(json.content);
        message.setMentions(getMentionsFromJSON(json), getRoleMentionsFromJSON(json));
        message.setMentionsEveryone(json.mention_everyone);
        message.setChannelMentions();
    }
    if (json.timestamp != null)
        message.setTimestamp(convertFromTimestamp(json.timestamp));
    if (json.edited_timestamp != null)
        message.setEditedTimestamp(convertFromTimestamp(json.edited_timestamp));
    if (json.pinned != null)
        message.setPinned(json.pinned);

    return message;
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Converts a json {@link WebhookObject} to a {@link IWebhook}. This method first checks the internal webhook cache
 * and returns that object with updated information if it exists. Otherwise, it constructs a new webhook.
 *
 * @param channel The channel the webhook belongs to.
 * @param json The json object representing the webhook.
 * @return The converted webhook object.
 *//*from   ww w  .  j  ava  2 s . co m*/
public static IWebhook getWebhookFromJSON(IChannel channel, WebhookObject json) {
    long webhookId = Long.parseUnsignedLong(json.id);
    if (channel.getWebhookByID(webhookId) != null) {
        Webhook webhook = (Webhook) channel.getWebhookByID(webhookId);
        webhook.setName(json.name);
        webhook.setAvatar(json.avatar);

        return webhook;
    } else {
        long userId = Long.parseUnsignedLong(json.user.id);
        IUser author = channel.getGuild().getUsers().stream().filter(it -> it.getLongID() == userId).findAny()
                .orElseGet(() -> getUserFromJSON(channel.getShard(), json.user));
        return new Webhook(channel.getClient(), json.name, Long.parseUnsignedLong(json.id), channel, author,
                json.avatar, json.token);
    }
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Converts a json {@link ChannelObject} to a {@link IChannel}. This method first checks the internal channel cache
 * and returns that object with updated information if it exists. Otherwise, it constructs a new channel.
 *
 * @param shard The shard the channel belongs to.
 * @param json The json object representing the channel.
 * @return The converted channel object.
 *//*  w w  w .  ja v  a2 s  .c o  m*/
public static IChannel getChannelFromJSON(IShard shard, IGuild guild, ChannelObject json) {
    DiscordClientImpl client = (DiscordClientImpl) shard.getClient();
    long id = Long.parseUnsignedLong(json.id);
    Channel channel = (Channel) shard.getChannelByID(id);
    if (channel == null)
        channel = (Channel) shard.getVoiceChannelByID(id);

    if (json.type == ChannelObject.Type.PRIVATE) {
        if (channel == null) {
            User recipient = getUserFromJSON(shard, json.recipients[0]);
            channel = new PrivateChannel(client, recipient, id);
        }
    } else if (json.type == ChannelObject.Type.GUILD_TEXT || json.type == ChannelObject.Type.GUILD_VOICE) {
        Pair<Cache<PermissionOverride>, Cache<PermissionOverride>> overrides = getPermissionOverwritesFromJSONs(
                client, json.permission_overwrites);
        long categoryID = json.parent_id == null ? 0L : Long.parseUnsignedLong(json.parent_id);

        if (channel != null) {
            channel.setName(json.name);
            channel.setPosition(json.position);
            channel.setNSFW(json.nsfw);
            channel.userOverrides.clear();
            channel.roleOverrides.clear();
            channel.userOverrides.putAll(overrides.getLeft());
            channel.roleOverrides.putAll(overrides.getRight());
            channel.setCategoryID(categoryID);

            if (json.type == ChannelObject.Type.GUILD_TEXT) {
                channel.setTopic(json.topic);
            } else {
                VoiceChannel vc = (VoiceChannel) channel;
                vc.setUserLimit(json.user_limit);
                vc.setBitrate(json.bitrate);
            }
        } else if (json.type == ChannelObject.Type.GUILD_TEXT) {
            channel = new Channel(client, json.name, id, guild, json.topic, json.position, json.nsfw,
                    categoryID, overrides.getRight(), overrides.getLeft());
        } else if (json.type == ChannelObject.Type.GUILD_VOICE) {
            channel = new VoiceChannel(client, json.name, id, guild, json.topic, json.position, json.nsfw,
                    json.user_limit, json.bitrate, categoryID, overrides.getRight(), overrides.getLeft());
        }
    }

    return channel;
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Converts an array of json {@link OverwriteObject}s to sets of user and role overrides.
 *
 * @param overwrites The array of json overwrite objects.
 * @return A pair representing the overwrites per id; left value = user overrides and right value = role overrides.
 *///from   w ww .j a v a  2s  .c  om
public static Pair<Cache<PermissionOverride>, Cache<PermissionOverride>> getPermissionOverwritesFromJSONs(
        DiscordClientImpl client, OverwriteObject[] overwrites) {
    Cache<PermissionOverride> userOverrides = new Cache<>(client, PermissionOverride.class);
    Cache<PermissionOverride> roleOverrides = new Cache<>(client, PermissionOverride.class);

    for (OverwriteObject overrides : overwrites) {
        if (overrides.type.equalsIgnoreCase("role")) {
            roleOverrides
                    .put(new PermissionOverride(Permissions.getAllowedPermissionsForNumber(overrides.allow),
                            Permissions.getDeniedPermissionsForNumber(overrides.deny),
                            Long.parseUnsignedLong(overrides.id)));
        } else if (overrides.type.equalsIgnoreCase("member")) {
            userOverrides
                    .put(new PermissionOverride(Permissions.getAllowedPermissionsForNumber(overrides.allow),
                            Permissions.getDeniedPermissionsForNumber(overrides.deny),
                            Long.parseUnsignedLong(overrides.id)));
        } else {
            Discord4J.LOGGER.warn(LogMarkers.API, "Unknown permissions overwrite type \"{}\"!", overrides.type);
        }
    }

    return Pair.of(userOverrides, roleOverrides);
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Converts a json {@link RoleObject} to a {@link IRole}. This method first checks the internal role cache
 * and returns that object with updated information if it exists. Otherwise, it constructs a new role.
 *
 * @param guild The guild the role belongs to.
 * @param json The json object representing the role.
 * @return The converted role object.// w  w  w  .jav  a2s  . com
 */
public static IRole getRoleFromJSON(IGuild guild, RoleObject json) {
    Role role;
    if ((role = (Role) guild.getRoleByID(Long.parseUnsignedLong(json.id))) != null) {
        role.setColor(json.color);
        role.setHoist(json.hoist);
        role.setName(json.name);
        role.setPermissions(json.permissions);
        role.setPosition(json.position);
        role.setMentionable(json.mentionable);
    } else {
        role = new Role(json.position, json.permissions, json.name, json.managed,
                Long.parseUnsignedLong(json.id), json.hoist, json.color, json.mentionable, guild);
        ((Guild) guild).roles.put(role);
    }
    return role;
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * Converts a json {@link VoiceStateObject} to a {@link IVoiceState}.
 *
 * @param guild The guild the voice state is in.
 * @param json The json object representing the voice state.
 * @return The converted voice state object.
 *//*from  ww  w.  j ava2  s. c  om*/
public static IVoiceState getVoiceStateFromJson(IGuild guild, VoiceStateObject json) {
    IVoiceChannel channel = json.channel_id != null
            ? guild.getVoiceChannelByID(Long.parseUnsignedLong(json.channel_id))
            : null;
    return new VoiceState(guild, channel, guild.getUserByID(Long.parseUnsignedLong(json.user_id)),
            json.session_id, json.deaf, json.mute, json.self_deaf, json.self_mute, json.suppress);
}