javadiscorddiscord-jda

JDA getReaction().getEmoji().getAsReactionCode() not returning unicode emoji


I'm trying to make a bot that when reacted to a message with some emojis it will post a slightly different message.

But seems that the method is not returning the unicode for those emojis, I placed a System.out.println but the only thing that seems to be returning is ? (alongside a unknown symbol according to debugger) anyone can help with it?

public class EmoteReactControler extends ListenerAdapter {
    @Override
    public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event){
        String reaction = event.getReaction().getEmoji().getAsReactionCode();
        User userReacted = event.getUser();
        Channel channel = event.getChannel();
        String channelMention = channel.getAsMention();
        String jumpLink = event.getJumpUrl();
        String answer = "nothing";
        System.out.println(reaction);
        if ( reaction.equals("U+1F600")) {
            answer = "one";
        } else if (reaction.equals("U+1F601")) {
            answer = "two";
        } else if (reaction.equals("U+1F60D")) {
            answer = "three";
        } else if (reaction.equals("U+1F62D")){
            answer = "four";
        }

        String message = userReacted.getName() + " did " + answer + " see here: " + jumpLink + " in " + channelMention;
        event.getGuild().getTextChannelById(channel.getId()).sendMessage(message).queue();
    }
}

Solution

  • Judging from the code you provided, it looks like you are trying to get the reaction in codepoint notation. You can ReactionEmote#getAsCodepoints do do this. Example:

    @Override
    public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event){
        String codepoints = event.getReactionEmote().getAsCodepoints();
        String answer;
    
        switch(codepoints) {
            case "U+1F600":
                answer = "one";
                break;
            case "U+1F601": 
                answer = "two";
                break;
            case "U+1F60D":
                answer = "three";
                break;
            case "U+1F62D":
                answer = "four";
                break;
            default:
                answer = "nothing"
                break;
        }    
    
        // ...
    }
    

    Keep in mind that getAsCodepoints can throw an IllegalStateException if the reaction is not a valid UTF-8 character (e.i. a custom server emote). You may want to use escaped unicode characters instead of codepoints and stick with ReactionEmote#getAsReactionCode, as this does not throw any exceptions and would seem to work best in this context. Example:

    @Override
    public void onMessageReactionAdd(@NotNull MessageReactionAddEvent event){
        String reaction = event.getReaction().getEmoji().getAsReactionCode();
        String answer;
        
        switch(codepoints) {
            case "\uD83D\uDE00":
                answer = "one";
                break;
            case "\uD83D\uDE01": 
                answer = "two";
                break;
            case "\uD83D\uDE0D":
                answer = "three";
                break;
            case "\uD83D\uDE2D":
                answer = "four";
                break;
            default:
                answer = "nothing"
                break;
        }    
    
        // ...
    }
    

    I like to use the website fileformat.info when dealing with Java and unicode because it has an Encodings table which lists a C/C++/Java source code column.

    Note on ?


    If you are using a terminal that is not configured to use UTF-8 (such as out-of-the-box Eclipse), then unicode characters will be displayed as ?.

    You can configure the Eclipse console to use UTF-8 encoding by going to Run > Run Configurations, selecting the configuration for your. application, then going to the Common tab and setting Encoding to UTF-8:

    run configurations