How to add multiple reactions to embed Discord JDA

brown tree logs

I am sending a discord embed using JDA and the following code:

event.getChannel().sendMessage(image.build()).queue();

I can add a single reaction to the message by changing the code to this:

event.getChannel().sendMessage(image.build()).complete().addReaction("✔").queue();

How can I add multiple reactions to this message?

edited 55 mins ago
dan1st

4,0324 gold badges13 silver badges32 bronze badges

asked 1 hour ago
UskompufUskompuf

4633 silver badges14 bronze badges

1 Answer

You can use the Message object returned by complete() multiple times.

So, you can just send the reactions one after another:

Message msg=event.getChannel().sendMessage(image.build()).complete();
msg.addReaction("✔").queue();
msg.addReaction("+1").queue();

This uses complete, however and will wait until the message has been sent. No listeners are executed during that time.

This means that your bot waits and other commands (by other users) are executed only after the message has been sent.

In order to fix that, you can use .queue() with a lambda:

event.getChannel().sendMessage(image.build()).queue(msg->{
    msg.addReaction("✔").queue();
    msg.addReaction("+1").queue();
});

If you want to do this multiple times, you can write a method for this:

public void sendMessageWithReactions(MessageChannel channel,MessageEmbed embed, String... reactions){
    channel.sendMessage(embed).queue(msg->{
        for(String reaction:reactions){
            msg.addReaction(reaction).queue();
        }
    });
}

You can call this method like this: sendMessageWithReactions(event.getChannel(),image.build(),"✔","+1");

edited 1 hour ago
answered 1 hour ago
dan1stdan1st

4,0324 gold badges13 silver badges32 bronze badges

Latest posts