Discord bot can't properly read message content

23 Views Asked by At

I'm currently trying to code my first Discord bot (a simple ping-pong bot). I am using discord4j, Maven and Java in IntelliJ Idea (I already have a bit of experience in Java, so I chose that). I'm only trying stuff out and don't need the bot to be online 24/7, so I am locally hosting it on my computer (and that is also the reason why I didn't bother with a proper profile picture for the bot).

The bot mostly works, however it seems like it can't properly detect the content of the received message. It always receives an empty string (not null, no whitespaces and no other characters). The length of the string it receives is also always 0. It can detect that a message was sent and also respond within Discord if that is the case. Even if I write "!ping", it treats it like any other message.

I used this guide

Code (Of course, I replaced TOKEN with my bot token):

import discord4j.core.DiscordClient;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.event.domain.lifecycle.ReadyEvent;
import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.User;
import reactor.core.publisher.Mono;

public class pingPongBot {
    public static void main(String[] args) {
        DiscordClient client = DiscordClient.create("TOKEN");

        Mono<Void> login = client.withGateway((GatewayDiscordClient gateway) -> {
            // ReadyEvent example
            Mono<Void> printOnLogin = gateway.on(ReadyEvent.class, event ->
                Mono.fromRunnable(() -> {
                    final User self = event.getSelf();
                    System.out.printf("Logged in as %s#%s%n", self.getUsername(), self.getDiscriminator());
                }))
                .then();

            // MessageCreateEvent example
            Mono<Void> pingCommand = gateway.on(MessageCreateEvent.class, event -> {
                Message message = event.getMessage();
                
                // Debug code
                if(message.getContent().equalsIgnoreCase("")) {
                    System.out.println("Message received: " + message.getContent());
                    System.out.println("Length of message received:" + message.getContent().length());
                    return message.getChannel().flatMap(channel ->
                            channel.createMessage("Message received: " + message.getContent()));
                }
                // Debug code end

                if(message.getContent().equalsIgnoreCase("!ping")) {
                    return message.getChannel().flatMap(channel -> channel.createMessage("Pong!"));
                }

                return Mono.empty();
            }).then();

            // combine them!
            return printOnLogin.and(pingCommand);
        });

        login.block();
    }
}

Example of sending both a random message and "!ping" in Discord as well as the output shown on the console

I know I can use slash commands as well, but I want to try it this way first (unless I literally can't fix what is going wrong).

English isn't my first language and this is my first time using stackoverflow, so please excuse any spelling-/ formatting mistakes. Thanks for any help in advance.

0

There are 0 best solutions below