Class Message.Attachment

  • All Implemented Interfaces:
    ISnowflake
    Enclosing interface:
    Message

    public static class Message.Attachment
    extends java.lang.Object
    implements ISnowflake
    Represents a Message file attachment.
    • Constructor Summary

      Constructors 
      Constructor Description
      Attachment​(long id, java.lang.String url, java.lang.String proxyUrl, java.lang.String fileName, java.lang.String contentType, int size, int height, int width, boolean ephemeral, net.dv8tion.jda.internal.JDAImpl jda)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.concurrent.CompletableFuture<java.io.File> downloadToFile()
      Downloads the attachment into the current working directory using the file name provided by getFileName().
      java.util.concurrent.CompletableFuture<java.io.File> downloadToFile​(java.io.File file)
      Downloads the attachment to a file at the specified path (relative or absolute).
      java.util.concurrent.CompletableFuture<java.io.File> downloadToFile​(java.lang.String path)
      Downloads the attachment to a file at the specified path (relative or absolute).
      java.lang.String getContentType()
      The Content-Type of this file.
      java.lang.String getFileExtension()
      The file extension of the Attachment when it was first uploaded.
      java.lang.String getFileName()
      The file name of the Attachment when it was first uploaded.
      int getHeight()
      The height of the Attachment if this Attachment is an image/video.
      long getIdLong()
      The Snowflake id of this entity.
      JDA getJDA()
      The corresponding JDA instance for this Attachment
      java.lang.String getProxyUrl()
      Url to the resource proxied by the Discord CDN.
      int getSize()
      The size of the attachment in bytes.
      java.lang.String getUrl()
      The url of the Attachment, most likely on the Discord servers.
      int getWidth()
      The width of the Attachment if this Attachment is an image/video.
      boolean isEphemeral()
      Whether or not this attachment is from an ephemeral Message.
      boolean isImage()
      Whether or not this attachment is an Image, based on getWidth(), getHeight(), and getFileExtension().
      boolean isSpoiler()
      Whether or not this attachment is marked as spoiler, based on getFileName().
      boolean isVideo()
      Whether or not this attachment is a video, based on getWidth(), getHeight(), and getFileExtension().
      java.util.concurrent.CompletableFuture<Icon> retrieveAsIcon()
      Retrieves the image of this attachment and provides an Icon equivalent.
      java.util.concurrent.CompletableFuture<java.io.InputStream> retrieveInputStream()
      Enqueues a request to retrieve the contents of this Attachment.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Attachment

        public Attachment​(long id,
                          java.lang.String url,
                          java.lang.String proxyUrl,
                          java.lang.String fileName,
                          java.lang.String contentType,
                          int size,
                          int height,
                          int width,
                          boolean ephemeral,
                          net.dv8tion.jda.internal.JDAImpl jda)
    • Method Detail

      • getJDA

        @Nonnull
        public JDA getJDA()
        The corresponding JDA instance for this Attachment
        Returns:
        The corresponding JDA instance for this Attachment
      • getIdLong

        public long getIdLong()
        Description copied from interface: ISnowflake
        The Snowflake id of this entity. This is unique to every entity and will never change.
        Specified by:
        getIdLong in interface ISnowflake
        Returns:
        Long containing the Id.
      • getUrl

        @Nonnull
        public java.lang.String getUrl()
        The url of the Attachment, most likely on the Discord servers.
        Returns:
        Non-null String containing the Attachment URL.
      • getProxyUrl

        @Nonnull
        public java.lang.String getProxyUrl()
        Url to the resource proxied by the Discord CDN.
        Returns:
        Non-null String containing the proxied Attachment url.
      • getFileName

        @Nonnull
        public java.lang.String getFileName()
        The file name of the Attachment when it was first uploaded.
        Returns:
        Non-null String containing the Attachment file name.
      • getFileExtension

        @Nullable
        public java.lang.String getFileExtension()
        The file extension of the Attachment when it was first uploaded.
        Null is returned if no characters follow the last occurrence of the '.' character (or if the character is not present in getFileName()).
        Returns:
        Non-null String containing the Attachment file extension, or null if it can't be determined.
      • getContentType

        @Nullable
        public java.lang.String getContentType()
        The Content-Type of this file.
        This is the Media type of the file that would be used in an HTTP request or similar.
        Returns:
        The content-type, or null if this isn't provided
      • retrieveInputStream

        @Nonnull
        public java.util.concurrent.CompletableFuture<java.io.InputStream> retrieveInputStream()
        Enqueues a request to retrieve the contents of this Attachment.
        The receiver is expected to close the retrieved InputStream.

        Example

        
         public void printContents(Message.Attachment attachment)
         {
             attachment.retrieveInputStream().thenAccept(in -> {
                 StringBuilder builder = new StringBuilder();
                 byte[] buf = byte[1024];
                 int count = 0;
                 while ((count = in.read(buf)) > 0)
                 {
                     builder.append(new String(buf, 0, count));
                 }
                 in.close();
                 System.out.println(builder);
             }).exceptionally(t -> { // handle failure
                 t.printStackTrace();
                 return null;
             });
         }
         
        Returns:
        CompletableFuture - Type: InputStream
      • downloadToFile

        @Nonnull
        public java.util.concurrent.CompletableFuture<java.io.File> downloadToFile()
        Downloads the attachment into the current working directory using the file name provided by getFileName().
        This will download the file using the callback pool. Alternatively you can use retrieveInputStream() and use a continuation with a different executor.

        Example

        
         public void saveLocally(Message.Attachment attachment)
         {
             attachment.downloadToFile()
                 .thenAccept(file -> System.out.println("Saved attachment to " + file.getName()))
                 .exceptionally(t ->
                 { // handle failure
                     t.printStackTrace();
                     return null;
                 });
         }
         
        Returns:
        CompletableFuture - Type: File
      • downloadToFile

        @Nonnull
        public java.util.concurrent.CompletableFuture<java.io.File> downloadToFile​(java.lang.String path)
        Downloads the attachment to a file at the specified path (relative or absolute).
        This will download the file using the callback pool. Alternatively you can use retrieveInputStream() and use a continuation with a different executor.

        Example

        
         public void saveLocally(Message.Attachment attachment)
         {
             attachment.downloadToFile("/tmp/" + attachment.getFileName())
                 .thenAccept(file -> System.out.println("Saved attachment to " + file.getName()))
                 .exceptionally(t ->
                 { // handle failure
                     t.printStackTrace();
                     return null;
                 });
         }
         
        Parameters:
        path - The path to save the file to
        Returns:
        CompletableFuture - Type: File
        Throws:
        java.lang.IllegalArgumentException - If the provided path is null
      • downloadToFile

        @Nonnull
        public java.util.concurrent.CompletableFuture<java.io.File> downloadToFile​(java.io.File file)
        Downloads the attachment to a file at the specified path (relative or absolute).
        This will download the file using the callback pool. Alternatively you can use retrieveInputStream() and use a continuation with a different executor.

        Example

        
         public void saveLocally(Message.Attachment attachment)
         {
             attachment.downloadToFile(new File("/tmp/" + attachment.getFileName()))
                 .thenAccept(file -> System.out.println("Saved attachment to " + file.getName()))
                 .exceptionally(t ->
                 { // handle failure
                     t.printStackTrace();
                     return null;
                 });
         }
         
        Parameters:
        file - The file to write to
        Returns:
        CompletableFuture - Type: File
        Throws:
        java.lang.IllegalArgumentException - If the provided file is null or cannot be written to
      • retrieveAsIcon

        @Nonnull
        public java.util.concurrent.CompletableFuture<Icon> retrieveAsIcon()
        Retrieves the image of this attachment and provides an Icon equivalent.
        Useful with AccountManager.setAvatar(Icon).
        This will download the file using the callback pool. Alternatively you can use retrieveInputStream() and use a continuation with a different executor.

        Example

        
         public void changeAvatar(Message.Attachment attachment)
         {
             attachment.retrieveAsIcon().thenCompose(icon -> {
                 SelfUser self = attachment.getJDA().getSelfUser();
                 AccountManager manager = self.getManager();
                 return manager.setAvatar(icon).submit();
             }).exceptionally(t -> {
                 t.printStackTrace();
                 return null;
             });
         }
         
        Returns:
        CompletableFuture - Type: Icon
        Throws:
        java.lang.IllegalStateException - If this is not an image (isImage())
      • getSize

        public int getSize()
        The size of the attachment in bytes.
        Example: if getSize() returns 1024, then the attachment is 1024 bytes, or 1KiB, in size.
        Returns:
        Positive int containing the size of the Attachment.
      • getHeight

        public int getHeight()
        The height of the Attachment if this Attachment is an image/video.
        If this Attachment is neither an image, nor a video, this returns -1.
        Returns:
        int containing image/video Attachment height, or -1 if attachment is neither image nor video.
      • getWidth

        public int getWidth()
        The width of the Attachment if this Attachment is an image/video.
        If this Attachment is neither an image, nor a video, this returns -1.
        Returns:
        int containing image/video Attachment width, or -1 if attachment is neither image nor video.
      • isEphemeral

        public boolean isEphemeral()
        Whether or not this attachment is from an ephemeral Message.
        If this Attachment is ephemeral, it will automatically be removed after 2 weeks. The attachment is guaranteed to be available as long as the message itself exists.
        Returns:
        True if this attachment is from an ephemeral message
      • isSpoiler

        public boolean isSpoiler()
        Whether or not this attachment is marked as spoiler, based on getFileName().
        Returns:
        True if this attachment is marked as spoiler
        Since:
        4.2.1