snowball666
snowball666

Reputation: 1

Discord JDA How to listen for reactions

When I add two reactions to this message but i don't how to use listening reactions.This means that the user's click reaction will not perform any action.Pls how do I modify the following code,Changed to listen to the user click reaction to make an action

        User member = event.getUser();
        EmbedBuilder eb = new EmbedBuilder()
                .setColor(new Color(67, 239, 152))
                .setDescription(event.getMember().getUser().getAsMention() + "")
                .setFooter(event.getGuild().getMemberCount() + "")
                .setTimestamp(Instant.now());
        System.out.println(event.getMember().getUser().getAsMention());
        EmbedBuilder a = new EmbedBuilder()
                .setColor(new Color(52, 213, 235))
                .setDescription("");
        EmbedBuilder b = new EmbedBuilder()
                .setColor(new Color(52, 213, 235))
                .setDescription("Will you follow the administrator's instructions and obey");
        member.openPrivateChannel().queue(channel -> channel.sendMessageEmbeds(a.build()).queue());
        member.openPrivateChannel().queue(channel -> channel.sendMessageEmbeds(b.build()).queue(message -> {
            message.addReaction("✔").queue();
            message.addReaction("❌").queue();

        }));
        Role role = event.getGuild().getRoleById(String.valueOf("------"));
        assert role != null;
        event.getGuild().addRoleToMember(event.getMember(), role).queue();
        event.getGuild().getTextChannelById("------").sendMessageEmbeds(eb.build()).queue();

    }```

Upvotes: 0

Views: 617

Answers (1)

Minn
Minn

Reputation: 6134

You have to use the MessageReactionAddEvent.

public void onMessageReactionAdd(MessageReactionAddEvent event) {
    switch (event.getReactionEmote().getName()) { // Note: This will be renamed to getEmoji() in jda 5
        case "✔":
            ...
            break;
        case "❌":
            ...
            break;
    }
}

However, I would highly recommend using buttons instead of reactions.

For your usage of openPrivateChannel, try not to make two requests here, you can just use the callback for both messages:

member.openPrivateChannel().queue(channel -> {
    channel.sendMessageEmbeds(a.build()).queue();
    channel.sendMessageEmbeds(b.build()).queue(message -> {
        message.addReaction("✔").queue();
        message.addReaction("❌").queue();
    });
});

And keep in mind that mentions do not render consistently in embeds. You might want to consider using user.getAsTag() instead of a mention.

Upvotes: 1

Related Questions