How to send file with message to Discord with JDA?

Vladislav Chernogorov picture Vladislav Chernogorov · May 28, 2017 · Viewed 8.6k times · Source

I am using JDA lib to create my discord bot and I encountered a problem: in order to send message with a file, I should use existing message:

RestAction<Message> sendFile(File file, Message message)
RestAction<Message> sendFile(File file, String fileName, Message message)
RestAction<Message> sendFile(InputStream data, String fileName, Message message)

There is no implementation for sending file with simple string message. So when I'm trying to send file and pass a message to it I've got duplicated message.

So the question is: How to create Message with attached file without duplicating the message?

Answer

Benjamin Urquhart picture Benjamin Urquhart · Oct 20, 2018

You can send messages with attached files in one go like this:

//channel is a MessageChannel object or similar
channel.sendMessage("message").addFile(new File("path/to/file")).queue();