Search code examples
javadiscord4j

Discord4J - getContent() - Method delivers empty string


I am currently trying to write a Discord Bot and I found a great tutorial at https://www.baeldung.com/spring-discord4j-bot

The source code is also available on GitHub, however I'm not able to make it run and couldn't find any advice when googling or asking ChatGPT.

In method processCommand of MessageListener, the text should be retrieved via eventMessage.getContent(). However, whenever a user is posting, this string is empty, only the messages coming from the Bot itself are recognized. Does anyone have an idea, how I could resolve that?

Please find below the snippet. I already tried to get the data directly from the field "eventMessage" but couldn't make it work.

 public Mono<Void> processCommand(Message eventMessage) {       
    
    
    System.out.println("Message: " + eventMessage.getContent());
    System.out.println("User: " + eventMessage.getAuthor().get().getUsername());

    
    return Mono.just(eventMessage)
            .filter(message -> message.getAuthor().map(user -> !user.isBot()).orElse(false))
            .filter(message -> message.getContent().equalsIgnoreCase("!todo"))
            .flatMap(Message::getChannel)
            .flatMap(channel -> channel.createMessage("Things to do today:\n" +
                                                      " - write a bot\n" +
                                                      " - eat lunch\n" +
                                                      " - play a game"))
            .then();
}

Looking forward to getting some advice,

Thank you and kind regards :-)

I tried to debug it in eclipse, but string is always empty. I'm using Gradle 8.1.1 and SpringBoot 3.1.0 and Discord4j 3.2.5

type here

Solution

  • You need to enable the "Message Content" intent in the developer portal: https://support-dev.discord.com/hc/en-us/articles/4404772028055-Message-Content-Privileged-Intent-FAQ