Postconditions

Postconditions

Postconditions have similar structure to preconditions, but they are executed when the command fails.

You can create a postcondition by creating a module that extends the built-in postcondition module PostconditionModule

Structure

import { PostconditionModule } from "reciple";

export class MyPostcondition extends PostconditionModule {
    async execute(data) {
        return true;
    }
}

export default new MyPostcondition();

A postcondition must implement the abstract method execute(). The method must return a value whether the postcondition data is handled or not.

Return Values

  • true — The postcondition has successfully handled the postcondition data.
  • false — The postcondition has failed to handle the postcondition data.
  • string — The postcondition has failed to handle the postcondition data. The given string is used as the reason message (Not an error).
  • Error — The postcondition has failed to handle the postcondition data with the given error.
  • CommandPostcondition.ResultData — The postcondition has failed to handle the postcondition data with the given result data.

Failed vs Error

Similar to preconditions, postconditions also have failed and error states.

Failed

A failed postcondition is defined when the postconditions’s execute() method returns false, string, or result data with a success property set to false.

If the value returned is false or string values, it will be coerced to a result data with a success property set to false and the given string as message.

Error

An error postcondition is defined when the postconditions’s execute() method throws or returns an error. You can also return a result data with a success property set to false and the error assigned to the error property.

The postcondition error will be thrown. You can handle postcondition errors by using a postconditionError event listener on the Client instance.

Execution

All postconditions are executed first, then each result data of each postcondition are stored in a PostconditionResultManager. The postcondition result manager is then used to determine whether the postconditions has an error or failed.

Execute Data

The data passed to the postcondition’s execute() method is a PostconditionData instance. These data describes what is need to be handled. You may encounter the following execute data:

UnknownExecuteData

The execute data is unknown. This is most likely a custom data passed from a precondition.

CooldownExecuteData

The execute data is a cooldown. This is used to handle cooledown commands.

PreconditionErrorExecuteData

The execute data is a precondition error. This is used to handle precondition errors. The error is thrown if the precondition error is not handled.

PreconditionFailedExecuteData

The execute data that is used when a precondition fails. This is used to handle precondition results with a success property set to false.

InvalidArgsExecuteData

This is an execute data for invalid arguments. This is only used for message commands to handle invalid arguments.

MissingArgsExecuteData

This is an execute data for missing arguments. This is only used for message commands to handle missing arguments.

InvalidFlagsExecuteData

This is an execute data for invalid flags. This is only used for message commands to handle invalid flags.

MissingFlagsExecuteData

This is an execute data for missing flags. This is only used for message commands to handle missing flags.

Precondition Trigger

Whenever a precondition specifically triggers a certain postcondition, the result data of that precondition is passed to the second parameter of the postcondition’s execute() method.