Search code examples
discord-jda

JDA 5 embed addreaction is incompatible types error


I have imported net.dv8tion.jda.api.entities.emoji.Emoji and below is the code written

EmbedBuilder embed = new EmbedBuilder();
                embed.setColor(Color.green);
                event.getChannel().sendTyping().queue();
                event.getChannel().sendMessage("").setEmbeds(embed.build()).queue(message -> {
                    message.addReaction("✔").queue();
                });

When running the code above, the error java: incompatible types: java.lang.String cannot be converted to net.dv8tion.jda.api.entities.emoji.Emoji occurs.


Solution

  • You have to create an instance of Emoji. It offers various factory methods:

    • Emoji.fromCustom Can be used to create a custom emoji (those you upload with custom images)
    • Emoji.fromUnicode Can be used to create a unicode emoji (such as smiley or similar)
    • Emoji.fromFormatted Can be used to parse any emoji from the mention format (relevant for messages)

    In your case, the string you have is a unicode emoji. To use it as a reaction, simply wrap it in Emoji.fromUnicode(...).

    Example: message.addReaction(Emoji.fromUnicode("✔")).queue()

    Note that you should also make sure that your files and compiler use UTF-8 if you directly paste emoji this way. To avoid issues with encoding you can use the codepoint notation instead. You can find the codepoint notation by searching for ✔ unicode on your search engine of choice.

    Example: message.addReaction(Emoji.fromUnicode("U+2713")).queue()

    For performance reasons, it is recommended to store these emoji instances in static final fields.

    public class Emojis {
      public static final Emoji CHECKMARK = Emoji.fromUnicode("U+2713");
    }
    

    Then you can just use it like this: message.addReaction(Emojis.CHECKMARK).queue().