【问题标题】:How to send picture into Discord Channel如何将图片发送到 Discord 频道
【发布时间】:2019-05-11 03:27:36
【问题描述】:

我需要将图片(屏幕截图)发送到 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

知道怎么做吗?

【问题讨论】:

    标签: java discord discord-jda


    【解决方案1】:

    根据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();
    

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2018-11-24
      • 2020-07-29
      • 2021-03-24
      • 2021-10-28
      • 2020-04-07
      • 2018-11-24
      • 2021-04-06
      • 2021-01-10
      相关资源
      最近更新 更多