Creating a Discord Bot that waits for a specific user's response using JDA

I’m new to Java and trying to make a Discord bot with JDA. My goal is to have the bot wait for a reply from the user who said “Hi Bananas!”.

Here’s what I want:

  1. User says “Hi Bananas!”
  2. Bot asks for their name
  3. Bot waits for that user’s response
  4. Bot says “Hello [name]!”

My current code doesn’t wait for input. It just spits out everything at once:

public class FruitBot extends ListenerAdapter {
  public void onMessageReceived(MessageReceivedEvent e) {
    if (e.getAuthor().isBot()) return;

    String msg = e.getMessage().getContentRaw();
    TextChannel channel = e.getTextChannel();

    if (msg.equals("Hi Bananas!")) {
      channel.sendMessage("What's your name?").queue();
      // How do I make it wait here?
      channel.sendMessage("Hello " + msg + "!").queue();
    }
  }
}

How can I make the bot wait for the original user’s reply? Also, is there a way to add a timeout if they don’t answer?

Any help would be awesome! I’m still learning Java and Discord bots are pretty new to me.

To achieve what you’re aiming for, you’ll need to implement a state management system. JDA doesn’t inherently support waiting for specific user responses, so you’ll have to track this yourself. Consider using a Map to store user IDs and their current ‘conversation state’.

When a user says “Hi Bananas!”, store their ID in the map with a ‘waiting for name’ state. In your message handler, check if the user’s ID is in the map and in the correct state. If so, process their response and update or remove their state.

For timeouts, you could use a ScheduledExecutorService to remove entries from your map after a set duration. This way, you’re not actually ‘waiting’, but rather managing conversation flow across multiple messages. Handle edge cases like users interrupting the conversation or multiple users interacting simultaneously.

I’ve been working with Discord bots for a while now, and I can tell you that handling user interactions like this can be tricky. One approach that’s worked well for me is using a HashMap to track conversation states. Here’s a rough idea:

Create a HashMap to store user IDs and their conversation state. When someone says “Hi Bananas!”, add their ID to the map with a “waiting for name” state. In your message handler, check if the user’s ID is in the map and in the right state. If it matches, process their response and update or remove their state.

For timeouts, you could use a ScheduledExecutorService to clear old entries from your map after a set time. This way, you’re not blocking execution, but managing the flow across multiple messages.

Remember to handle edge cases like users interrupting the conversation or multiple users interacting at once. It takes some trial and error, but you’ll get there!

hey there! i’ve faced similar issues before. u could use a WaitForEvent method to pause execution until the user replies. somethin like:

channel.sendMessage(“What’s your name?”).queue();
Message response = event.getChannel().sendMessage(“Waiting…”).complete();
event.getJDA().awaitEvents(…)

this should make ur bot wait for the specific user. good luck with ur project!