Interface Thumbnail

All Superinterfaces:
Component, SectionAccessoryComponent
All Known Implementing Classes:
ThumbnailFileUpload, ThumbnailImpl

public interface Thumbnail extends SectionAccessoryComponent
Component displaying a thumbnail, you can mark it as a spoiler and set a description.

Requirements: Components V2 to be enabled!

  • Field Details

    • MAX_DESCRIPTION_LENGTH

      static final int MAX_DESCRIPTION_LENGTH
      The maximum number of characters a thumbnail's description can have. (1024)
      See Also:
  • Method Details

    • fromUrl

      @Nonnull static Thumbnail fromUrl(@Nonnull String url)
      Constructs a new Thumbnail from the given URL.

      Re-uploading external images

      If you instead want to re-upload the content of the URL, you have two options to reference images to upload:
      1. Use fromFile(FileUpload) instead, it will set a URI similar to attachment://file.ext, and automatically adds the attachment to the request.
        This is the easiest, but it may upload unnecessary files when editing, as it is not aware if the file already exists on the message, wasting bandwidth and potentially slowing down response times.
      2. Do the same manually, meaning you need to pass the attachment://file.ext URI, where file.ext is the name of the file you will upload, then, on the request, add the file, using MessageCreateRequest#addFiles(FileUpload...) for example.
        This is more tedious but gives you full control over the uploads.

      Example

      
       MessageChannel channel; // = reference of a MessageChannel
       Thumbnail thumbnail = Thumbnail.fromUrl("attachment://cat.png") // we specify this in sendFile as "cat.png"
           .setDescription("This is a cute car :3");
       Section section = Section.of(
           thumbnail,
           TextDisplay.of("Sample text")
       );
       // It's recommended to use a more robust HTTP library instead,
       // such as Java 11+'s HttpClient, or OkHttp (included with JDA), among many other options.
       InputStream file = new URL("https://http.cat/500").openStream();
       channel.sendFiles(FileUpload.fromData(file, "cat.png"))
           .setComponents(section)
           .useComponentsV2()
           .queue();
       
      Parameters:
      url - The URL of the thumbnail to display
      Returns:
      The new Thumbnail
      Throws:
      IllegalArgumentException - If null is provided
    • fromFile

      @Nonnull static Thumbnail fromFile(@Nonnull FileUpload file)
      Constructs a new Thumbnail from the FileUpload.

      This method can also be used to upload external resources, such as by using FileUpload.fromData(InputStream, String), in which case it will re-upload the entire file.

      This will automatically add the file when building the message; as such, you do not need to add it manually (with MessageCreateRequest.addFiles(FileUpload...) for example).

      Example

      
       MessageChannel channel; // = reference of a MessageChannel
       // It's recommended to use a more robust HTTP library instead,
       // such as Java 11+'s HttpClient, or OkHttp (included with JDA), among many other options.
       InputStream file = new URL("https://http.cat/500").openStream();
       // You can also replace this with a local file
       Thumbnail thumbnail = Thumbnail.fromFile(FileUpload.fromData(file, "cat.png"))
           .setDescription("This is a cute car :3");
       Section section = Section.of(
           thumbnail,
           TextDisplay.of("Sample text")
       );
       channel.sendComponents(section)
           .useComponentsV2()
           .queue();
       
      Parameters:
      file - The FileUpload to display
      Returns:
      The new Thumbnail
      Throws:
      IllegalArgumentException - If null is provided
    • withUniqueId

      @Nonnull @CheckReturnValue Thumbnail withUniqueId(int uniqueId)
      Description copied from interface: Component
      Creates a new component with the provided numeric ID.
      If no ID is set, Discord will generate IDs incrementally starting from 1 and will not use existing IDs from the same message/modal.
      Specified by:
      withUniqueId in interface Component
      Specified by:
      withUniqueId in interface SectionAccessoryComponent
      Parameters:
      uniqueId - The new ID; must be higher or equal to 1
      Returns:
      The new component
    • withDescription

      @Nonnull @CheckReturnValue Thumbnail withDescription(@Nullable String description)
      Creates a new Thumbnail with the provided description.
      The description is known as an "alternative text", and must not exceed 1024 characters.
      Parameters:
      description - The new description
      Returns:
      The new Thumbnail
      Throws:
      IllegalArgumentException - If the description is longer than 1024 characters.
    • withSpoiler

      @Nonnull @CheckReturnValue Thumbnail withSpoiler(boolean spoiler)
      Creates a new Thumbnail with the provided spoiler status.
      Spoilers are hidden until the user clicks on it.
      Parameters:
      spoiler - The new spoiler status
      Returns:
      The new Thumbnail
    • getUrl

      @Nonnull String getUrl()
      The URL of this thumbnail, this is always where the file originally came from.
      This can be either attachment://filename.extension or an actual URL.

      If you want to download the file, you should use getResolvedMedia() then ResolvedMedia.getProxy(), to avoid connecting your bot to unknown servers.

      Returns:
      The URL of this thumbnail
    • getResolvedMedia

      @Nullable ResolvedMedia getResolvedMedia()
      The media resolved from this thumbnail, this is only available if you receive this component from Discord.
      Returns:
      Possibly-null ResolvedMedia
    • getDescription

      @Nullable String getDescription()
      The description of this thumbnail, or null if none has been set.
      Returns:
      Possibly-null description
    • isSpoiler

      boolean isSpoiler()
      Whether this thumbnail is hidden until the user clicks on it.
      Returns:
      true if this is hidden by default, false otherwise