Skip to content

Answer Object

GodDragoner edited this page Sep 6, 2018 · 7 revisions

What is an answer object?

An answer object is used to store and represent the latest message that the sub sent. It is returned by the sendInput(...) chat method.

How do I use it properly?

The answer object provides multiple methods to check whether the answer is fitting what you are looking for. These are the methods:

answer::matchesRegex(String... regexes) Returns whether one of the provided regexes is matching the answer string

answer::matchesRegexLowerCase(String... regexes) Returns whether one of the provided regexes is matching the lower case answer string. Useful if you want to ignore the case without implementing it into your regex pattern.

answer::contains(String... regexes) Returns whether the answer contains one of the provided strings.

answer::isLike(String... regexes) Returns whether the answer contains one of the provided strings ignoring the case.

answer::isTimeout() Returns whether the answer has timed out.

answer::loop() Loops the answer if you want to wait for a new message from the sub.

answer::isInteger()/isInt() Returns whether the answer is an integer

answer::getInteger()/getInt() Returns the integer object represented by the answer. Never use without checking isInteger first

answer::isDouble() Returns whether the answer is a double

answer::getDouble() Returns the double object represented by the answer. Never use without checking isDouble first

answer::getAnswer() Returns the answer the user gave as a string

answer::addOption(String optionMessage) Adds an option to the lazy sub interface that sends the message provided when clicked

answer::addOption(String optionName, String optionMessage) Adds an option to the lazy sub interface and sends the message provided when clicked

answer::clearOptions() Clears all non default options from the lazy sub interface, always use when done with an answer unless you want those options to remain available

Okay, I get it but still I want an example

answer = sendInput("Hey %name%");
while (true) {
    if (answer.matchesRegexLowerCase("hey([ ]|$)", "hello([ ]|$)", "hi([ ]|$)")) {
        break;
    } else {
        sendMessage("What did you say?");
        answer.loop();
    }
}
sendMessage("You greeted me!");

With timeout:

answer = sendInput("Hey %name%", 10);
while (true) {
    if (answer.matchesRegexLowerCase("hey([ ]|$)", "hello([ ]|$)", "hi([ ]|$)")) {
        sendMessage("You greeted me!");
        break;
    } else if(answer.isTimeout()) {
        sendMessage("You failed to greet me properly within 10 seconds.");
        //Punish
    } else {
        sendMessage("What did you say?");
        answer.loop();
   }
}
sendMessage("Okay, let's move on");

Accept custom integer input:

    sendMessage("Which personality do you choose...");
    answer = sendInput("1, 2 or 3?");

    while (true) {
        if (answer.isInteger()) {
            const id = answer.getInt();
            if (id > 3) {
                sendMessage("You can't choose a number higher than 3. 1, 2 or 3?");
                answer.loop();
            } else {
                setVar("personalityStrictness", id - 1);
                break;
            }
        } else {
            sendMessage("1, 2 or 3?");
            answer.loop();
        }
    }

Accept custom string input (simplified - just use getAnswer in general if you want to get the full string the user replied):

sendMessage("So tell me the full name of the first person", false);
setVar('blackmailName1', createInput().getAnswer());
sendMessage("Good, and now their phone number...", false);
setVar('blackmailPhone1', createInput().getAnswer());

Clone this wiki locally