Anton Lieskovsky
Anton Lieskovsky

Reputation: 189

How to send picture into Discord Channel

I need to send picture (screenshot) into Discord channel. I have successfully developed text sending into channel but I do not know how to send a screen.

Here is part of my code:

// connection to the Channel
TextChannel channel = api.getTextChannelById(this.channelId);
        if (channel != null) {
            channel.sendMessage(pMessage).queue();
        }

// capture the whole screen
BufferedImage screencapture = new Robot().createScreenCapture( new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );

// Save as JPEG - not necessary
File file = new File("screencapture.jpg");
ImageIO.write(screencapture, "jpg", file);

// CODE for sendPicture (screencapture to the Channel) HERE!!!
// code here
// code here

Any idea how to do it?

Upvotes: 0

Views: 2718

Answers (1)

Fabricio20
Fabricio20

Reputation: 508

As per the JDA docs, to send a file to a channel, you should use the appropriate sendFile RestAction.

There's a range of different send methods you may use, some of which allow you to send a message along with your file.

As an example, to send a file by using a File object:

channel.sendFile(new File("path/to/file")).queue();

Or, directly with an InputStream (in your case - to avoid writing to disk).

ByteArrayOutputStream stream = new ByteArrayOutputStream();
ImageIO.write(screencapture, "jpg", stream);
channel.sendFile(stream.toByteArray(), "fileName.jpg").queue();

Upvotes: 2

Related Questions