Package net.dv8tion.jda.core.entities
Class Message.Attachment
- java.lang.Object
-
- net.dv8tion.jda.core.entities.Message.Attachment
-
- All Implemented Interfaces:
ISnowflake
- Enclosing interface:
- Message
public static class Message.Attachment extends java.lang.Object implements ISnowflake
Represents aMessage
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 FileIcon
getAsIcon()
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 theInputStream
that is created using anOkHttpClient
.JDA
getJDA()
The corresponding JDA instance for this Attachmentjava.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 HTTPResponse
to be used without having to copy it.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.dv8tion.jda.core.entities.ISnowflake
getCreationTime, getId
-
-
-
-
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 interfaceISnowflake
- 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 anIcon
instance for this attachment ifisImage()
istrue
!
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 anInputStream
otherwise it will use a normalURLConnection
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 imagejava.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 theInputStream
that is created using anOkHttpClient
.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 HTTPResponse
to be used without having to copy it.
UnlikegetInputStream()
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-nullIOConsumer
to accept the InputStream- Throws:
java.lang.IllegalArgumentException
- If the provided IOConsumer isnull
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: ifgetSize()
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.
-
-