Package net.dv8tion.jda.core.utils
Class MiscUtil
- java.lang.Object
-
- net.dv8tion.jda.core.utils.MiscUtil
-
public class MiscUtil extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description static long
DISCORD_EPOCH
static long
TIMESTAMP_OFFSET
-
Constructor Summary
Constructors Constructor Description MiscUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
appendTo(java.util.Formatter formatter, int width, int precision, boolean leftJustified, java.lang.String out)
Can be used to append a String to a formatter.static okhttp3.RequestBody
createRequestBody(okhttp3.MediaType contentType, java.io.InputStream stream)
Creates a new request body that transmits the providedInputStream
.static java.lang.String
encodeUTF8(java.lang.String chars)
URL-Encodes the given String to UTF-8 after form-data specifications (space -> +)static java.time.OffsetDateTime
getCreationTime(long entityId)
Gets the creation-time of a JDA-entity by doing the reverse snowflake algorithm on its id.static java.time.OffsetDateTime
getCreationTime(ISnowflake entity)
Gets the creation-time of a JDA-entity by doing the reverse snowflake algorithm on its id.static java.lang.String
getDateTimeString(java.time.OffsetDateTime time)
Returns a prettier String-representation of a OffsetDateTime objectstatic long
getDiscordTimestamp(long millisTimestamp)
Converts the provided epoch millisecond timestamp to a Discord Snowflake.static int
getShardForGuild(long guildId, int shards)
Returns the shard id the given guild will be loaded on for the given amount of shards.static int
getShardForGuild(java.lang.String guildId, int shards)
Returns the shard id the given guild will be loaded on for the given amount of shards.static int
getShardForGuild(Guild guild, int shards)
Returns the shard id the givenGuild
will be loaded on for the given amount of shards.static void
locked(java.util.concurrent.locks.ReentrantLock lock, java.lang.Runnable task)
static <E> E
locked(java.util.concurrent.locks.ReentrantLock lock, java.util.function.Supplier<E> task)
static <T> gnu.trove.map.TLongObjectMap<T>
newLongMap()
Generates a new thread-safeTLongObjectMap
static long
parseSnowflake(java.lang.String input)
-
-
-
Field Detail
-
DISCORD_EPOCH
public static final long DISCORD_EPOCH
- See Also:
- Constant Field Values
-
TIMESTAMP_OFFSET
public static final long TIMESTAMP_OFFSET
- See Also:
- Constant Field Values
-
-
Method Detail
-
getDiscordTimestamp
public static long getDiscordTimestamp(long millisTimestamp)
Converts the provided epoch millisecond timestamp to a Discord Snowflake.
This can be used as a marker/pivot forMessageHistory
creation.- Parameters:
millisTimestamp
- The epoch millis to convert- Returns:
- Shifted epoch millis for Discord
-
getCreationTime
public static java.time.OffsetDateTime getCreationTime(long entityId)
Gets the creation-time of a JDA-entity by doing the reverse snowflake algorithm on its id. This returns the creation-time of the actual entity on Discords side, not inside JDA.- Parameters:
entityId
- The id of the JDA entity where the creation-time should be determined for- Returns:
- The creation time of the JDA entity as OffsetDateTime
-
getCreationTime
public static java.time.OffsetDateTime getCreationTime(ISnowflake entity)
Gets the creation-time of a JDA-entity by doing the reverse snowflake algorithm on its id. This returns the creation-time of the actual entity on Discords side, not inside JDA.- Parameters:
entity
- The JDA entity where the creation-time should be determined for- Returns:
- The creation time of the JDA entity as OffsetDateTime
- Throws:
java.lang.IllegalArgumentException
- If the provided entity isnull
-
getDateTimeString
public static java.lang.String getDateTimeString(java.time.OffsetDateTime time)
Returns a prettier String-representation of a OffsetDateTime object- Parameters:
time
- The OffsetDateTime object to format- Returns:
- The String of the formatted OffsetDateTime
-
getShardForGuild
public static int getShardForGuild(long guildId, int shards)
Returns the shard id the given guild will be loaded on for the given amount of shards. Discord determines which guilds a shard is connect to using the following format:shardId == (guildId >>> 22) % totalShards
Source for formula: Discord Documentation- Parameters:
guildId
- The guild id.shards
- The amount of shards.- Returns:
- The shard id for the guild.
-
getShardForGuild
public static int getShardForGuild(java.lang.String guildId, int shards)
Returns the shard id the given guild will be loaded on for the given amount of shards. Discord determines which guilds a shard is connect to using the following format:shardId == (guildId >>> 22) % totalShards
Source for formula: Discord Documentation- Parameters:
guildId
- The guild id.shards
- The amount of shards.- Returns:
- The shard id for the guild.
-
getShardForGuild
public static int getShardForGuild(Guild guild, int shards)
Returns the shard id the givenGuild
will be loaded on for the given amount of shards. Discord determines which guilds a shard is connect to using the following format:shardId == (guildId >>> 22) % totalShards
Source for formula: Discord Documentation- Parameters:
guild
- The guild.shards
- The amount of shards.- Returns:
- The shard id for the guild.
-
newLongMap
public static <T> gnu.trove.map.TLongObjectMap<T> newLongMap()
Generates a new thread-safeTLongObjectMap
- Type Parameters:
T
- The Object type- Returns:
- a new thread-safe
TLongObjectMap
-
encodeUTF8
public static java.lang.String encodeUTF8(java.lang.String chars)
URL-Encodes the given String to UTF-8 after form-data specifications (space -> +)- Parameters:
chars
- The characters to encode- Returns:
- The encoded String
-
parseSnowflake
public static long parseSnowflake(java.lang.String input)
-
locked
public static <E> E locked(java.util.concurrent.locks.ReentrantLock lock, java.util.function.Supplier<E> task)
-
locked
public static void locked(java.util.concurrent.locks.ReentrantLock lock, java.lang.Runnable task)
-
appendTo
public static void appendTo(java.util.Formatter formatter, int width, int precision, boolean leftJustified, java.lang.String out)
Can be used to append a String to a formatter.- Parameters:
formatter
- TheFormatter
width
- Minimum width to meet, filled with space if neededprecision
- Maximum amount of characters to appendleftJustified
- Whether or not to left-justify the valueout
- The String to append
-
createRequestBody
public static okhttp3.RequestBody createRequestBody(okhttp3.MediaType contentType, java.io.InputStream stream)
Creates a new request body that transmits the providedInputStream
.- Parameters:
contentType
- TheMediaType
of the datastream
- TheInputStream
to be transmitted- Returns:
- RequestBody capable of transmitting the provided InputStream of data
-
-