Class UserUtils

java.lang.Object
com.dianxin.tori.api.utils.jda.UserUtils

public final class UserUtils extends Object
A utility class providing convenient methods to extract information from and interact with JDA User objects.

Note: Methods that retrieve users from the API or cache require this class to be initialized via initialize(JDA) first.

  • Method Summary

    Modifier and Type
    Method
    Description
    static @Nullable String
    avatarId(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the ID hash of the user's custom avatar.
    static @Nullable String
    avatarLink(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the URL of the user's custom avatar.
    static @NotNull net.dv8tion.jda.api.requests.RestAction<@Nullable String>
    bannerId(@NotNull net.dv8tion.jda.api.entities.User user)
    Retrieves the ID hash of the user's profile banner.
    static @NotNull net.dv8tion.jda.api.requests.RestAction<@Nullable String>
    bannerLink(@NotNull net.dv8tion.jda.api.entities.User user)
    Retrieves the URL of the user's profile banner.
    static @NotNull OffsetDateTime
    createdTime(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the exact time the user's Discord account was created.
    static @NotNull String
    debugTag(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets a formatted string useful for logging or debugging, combining the user's display name and ID.
    static @NotNull String
    defaultAvatarId(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the identifier for the user's default Discord avatar.
    static @NotNull String
    defaultAvatarLink(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the URL of the user's default Discord avatar.
    static @NotNull String
    displayName(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the effective display name of the user.
    static @NotNull String
    effectiveAvatarLink(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the effective avatar URL for the user.
    static @Nullable net.dv8tion.jda.api.entities.User
    getCachedUser(@NotNull String id)
    Retrieves a user by their ID directly from the internal JDA cache.
    static @Nullable String
    globalName(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the global display name of the user, if they have set one.
    static boolean
    hasGlobalName(@NotNull net.dv8tion.jda.api.entities.User user)
    Checks if the user has configured a global display name.
    static boolean
    hasPrivateChannel(@NotNull net.dv8tion.jda.api.entities.User user)
    Checks if the bot currently has an open Private Channel (DM) with this user.
    static @NotNull String
    id(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the snowflake ID of the user.
    static void
    initialize(@NotNull net.dv8tion.jda.api.JDA jda)
    Initializes the UserUtils with a JDA instance.
    static @NotNull String
    mention(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the mention string for the specified user.
    static @NotNull net.dv8tion.jda.api.requests.RestAction<net.dv8tion.jda.api.entities.User>
    retrieveUser(@NotNull String id)
    Retrieves a user by their ID from the Discord API.
    static @NotNull String
    tag(@NotNull net.dv8tion.jda.api.entities.User user)
    Deprecated.
    Discord has removed discriminators.
    static @NotNull String
    username(@NotNull net.dv8tion.jda.api.entities.User user)
    Gets the raw username of the user.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • initialize

      public static void initialize(@NotNull @NotNull net.dv8tion.jda.api.JDA jda)
      Initializes the UserUtils with a JDA instance. This must be called before using retrieveUser(String) or getCachedUser(String).
      Parameters:
      jda - The JDA instance to be used for user lookups.
    • mention

      @NotNull public static @NotNull String mention(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the mention string for the specified user.
      Parameters:
      user - The user to mention.
      Returns:
      The mention string (e.g., <@123456789>).
    • id

      @NotNull public static @NotNull String id(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the snowflake ID of the user.
      Parameters:
      user - The target user.
      Returns:
      The user's ID as a String.
    • username

      @NotNull public static @NotNull String username(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the raw username of the user.
      Parameters:
      user - The target user.
      Returns:
      The user's account username.
    • globalName

      @Nullable public static @Nullable String globalName(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the global display name of the user, if they have set one.
      Parameters:
      user - The target user.
      Returns:
      The global name, or null if not set.
    • displayName

      @NotNull public static @NotNull String displayName(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the effective display name of the user. Returns the global name if present; otherwise, falls back to their standard username.
      Parameters:
      user - The target user.
      Returns:
      The effective display name.
    • hasGlobalName

      public static boolean hasGlobalName(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Checks if the user has configured a global display name.
      Parameters:
      user - The target user.
      Returns:
      true if a global name is set, false otherwise.
    • tag

      @NotNull @Deprecated public static @NotNull String tag(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Deprecated.
      Discord has removed discriminators. Use username(User) or displayName(User) instead.
      Gets the user's tag (Username#Discriminator).
      Parameters:
      user - The target user.
      Returns:
      The user's tag.
    • debugTag

      @NotNull public static @NotNull String debugTag(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets a formatted string useful for logging or debugging, combining the user's display name and ID.
      Parameters:
      user - The target user.
      Returns:
      A string formatted as "DisplayName (ID)".
    • avatarLink

      @Nullable public static @Nullable String avatarLink(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the URL of the user's custom avatar.
      Parameters:
      user - The target user.
      Returns:
      The avatar URL, or null if they are using a default avatar.
    • avatarId

      @Nullable public static @Nullable String avatarId(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the ID hash of the user's custom avatar.
      Parameters:
      user - The target user.
      Returns:
      The avatar ID, or null if they are using a default avatar.
    • effectiveAvatarLink

      @NotNull public static @NotNull String effectiveAvatarLink(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the effective avatar URL for the user. This will return their custom avatar if present, or their default avatar otherwise.
      Parameters:
      user - The target user.
      Returns:
      The effective avatar URL.
    • defaultAvatarLink

      @NotNull public static @NotNull String defaultAvatarLink(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the URL of the user's default Discord avatar.
      Parameters:
      user - The target user.
      Returns:
      The default avatar URL.
    • defaultAvatarId

      @NotNull public static @NotNull String defaultAvatarId(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the identifier for the user's default Discord avatar.
      Parameters:
      user - The target user.
      Returns:
      The default avatar ID.
    • hasPrivateChannel

      public static boolean hasPrivateChannel(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Checks if the bot currently has an open Private Channel (DM) with this user.
      Parameters:
      user - The target user.
      Returns:
      true if a private channel exists in cache.
    • bannerLink

      @NotNull public static @NotNull net.dv8tion.jda.api.requests.RestAction<@Nullable String> bannerLink(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Retrieves the URL of the user's profile banner. This operation requires an API request.
      Parameters:
      user - The target user.
      Returns:
      A RestAction resolving to the banner URL, or null if no banner is set.
    • bannerId

      @NotNull public static @NotNull net.dv8tion.jda.api.requests.RestAction<@Nullable String> bannerId(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Retrieves the ID hash of the user's profile banner. This operation requires an API request.
      Parameters:
      user - The target user.
      Returns:
      A RestAction resolving to the banner ID, or null if no banner is set.
    • retrieveUser

      @NotNull public static @NotNull net.dv8tion.jda.api.requests.RestAction<net.dv8tion.jda.api.entities.User> retrieveUser(@NotNull @NotNull String id)
      Retrieves a user by their ID from the Discord API.

      Requires initialize(JDA) to have been called previously.

      Parameters:
      id - The snowflake ID of the user.
      Returns:
      A RestAction resolving to the requested User.
    • getCachedUser

      @Nullable public static @Nullable net.dv8tion.jda.api.entities.User getCachedUser(@NotNull @NotNull String id)
      Retrieves a user by their ID directly from the internal JDA cache.

      Requires initialize(JDA) to have been called previously.

      Parameters:
      id - The snowflake ID of the user.
      Returns:
      The cached User, or null if the user is not in the cache.
    • createdTime

      @NotNull public static @NotNull OffsetDateTime createdTime(@NotNull @NotNull net.dv8tion.jda.api.entities.User user)
      Gets the exact time the user's Discord account was created.
      Parameters:
      user - The target user.
      Returns:
      An OffsetDateTime representing the account creation timestamp.