Im making a discord bot with JDA and I want to know how to wait for a message. Something like this
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Listener extends ListenerAdapter {
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
String message = event.getMessage().getContentRaw();
boolean isBot = event.getAuthor().isBot();
// Check if message is not from a bot
if (!isBot) {
if (message.equalsIgnoreCase("hi")) {
event.getChannel().sendMessage("Hello, what's your name?").queue();
// Wait for second message
String name = event.getMessage().getContentRaw(); // Saving content from second message
event.getChannel().sendMessage("Your name is: " + name).queue();
}
}
}
}
User: Hi
Bot: Hello, what's your name?
User: sends name
Bot: Your name is: name
How do i get the second message?
As already told by Minn in the comment. You could use that method given in the thread. However, I'd recommend using the JDA-Utilities EventWaiter.
If you're using Maven or Gradle, use either one of these:
For gradle it's much easier:
When this has loaded in, I'd recommend checking the ExampleBot main class. for adding your own stuff to it. Since I will only explain the basics of getting the EventWaiter to work.
This will be the main class of the bot to start with, when you run this, it will start the bot with these commands. ShutdownCommand and PingCommand are a part of the Utilities. So these will be added automaticly.
Now, for the HelloCommand. You start by making a new class called "HelloCommand". In this class you will extend the "Command" portion of the utilities.
Now, this is where we get to the jucy stuff. When using the event waiter. There are a few area's you need to check for.
Explained in more detail:
This should be the BASICS to using the EventWaiter. This may depend on your use case. And it can be done by only using the eventwaiter class as well.