Interface Thumbnail
- All Superinterfaces:
Component,SectionAccessoryComponent
Component displaying a thumbnail, you can mark it as a spoiler and set a description.
Requirements: Components V2 to be enabled!
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.dv8tion.jda.api.components.Component
Component.Type -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe maximum number of characters a thumbnail's description can have. -
Method Summary
Modifier and TypeMethodDescriptionstatic ThumbnailfromFile(FileUpload file) Constructs a newThumbnailfrom theFileUpload.static ThumbnailConstructs a newThumbnailfrom the given URL.The description of this thumbnail, ornullif none has been set.The media resolved from this thumbnail, this is only available if you receive this component from Discord.getUrl()The URL of this thumbnail, this is always where the file originally came from.booleanWhether this thumbnail is hidden until the user clicks on it.withDescription(String description) Creates a newThumbnailwith the provided description.withSpoiler(boolean spoiler) Creates a newThumbnailwith the provided spoiler status.withUniqueId(int uniqueId) Creates a new component with the provided numeric ID.Methods inherited from interface net.dv8tion.jda.api.components.Component
getType, getUniqueId, isMessageCompatible, isModalCompatible
-
Field Details
-
MAX_DESCRIPTION_LENGTH
static final int MAX_DESCRIPTION_LENGTHThe maximum number of characters a thumbnail's description can have. (1024)- See Also:
-
-
Method Details
-
fromUrl
Constructs a newThumbnailfrom 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:- Use
fromFile(FileUpload)instead, it will set a URI similar toattachment://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. -
Do the same manually, meaning you need to pass the
attachment://file.extURI, wherefile.extis the name of the file you will upload, then, on the request, add the file, usingMessageCreateRequest#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- Ifnullis provided
- Use
-
fromFile
Constructs a newThumbnailfrom theFileUpload.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- TheFileUploadto display- Returns:
- The new
Thumbnail - Throws:
IllegalArgumentException- Ifnullis provided
-
withUniqueId
Description copied from interface:ComponentCreates 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:
withUniqueIdin interfaceComponent- Specified by:
withUniqueIdin interfaceSectionAccessoryComponent- Parameters:
uniqueId- The new ID; must be higher or equal to 1- Returns:
- The new component
-
withDescription
Creates a newThumbnailwith 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
Creates a newThumbnailwith the provided spoiler status.
Spoilers are hidden until the user clicks on it.- Parameters:
spoiler- The new spoiler status- Returns:
- The new
Thumbnail
-
getUrl
The URL of this thumbnail, this is always where the file originally came from.
This can be eitherattachment://filename.extensionor an actual URL.If you want to download the file, you should use
getResolvedMedia()thenResolvedMedia.getProxy(), to avoid connecting your bot to unknown servers.- Returns:
- The URL of this thumbnail
-
getResolvedMedia
The media resolved from this thumbnail, this is only available if you receive this component from Discord.- Returns:
- Possibly-null
ResolvedMedia
-
getDescription
The description of this thumbnail, ornullif none has been set.- Returns:
- Possibly-null description
-
isSpoiler
boolean isSpoiler()Whether this thumbnail is hidden until the user clicks on it.- Returns:
trueif this is hidden by default,falseotherwise
-