'Java Discord Bot
I am currently creating a bot for Discord. So far, I have got the bot up, and have it "Online" in my server. I created a custom command named !yata that displays a motivational message after being input. Is there a reason as to why my bot will not pick up commands? When I type in !yata it does not run the command.
Buki.java
package Buki;
import javax.security.auth.login.LoginException;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
public class Buki {
public static JDA jda;
public static String prefix = "!";
//Main method
public static void main(String[] args) throws LoginException {
JDA jda = JDABuilder.createDefault("bot token").build();
jda.addEventListener(new Commands());
}
}
Commands.java
package Buki;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Commands extends ListenerAdapter {
public void onGuildMessageReceived(MessageReceivedEvent e) {
String[] message = e.getMessage().getContentRaw().split(" ");
if (message[0].equalsIgnoreCase("!yata")) {
e.getChannel().sendTyping().queue();
e.getChannel().sendMessage("The road of a ninja is long!").queue();
}
else {
}
}
}
Solution 1:[1]
First of all, your codes are so weird!
How could the event be called MessageReceivedEvent
, meanwhile, the method called onGuildMessageReceivedEvent
, JDA already renamed the methods! and second you didn't use the GatewayIntent.GUILD_MESSAGES
, so basically, it going to be like this:
JDABuilder jda = JDABuilder.create(token, GatewayIntent.GUILD_MESSAGES);
and third, think is I see you using JDA
, not JDABuilder
which is wrong, I recommended you to check what version of JDA you use and use the last one!
Solution 2:[2]
public void onGuildMessageReceived(MessageReceivedEvent e)
needed to be changed to
public void onMessageReceivedEvent(MessageReceivedEvent e).
"Guild" was not required in this method's name.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | omar ibrahim |
Solution 2 | Wai Ha Lee |