Discord Bot in Java Not Responding to Commands

Hey everyone! I’m having trouble with my Discord bot. I’ve got it online in my server, but it’s not responding to any commands. I made a custom command called !yata that’s supposed to send a motivational message, but nothing happens when I type it. Here’s what I’ve done so far:

  1. Set up the bot and got it online
  2. Created a main class (let’s call it BotMain) to initialize the bot
  3. Made a Commands class that extends ListenerAdapter
  4. Added a method to handle the !yata command

The bot connects fine, but it just ignores all commands. Any ideas what might be wrong? I’m pretty new to Discord bot development, so I might be missing something obvious. Thanks for any help!

Here’s a simplified version of my code:

public class BotMain {
    public static void main(String[] args) {
        JDA bot = JDABuilder.createDefault("token").build();
        bot.addEventListener(new CommandHandler());
    }
}

public class CommandHandler extends ListenerAdapter {
    public void onMessageReceived(MessageReceivedEvent event) {
        if (event.getMessage().getContentRaw().equals("!motivate")) {
            event.getChannel().sendMessage("Keep pushing forward!").queue();
        }
    }
}

What am I doing wrong?

I’ve been there, Emma. One thing that caught me out when I first started was forgetting to add the correct gateway intents. Try modifying your JDABuilder like this:

JDA bot = JDABuilder.createDefault("token")
    .enableIntents(GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT)
    .build();

This ensures your bot can actually read message content. Also, double-check that your bot’s token is correct and hasn’t expired. I once spent hours debugging only to realize I’d pasted an old token!

If that doesn’t work, try adding some System.out.println() statements in your onMessageReceived method to see if it’s being called at all. This can help narrow down where the problem might be occurring.

Let us know if you’re still stuck after trying these suggestions!

I’ve encountered this issue before. One thing to check is if you’ve enabled the necessary Intents for your bot. In your JDABuilder, try adding .enableIntents(GatewayIntent.MESSAGE_CONTENT) before calling .build(). Without this, your bot might not receive message content.

Also, ensure your CommandHandler class is properly registered and verify that the onMessageReceived method is spelled correctly. A small typo can cause the event not to trigger as expected.

Adding debug logging in your onMessageReceived method might help you determine if the method is being called at all. If these suggestions don’t resolve the issue, reviewing Discord’s developer documentation could provide additional insights.

hey emma, i had a similar issue. check if u gave the bot proper permissions in ur server. also, make sure the bot’s role is high enough in the hierarchy. sometimes that can prevent it from responding. good luck!