0

I am creating a discord bot which will get any emojis within a message and will then react to that message with those emojis.

I have tried both event.getMessage().getEmotes() and event.getMessage().getEmotesBag(). However both return an empty list/bag.

Here is my current code:

@Override
    public void onGuildMessageReceived(GuildMessageReceivedEvent event) 
    {
        System.out.println("Event Fired");
        if(event.getMessage().getChannel() == event.getGuild().getTextChannelById("632303757929086998"))
        {
            System.out.println("Found Message");
            System.out.println("Emojis: " + event.getMessage().getEmotes());
            for(Emote emote : event.getMessage().getEmotes())
            {
                System.out.println("Found emote" + emote);
                event.getMessage().addReaction(emote).queue();
            }
        }
    }

This outputs: Event Fired, Found Message and Emojis: []

I expected/wanted it to output all of the emojis that are in the message and add them as a reaction to the message.

TurtyWurty
  • 13
  • 1
  • 6

1 Answers1

2

As the documentation mentions Emote is only for custom emoji of servers. To retrieve emoji you have to use an emoji library which can find and filter out emoji from a string.

You can use emoji-java (updated in my fork) for this:

String content = message.getContentRaw();
List<String> emojis = EmojiParser.extractEmojis(content);
for (String emoji : emojis) {
    message.addReaction(emoji).queue();
}

To combine this with custom emotes you can use the mention format of the emotes and the unicodes to find each index and order your list.

// Collect emojis
String content = message.getContentRaw();
List<String> emojis = EmojiParser.extractEmojis(content);
List<String> customEmoji = message.getEmotes().stream()
        .map((emote) -> emote.getName() + ":" + emote.getId())
        .collect(Collectors.toList());

// Create merged list
List<String> merged = new ArrayList<>();
merged.addAll(emojis);
merged.addAll(customEmoji);

// Sort based on index in message to preserve order
merged.sort(Comparator.comparingInt(content::indexOf));

for (String emoji : merged) {
    message.addReaction(emoji).queue();
}

Note that duplicates are ignored by this because reactions can't be duplicated.

Minn
  • 5,688
  • 2
  • 15
  • 42
  • Ok, I tried this, but it doesn't seem to work as it says `[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception java.lang.NoClassDefFoundError: org/json/JSONArray` – TurtyWurty Oct 13 '19 at 14:54
  • That is completely unrelated, you're missing dependencies of JDA. Check the setup guide in the wiki or ask on the support server. – Minn Oct 13 '19 at 15:11
  • @TurtyWurty - https://stackoverflow.com/questions/6792197/how-to-add-jar-libraries-to-war-project-without-facing-java-lang-classnotfoundex – Mauricio Gracia Gutierrez Dec 19 '21 at 17:29