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, int size, int height, int width, net.dv8tion.jda.core.entities.impl.JDAImpl jda)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean download​(java.io.File file)
      Downloads this attachment to given File
      Icon getAsIcon()
      Creates an Icon instance for this attachment if isImage() is true!
      This is a convenience method that can be used to retrieve an Icon from an attachment image link which requires a set user-agent to be loaded.
      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.
      long getIdLong()
      The Snowflake id of this entity.
      java.io.InputStream getInputStream()
      Creates a copy of the InputStream that is created using an OkHttpClient.
      JDA getJDA()
      The corresponding JDA instance for this Attachment
      java.lang.String getProxyUrl()
      The url of the Attachment, proxied by Discord.
      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.
      boolean isImage()
      Whether or not this attachment is an Image.
      void withInputStream​(IOConsumer<java.io.InputStream> then)
      Allows to access the InputStream that is available from the HTTP Response to be used without having to copy it.
      • 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,
                          int size,
                          int height,
                          int width,
                          net.dv8tion.jda.core.entities.impl.JDAImpl jda)
    • Method Detail

      • getJDA

        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

        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

        public java.lang.String getProxyUrl()
        The url of the Attachment, proxied by Discord.
        Url to the resource proxied by https://images.discordapp.net
        Note: This URL will most likely only work for images. (isImage())
        Returns:
        Non-null String containing the proxied Attachment url.
      • getFileName

        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.
      • getAsIcon

        public Icon getAsIcon()
                       throws java.io.IOException
        Creates an Icon instance for this attachment if isImage() is true!
        This is a convenience method that can be used to retrieve an Icon from an attachment image link which requires a set user-agent to be loaded.

        When a global proxy was specified via JDABuilder this will use the specified proxy to create an InputStream otherwise it will use a normal URLConnection with the User-Agent for the currently logged in account.

        Returns:
        Icon for this image attachment
        Throws:
        java.io.IOException - If an IOError occurs while reading the image
        java.lang.IllegalStateException - If this is not an image attachment
        Since:
        3.4.0
      • download

        public boolean download​(java.io.File file)
        Downloads this attachment to given File
        Parameters:
        file - The file, where the attachment will get downloaded to
        Returns:
        boolean true, if successful, otherwise false
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        Creates a copy of the InputStream that is created using an OkHttpClient.

        You can access the input stream directly using withInputStream(IOConsumer) which will have an open input stream available within the consumer scope. The stream will be closed once that method returns.

        Returns:
        InputStream copy of the response body for this Attachment
        Throws:
        java.io.IOException - If an IO error occurs trying to read from the opened HTTP channel
        Since:
        3.4.0
      • withInputStream

        public void withInputStream​(IOConsumer<java.io.InputStream> then)
                             throws java.io.IOException
        Allows to access the InputStream that is available from the HTTP Response to be used without having to copy it.
        Unlike getInputStream() this does not return a full copy of the input stream. Instead this method will provide the InputStream data in the specified consumer in which it is still accessible.

        When this method returns the InputStream will be closed accordingly!

        Parameters:
        then - Not-null IOConsumer to accept the InputStream
        Throws:
        java.lang.IllegalArgumentException - If the provided IOConsumer is null
        java.io.IOException - If an IOException occurs within the IOConsumer or while opening an HTTP channel
        Since:
        3.4.0
      • 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.
        If this Attachment is not an image, this returns -1.
        Returns:
        int containing image Attachment height.
      • getWidth

        public int getWidth()
        The width of the Attachment if this Attachment is an image.
        If this Attachment is not an image, this returns -1.
        Returns:
        int containing image Attachment width.
      • isImage

        public boolean isImage()
        Whether or not this attachment is an Image.
        Based on the values of getHeight and getWidth being larger than zero.
        Returns:
        True if width and height are greater than zero.