Try to search your question here, if you can't find : Ask Any Question Now ?

Discord JDA. Bot waits for user respond (to multiple questions)

HomeCategory: stackoverflowDiscord JDA. Bot waits for user respond (to multiple questions)
pushpa asked 3 weeks ago

I am writing a Discord bot using the JDA library and faced with a rather commonplace problem.

I need to write a method that would read the /addperson command (without any additional information after /addperson), then send the message “What is your name” to the same chat

The bot at this moment expects a response from the User (without interfering with other users to use the bot functionality in other TextChannels).

Possible events:

  1. If the User sent to the bot correct name (let’s say, Tony), then the bot should move to next question.
  2. If the User sent to the bot wrong name (let’s say, 21324), the bot repeatedly asks for a user name or waits for the /end command.
  3. If the User does not respond to the bot within X seconds, the bot stops waiting for a response from the user and notifies him that the bot no longer expects a response from the user.

Update №1

I added code from ExampleBot to my project. It’s hard to find information on the JDA-Utilities library, so I wrote sample code for several questions:

public class TestLoop extends Command {

    private EventWaiter waiter;
    private Database database;

    public TestLoop(Database database, EventWaiter waiter) {
        this.waiter = waiter;
        this.name = "addperson";
        this.help = "add person to server";
        this.database = database;
    }

    @Override
    protected void execute(CommandEvent event)
    {
        // ask what is the user's name
        event.reply("What is your name?");

        // wait for a response
        waiter.waitForEvent(MessageReceivedEvent.class,
                e -> e.getAuthor().equals(event.getAuthor()) && e.getChannel().equals(event.getChannel()),

                e -> {
                    e.getChannel().sendMessage("Thank you, `" + e.getMessage().getContentRaw() + "`. What is your age?").queue();
                    waiter.waitForEvent(MessageReceivedEvent.class,
                            w -> w.getAuthor().equals(e.getAuthor()) && w.getChannel().equals(e.getChannel()),
                            w -> w.getChannel().sendMessage("So, you,re `" + e.getMessage().getContentRaw() + "` and your age is `" + w.getMessage().getContentRaw() + "`").queue());
                },

            // if the user takes more than a minute, time out
            10, TimeUnit.SECONDS, () -> event.reply("Sorry, you took too long."));
    }
}
1 Answers
Best Answer
Mikhail answered 3 weeks ago
Your Answer

8 + 11 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com