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?