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 Details

    • initialize

      public static void initialize(@NotNull @NotNull 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 RestAction<@Nullable String> bannerLink(@NotNull @NotNull 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 RestAction<@Nullable String> bannerId(@NotNull @NotNull 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 RestAction<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 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 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.