javadiscordbotsdiscord-jda

Edit an embed message with an attachment for image doubles it in chat - Discord JDA


I'm trying to edit the embed of a message with another embed. When i do so the image appears once more in chat on top of the edited embed. Is there a way to prevent this ?

@Override
    public void onGuildReady(@NotNull GuildReadyEvent event) {
        super.onGuildReady(event);

        String fileName = "image.png";

        Guild guild = event.getGuild();
        InputStream in = getClass().getResourceAsStream("/someResource.png");

        EmbedBuilder eb = new EmbedBuilder()
        .setTitle("Title :")
        .setDescription("Description")
        .setImage("attachment://" + fileName)
        .setColor(new Color(255, 204, 81));
        guild.getTextChannels().get(0).sendMessage(eb.build()).addFile(in, fileName).queue(
                message -> {
                    MessageEmbed me = message.getEmbeds().get(0);
                    message.editMessage(
                            new EmbedBuilder()
                            .setTitle(me.getTitle())
                            .setDescription(me.getDescription())
                            .setImage(me.getImage().getUrl())
                            .setColor(me.getColor())
                            .build()
                    ).queue();
                }
        );
    }

Result:

Image doubled on top in chat


Solution

  • Your problem is the following line: .setImage(me.getImage().getUrl()), that line will return a discord url like https://media.discordapp.net/attachments/203572340280262657/877103116036759642/unknown.png and not attachment://image.png.

    The image in the embed thus will be changed to the image from the url, and as discord isn't able to edit or delete attachments your initial image will get placed outside the embed.

    One way to get around this is to use the following code:

    guild.getTextChannels().get(0).sendFile(in, fileName).embed(eb.build()).queue(
                message -> {
                    MessageEmbed me = message.getEmbeds().get(0);
                    message.editMessage(
                            new EmbedBuilder(me)
                            .setTitle("New Title")
                            .build()
                    ).queue();
                }
        );
    

    new EmbedBuilder(me) will create an embedbuilder starting from another embed. If you use this you only need to set the parts of the embed which need to be changed and it will keep everything else. Altough I prefer using channel.sendFile(...).embed(..).queue() as mentioned by Redi, your way of sending the image is equally correct.

    If you want to change the image however then it's another story. As I mentioned before discord doesn't allow for attachments to be changed. There might be confusion around the clearFiles method, but this method only closes files that are added to the RestAction but not sent. The only way to be able to change the image after sending the embed is not using an attachment but using a normal url from the beginning. If you do this you can change the url later which will result in the image changing as it isn't an attachment.