Discord Bot Commands Not Working in Java

I’m working on a Discord bot using Java and having trouble with command recognition. The bot shows as online in my server but won’t respond to commands.

I made a command called !motivate that should send an inspirational quote when someone types it in chat. However, when I test the command, nothing happens. The bot just ignores it completely.

Here’s my main bot class:

package DiscordBot;

import javax.security.auth.login.LoginException;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;

public class MainBot {
    public static JDA botInstance;
    public static String commandPrefix = "!";

    public static void main(String[] args) throws LoginException {
        JDA botInstance = JDABuilder.createDefault("your_bot_token_here").build();
        
        botInstance.addEventListener(new MessageHandler());
    }
}

And here’s my command handler:

package DiscordBot;

import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class MessageHandler extends ListenerAdapter {

    public void onGuildMessageReceived(MessageReceivedEvent event) {
        String[] args = event.getMessage().getContentRaw().split(" ");
                
        if (args[0].equalsIgnoreCase("!motivate")) {
            event.getChannel().sendTyping().queue();
            event.getChannel().sendMessage("Success comes to those who work hard!").queue();
        }
    }
}

What could be causing this issue? Am I missing something in my event handling setup?

You’re using the wrong event method. onGuildMessageReceived doesn’t exist in current JDA versions - it should be onMessageReceived. Also, check that Message Content Intent is enabled in your Discord Developer Portal under Bot settings. Without it, your bot can’t access message content at all. I ran into this exact issue when I started with JDA and wasted way too much time figuring out why my bot wasn’t responding. Oh, and add a check to ignore bot messages - prevents infinite loops if you’ve got multiple bots in your server.

hey, looks like you need to change onGuildMessageReceived to onMessageReceived. also, double-check in the dev portal that message content intent is enabled, otherwise the bot wont see messages.

Your problem is the deprecated onGuildMessageReceived method - it’s been removed in newer JDA versions. Switch to onMessageReceived like mentioned above. Also check that you’ve got Message Content Intent enabled in your Discord Developer Portal (Bot section). Without it, your bot can’t read messages. And throw in if (event.getAuthor().isBot()) return; at the start of your handler to ignore bot messages and avoid loops. I hit this exact issue when updating JDA and wasted hours debugging before I figured out they changed the method name.