Luke Prior
Luke Prior

Reputation: 967

How to add multiple reactions to embed Discord JDA

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?

Upvotes: 1

Views: 1171

Answers (1)

dan1st
dan1st

Reputation: 16476

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");

Upvotes: 1

Related Questions