如何将图片发送到Discord频道

时间:2018-12-10 08:47:02

标签: java discord discord-jda

我需要将图片(屏幕截图)发送到Discord频道。我已经成功开发了将文本发送到频道的功能,但是我不知道如何发送屏幕。

这是我的代码的一部分:

// 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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

根据JDA docs,要将文件发送到通道,应使用适当的sendFile RestAction。

您可以使用多种不同的发送方法,其中一些允许您将消息与文件一起发送。

例如,使用File对象发送文件:

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

或者,直接使用InputStream(在您的情况下-避免写入磁盘)。

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